Search code examples
phpjavascriptcodeigniterflexigrid

How to create a flexigrid with passed variable from another controller in code igniter?


My problem is kinda silly but I don't know how to do it. I'm creating a standard flexigrid using standard structure:

//ver lib

    /*
     * 0 - display name
     * 1 - width
     * 2 - sortable
     * 3 - align
     * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
     */
    $colModel['users.id'] = array('id',40,TRUE,'center',1);
    $colModel['users.first_name'] = array('first_name',80,TRUE,'center',0);
    $colModel['users.last_name'] = array('last_name',180,TRUE,'left',1);
    $colModel['users.email'] = array('email',120,TRUE,'left',0);
    $colModel['users.username'] = array('username',130, TRUE,'left',0);
    $colModel['users.activation_code'] = array('activation_code',80, FALSE, 'right',0);
    $colModel['users.activated'] = array('activated',80, FALSE, 'right',0);
    $colModel['lists'] = array('Lists',90, FALSE, 'right',0);

    /*
     * Aditional Parameters
     */
    $gridParams = array(
    'width' => 'auto',
    'height' => 400,
    'rp' => 15,
    'rpOptions' => '[10,15,20,25,40]',
    'pagestat' => 'Displaying: {from} to {to} of {total} items.',
    'blockOpacity' => 0.5,
    'title' => 'Hello',
    'showTableToggleBtn' => true
    );

    /*
     * 0 - display name
     * 1 - bclass
     * 2 - onpress
     */
    $buttons[] = array('Delete','delete','test');
    $buttons[] = array('separator');
    $buttons[] = array('Select All','add','test');
    $buttons[] = array('DeSelect All','delete','test');
    $buttons[] = array('separator');


    //Build js
    //View helpers/flexigrid_helper.php for more information about the params on this function
    $grid_js = build_grid_js('flex1',site_url("admin/users/list_users"),$colModel,'users.first_name','asc',$gridParams,$buttons);

    $data['js_grid'] = $grid_js;

    $this->load->view('admin_panel/admin_content', $data);

Now my question is: is it possible to create a flexigrid WITH a parameter in third URI segment? For instance, I want to display a list, which user created and to do that I need his id. I can easily do that within the same controller but I want to pass it to different controller and create new flexigrid form there. So in 'index()' method can I have, for instance $get_id variable passed from different controller? Like this?

        $grid_js = build_grid_js('flex1',site_url("admin/admin_lists/display_show_list/".$get_id),$colModel,'name','asc',$gridParams,$buttons);

I hope I explained my problem well enough. Can someone give me some help with this? Mostly appreciated for any hints!


Solution

  • There are 2 ways you can achieve this:

    1. Create Helper Instead of using a controller to build your grid structure, you should create a helper to do it instead. Refer to http://codeigniter.com/user_guide/general/helpers.html

    Simply wrap the function in a helper will do.

    1. Use Ajax

    Create a function in controller like such:

    //in controller

    function makeGrid() { 
      $id = $this->uri->segment(3);
      $grid_js = .....;
    
      echo json_encode($grid_js);
    }
    

    //in View (Javascript)

    $.ajax({ 
       url:"/path/makeGrid" + id
       ...
       success: function(data){
          $.flexigrid( {... data .... });
       }