首页 文章

Codeigniter Grocery Crud扩展set_relation()函数?

提问于
浏览
2

有没有人知道如何扩展 set_relation()

这是一个想法,基于Grocery Crud员工和办公室表的例子
sample table

$crud->set_relation('officeCode','offices','city');

代码输出将在下拉列表中显示所有城市,但我想扩展 set_relation() 以按状态过滤所有城市以显示在下拉列表中?

例如,我想在下拉列表中显示所有亚利桑那州的城市,有没有人在使用杂货店之前完成此操作?

参考示例:

Grocery Crud

4 回答

  • 5

    我想你需要这个:

    function employees_management()
    {
        $crud = new grocery_CRUD();
    
        $crud->set_theme('datatables');
        $crud->set_table('employees');
        // add this line
        $crud->callback_edit_field('officeCode',array($this,'_call_back_offices'));
        $crud->callback_add_field('officeCode',array($this,'_call_back_offices'));
    
        $crud->display_as('officeCode','Office City');
        $crud->set_subject('Employee');
    
        $crud->set_relation('officeCode','offices','city');
    
        $output = $crud->render();
    
        $this->_example_output($output);
    }
    
    function _call_back_offices()
    {
        $this->load->model('user_model'); // your model
        $data = $this->user_model->getYou('offices','officeCode, city', "state = '".$this->session->userdata('state')."'"); // iam using session here
        $hasil ='<select name="officeCode">';
        foreach($data as $x)
        {
            $hasil .='<option value="'.$x->officeCode.'">'.$x->city.'</option>';
        }
        return $hasil.'</select>';
    }
    
    example user_model code:
    
    function getYou($tabel,$field = '*', $cond ='')
    {
        $this->db->select($field);
        $this->db->from($tabel);
        if(!empty($cond)) $this->db->where($cond);
        return $this->db->get()->result();
    }
    
  • 4

    我知道我的回答是一些过时的,现在可能对OP没什么用处,但我猜有很多人可能正在看这篇文章,以寻找他们自己的set_relation问题的灵感 . 我提出了一个更简单的解决方案...(对Idham的公平性我不知道set_relation()的第4个参数何时可用 . )

    ...
    $state = "AZ"; // or however you get the state you want to filter by
    $crud->set_relation('officeCode','offices','city', 'officeCode IN (SELECT officeCode FROM offices WHERE state='.$state.')');
    ...
    

    瞧!真正的杂货_CRUD真棒!

  • 1
    $crud->set_relation('officeCode','offices','city',array('country' => 'Arizona'));
    

    这适用于版本v.1.2或更高版本

  • 1

    我相信你要找的是 - > http://www.grocerycrud.com/crud/function_name/set_model

    这将允许您以您想要的方式扩展杂货CRUD的功能 . 我实际上并没有冒险创建自己的自定义功能,但这就是你想要的方式 .

相关问题