Jump to content


Insert Account number on Add

Callback

  • Please log in to reply
10 replies to this topic

#1 drinkingwine

drinkingwine

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 15 October 2019 - 02: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

 

Attached Thumbnails

  • Screenshot 2019-10-15 09.35.48.png
  • Screenshot 2019-10-15 09.35.37.png


#2 drinkingwine

drinkingwine

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 15 October 2019 - 02: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);
    }
 


#3 Soreno

Soreno

    Member

  • Members
  • PipPip
  • 10 posts

Posted 15 October 2019 - 05:24 PM

Hi
Try callbackAddForm

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


#4 drinkingwine

drinkingwine

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 16 October 2019 - 02: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"



#5 Soreno

Soreno

    Member

  • Members
  • PipPip
  • 10 posts

Posted 16 October 2019 - 04: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



#6 Soreno

Soreno

    Member

  • Members
  • PipPip
  • 10 posts

Posted 16 October 2019 - 04:50 PM

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



#7 drinkingwine

drinkingwine

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 17 October 2019 - 02: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);

        
        
    }


#8 Soreno

Soreno

    Member

  • Members
  • PipPip
  • 10 posts

Posted 17 October 2019 - 03: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.



#9 drinkingwine

drinkingwine

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 20 November 2019 - 03: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 

 



#10 Soreno

Soreno

    Member

  • Members
  • PipPip
  • 10 posts

Posted 20 November 2019 - 10: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;
        });
        ...
    }

}

 



#11 drinkingwine

drinkingwine

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 21 November 2019 - 02: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'







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users