⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Insert Account number on Add



drinkingwine
  • profile picture
  • Member

Posted 15 October 2019 - 14:40 PM

Hello Folks,

 

I'm very new to CRUD and have, what I hope is a simple question.

When I press the ADD button, the fields in the table are displayed.

I add button calls the controller/function and passes a record number for the column Account

I would to fill in this number on the add screen.

 

Please see the attached screen grabs.

 

Thanks in advance.

 

-ralph

 


drinkingwine
  • profile picture
  • Member

Posted 15 October 2019 - 14:42 PM

Here is the code for the function notes()

    public function notes($id)
    {
        $crud = $this->_getGroceryCrudEnterprise();
        $crud->setSkin('bootstrap-v4');
        $crud->setTable('notes');
        $crud->setSubject('Note''Notes');
        $crud->columns(['client_id','title','date','body']);
        $crud->where(['notes.client_id' => $id]);
        
        $crud->displayAs('client_id','Account');
        $crud->displayAs('date','Date of Note');
        $crud->unsetFields(['created_at''updated_at']);
        $crud->setTexteditor(['body''full_text']);
       
        $crud->fieldType('title''dropdown_search', [
            'Progress' => 'Progress',
            'General' => 'General'
        ]);
        
        $output = $crud->render();
        $data['title'] = 'Client:';
        $data['client'] = $this->client_model->get_clients($id);
        $client_name =  $data['client']['first_name'] . ' ' . 
                        $data['client']['middle_name'] . ' ' . 
                        $data['client']['last_name'];
            
        $this->_crud_output($output,$client_name);
    }
 

Soreno
  • profile picture
  • Member

Posted 15 October 2019 - 17:24 PM

Hi
Try callbackAddForm

$crud->callbackAddForm(function ($data) {
   $data['client_id'] = $this->yourModel->getYourId();
   return $data;
});

drinkingwine
  • profile picture
  • Member

Posted 16 October 2019 - 14:35 PM

Thanks for the help. I put this code above the render

$crud->callbackAddForm(function ($data) {
            $data['client_id'] = $this->client_model->get_clients($id);
            return $data;
         });
        

 

Page hangs on "loading"


Soreno
  • profile picture
  • Member

Posted 16 October 2019 - 16:36 PM

Why you use $this->client_model->get_clients($id) ? You want to display select in form ?

If you need to show current user info in add form try this:

    private $userId = 0;
    
    public function notes($id)
    {
        $crud = $this->_getGroceryCrudEnterprise();
        $crud->setSkin('bootstrap-v4');
        $crud->setTable('notes');
        $crud->setSubject('Note', 'Notes');
        $crud->columns(['client_id','title','date','body']);
        $crud->where(['notes.client_id' => $id]);
        
        $crud->displayAs('client_id','Account');
        $crud->displayAs('date','Date of Note');
        $crud->unsetFields(['created_at', 'updated_at']);
        $crud->setTexteditor(['body', 'full_text']);
       
        $crud->fieldType('title', 'dropdown_search', [
            'Progress' => 'Progress',
            'General' => 'General'
        ]);
        
        $crud->setRelation('client_id', 'clients', '{first_name} {middle_name} {last_name}', ['id' => $id]);
        $this->userId = $id;
        
        $crud->callbackAddForm(function ($data) {
            $data['client_id'] = $this->userId;
            return $data;
        });
        
        $output = $crud->render();
        $data['title'] = 'Client:';
        $data['client'] = $this->client_model->get_clients($id);
        $client_name =  $data['client']['first_name'] . ' ' . 
                        $data['client']['middle_name'] . ' ' . 
                        $data['client']['last_name'];
            
        $this->_crud_output($output,$client_name);
    }

In $crud->setRelation('client_id', 'clients', '{first_name} {middle_name} {last_name}', ['id' => $id]);

'clients' - table where you store clients info
'id' - PK field of 'clients' table
'first_name', 'middle_name', 'last_name' - fields with info in 'clients' table


Soreno
  • profile picture
  • Member

Posted 16 October 2019 - 16:50 PM

And I think you will need to use requiredFields or setRule to prevent empty records.


drinkingwine
  • profile picture
  • Member

Posted 17 October 2019 - 14:29 PM

Hello Soreno,

Thank you for your help. I added the setRelation and the loading hang has stopped.

However, there are no records found. If I comment out the setRelation then records are found.

 

public function notes($id)
    {
        $crud = $this->_getGroceryCrudEnterprise();
        $crud->setSkin('bootstrap-v4');
        $crud->setTable('notes');
        $crud->setSubject('Note''Notes');
        $crud->columns(['client_id','title','date','body']);
        $crud->where(['notes.client_id' => $id]);
        
        $crud->displayAs('client_id','Account');
        $crud->displayAs('date','Date of Note');
        $crud->unsetFields(['created_at''updated_at']);
        $crud->setTexteditor(['body''full_text']);
       
        $crud->fieldType('title''dropdown_search', [
            'Progress' => 'Progress',
            'General' => 'General'
        ]);
        
        $crud->setRelation('client_id''clients''{first_name} {middle_name} {last_name}', ['id' => $id]);
        $this->userId = $id;
        
        $crud->callbackAddForm(function ($data) {
            $data['client_id'] = $this->userId;
            return $data;
        });
                        
        $output = $crud->render();
        $data['title'] = 'Client:';
        $data['client'] = $this->client_model->get_clients($id);
        $client_name =  $data['client']['first_name'] . ' ' . 
                        $data['client']['middle_name'] . ' ' . 
                        $data['client']['last_name'];

         
        $this->_crud_output($output,$client_name);

        
        
    }

Soreno
  • profile picture
  • Member

Posted 17 October 2019 - 15:45 PM

 

Hello Soreno,

Thank you for your help. I added the setRelation and the loading hang has stopped.

However, there are no records found. If I comment out the setRelation then records are found.

 

I wrote setRelation parameters as an example. I don't know your DB structure, so if you need it, you must insert your own parameters.


drinkingwine
  • profile picture
  • Member

Posted 20 November 2019 - 15:22 PM

I could not get this to work as I want. The callbackAddForm is suppose to do what I want. However,

 

$crud->callbackAddForm(function ($data) {
    $data['account'] = '12345';
 
This will fill the account field with 12345.
 
if $id is set at '12345'
 
$crud->callbackAddForm(function ($data) {
    $data['account'] = $id;
 
This hangs with the "loading" prompt.
 
How can I use a variable here?
 
thanks in advance 

 


Soreno
  • profile picture
  • Member

Posted 20 November 2019 - 22:19 PM

Hello, drinkingwine.

Try to create private variable in you class than set your id value in it and use it in callbackAddForm function.

class YourController extends CI_Controller {

    private $accountId = 0;

    public function YourFunction($id)
    {
        ...
        $this->accountId = $id;
        
        $crud->callbackAddForm(function ($data) {
            $data['account'] = $this->accountId;
            return $data;
        });
        ...
    }

}

 


drinkingwine
  • profile picture
  • Member

Posted 21 November 2019 - 14:44 PM

That works, thank you.

I guess I have to try to figure out the difference between

 

$this->accountId and $id

 

both contain the value '123456'