⚠ 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

Pass values from grid row to add form using add_action



marlaaragao
  • profile picture
  • Member

Posted 15 June 2015 - 11:26 AM

Hi, 

 

I created an action with add_action and it must redirect to add form, passing generated data from the row primary key to some of the fields. Here is how I did it:

    $this->grocery_crud->set_table($this->table);
    [...]


    //set the action
    $this->grocery_crud->add_action('Add', base_url() . 'assets/_templates/backend/images/external_link.gif'
     , 'planocontas/addAccount', 'sub-menu-icon');
    $this->grocery_crud->unset_jquery();       
    $output = $this->grocery_crud->render();

    //load the view
    $this->template->load('_templates/backend/backend_default', 'default_view', $output);
}

The action function:

public function addAccount($primary_key) {

        try {
            
            //get info from $primary_key
            $row = $this->plano_contas->findByPk($primary_key);
            
            if ($row != null) {
                
                //do stuff
                $id_conta = $this->plano_contas->_geraProximaSubConta($row->parent_id);

                //uses callback_add_field to pass the info 
                $this->grocery_crud->callback_add_field('id_conta', function () {
                    return '<input type="text" maxlength="50" value="' . $id_conta . '" name="natureza">';
                });
                
                [...]
            
                
            } else {
                [...]
            }

            $this->grocery_crud->set_table($this->db->table_prefix . $this->table);
            $this->grocery_crud->unset_list();
            $this->grocery_crud->unset_edit();
            $this->grocery_crud->render();
            
        //as I did [...]unset_list() I get an exception on render() so i'm redirected here:
        } catch (Exception $e) {

            if ($e->getCode() == 14) { //The 14 is the code of the "You don't have permissions" err                                          //or on grocery CRUD.]
                
                //here I redirect to the add form, it opens correctly, but the callback_add_field is never 'executed' and all the fields come blank  
                
                redirect(site_url(strtolower(__CLASS__) .  '/index/add/'));
           
            } else {
                show_error($e->getMessage());
                return false;
            }
        }
    }

I must be doing something wrong, can you help me? How can I acomplish this?

 

Second thing: at add action, on the style class, i have a class of my own, but the name concats with ' crud-action' and it doesn't work. Is it possible to use class style that is not from grocery crud's theme? 

 

I really need to pass the info for add form on the action click. Please help me! Thank you very much.

Ps.: when I click the grocery 'Add' button, the form has to come clean, the information is generated and passed only when the action is clicked.

 


Paul Savostin
  • profile picture
  • Member

Posted 15 June 2015 - 21:03 PM

describe please what a functionallity you need to achieve more clean way, mb i will help you, cause now i am not exactly understand what you wanna do


marlaaragao
  • profile picture
  • Member

Posted 16 June 2015 - 00:20 AM

Hello!

 

I need to add an action to each row of the grid so when I click it, I'm redirected to grocery crud's add form, but passing data that is generated by some functions of my own. Example:

 

Grid:

mYAJ8IN.png

 

 

Rediretcs to:

tDaPcTW.png

 

You can see the code in my first post. I add the action with add_action and then in the action function (addAccount) I have callback_add_field(s) to pass the data that I generate from the row primary key to the add form.

 

Thanks in advance!


Paul Savostin
  • profile picture
  • Member

Posted 16 June 2015 - 09:52 AM

Hi!. First of all, I dont understand why you set some ID just like numbers "Id conta" and "Parent Id" - trust me this is wrong!

 

Why? Because you cant remember what all ID means, That is why GC using set_relation and show name from related table - not ID and if do not need to show field, set it like "hidden". (by the way, you can set default value for hidden)

Anyway, in add_action set you custom values using session, and in callback_add_field make custom field with this values from session.



P.S. I think you do smth wrong from the beginning, that is why you have this problem. And I asked you write what do you need to achieve - not what and how you DID.


marlaaragao
  • profile picture
  • Member

Posted 17 June 2015 - 00:12 AM

Hi! Thanks for the answer.

 

First: I did not achieve this. It was an example of what I wanted to achieve, I just wrote the 1 and 2 in the fields and printed it.

 

Second: It's an example. This ids in the screen are examples. I want to get the row id, run some functions and generate some data (it's more complex than the '1' and '2' in the example, that is just an example). The names of the fields in this case may be "wrong" but.. anyways... It's an example only. I'll generate string values masked with a mask that the user chooses, and each user can have a diferent mask, but this is not important here, I just want to pass data to add form through a custom action, just it.

 

Your suggestion is very much appreciated, and I'll try it, but first I want to know: callback_add_field works inside the action? Because I debugged and they are added, but never executed. And I realize I did simething wrong, but it's impossible to be before this code I put here.

 

I'll try it, but as I said, what happens is that the callback_add_fields are never executed, I wanted to know why.

 

Thank you!


Paul Savostin
  • profile picture
  • Member

Posted 17 June 2015 - 09:48 AM

OK, whatever you say.

Lets look what is the add_action - from docs:
 

"Add an action/operation to the list table. The way to do that its simple:

  1. add the label of your subject, for example "Photo Gallery".
  2. add an image url . Notice that in the flexigrid theme is a required field.
  3. add your custom url. This url will be connected with the site_url function and the primary key. For example if you add 'my_controller/photo/' then it will transform to site_url('my_controller/photo/'.$primary_key_for_each_row)
  4. add a CSS class. Remember that at the theme of datatables a CSS class includes images , for example :  ui-icon-image, ui-icon-plus, e.t.c.
  5. You can even add your own url callback. If you don't want the default transformation of the url, you can use this paremeter to add your own algorithm of the url transformation"

 

How you see, this is just a LINK to smth related with current row(LIST state if talking about GC states). So callback_add_fields working only when you press add item, and response for view of the adding form fields.


Conclusion - add_action and callback_add_field unrelated att all.


marlaaragao
  • profile picture
  • Member

Posted 17 June 2015 - 13:03 PM

Hello!

 

Thanks for the explanation, I really didn't think about that the action is just a link...

 

I was able to try you session data suggestion today, and it works reaaally well. Thank you very much! It solved it, trying to put callback_add_field inside action was really weird, hehe. Your way is very simple and does what I want.

 

Anyway, thanks!


Paul Savostin
  • profile picture
  • Member

Posted 17 June 2015 - 15:18 PM

you're welcome!