duankuiyuant3940 2013-03-05 09:48
浏览 34
已采纳

crud codeigniter默认值

Hi all I have a site develop in codeigniter with crud. Into my insert view I have a select where I want to put in default a value, I have done int his mode (the name of the filed is id_company)

$crud = new grocery_CRUD();
$state_crud = $crud->getState();
$crud->set_relation('id_company','company','name_company');
$crud->set_relation('id_plant','plant','name_plant');
$crud->set_relation('id_order','order','name_order');
$crud->field_type('id_plant','dropdown', array('0' => '') );
$crud->unset_print();
$crud->unset_export();
$crud->unset_delete();
$data['name_company'] = $company[0]['name_company'];
$data['id_company'] = $company[0]['id'];

$crud->callback_edit_field('id_company',array($this,'edit_field_callback_id_company_add'));

and the callback is:

function edit_field_callback_id_company_add($value, $primary_key){
    $company = $this->Company_model->getCompany($value);
    return '<div id="field-id_company" class="readonly_label">'.$company->name_company.'</div>';
}

my model function

function getCompany($id_company, $select = ''){
        if( isset($id_company) && $id_company > 0 ) : 
            $this->CI =& get_instance();
            if( $select ) $this->CI->db->select($select);
            $this->CI->db->where('id', $id_company);
            $query = $this->CI->db->get_where($this->company_table);
            return $query->result_array();
        endif;
        return FALSE;
    }

i have seen that crud insert a select with name field-id_company_czhn I have try to insert it but nothing. Where is the problem?

  • 写回答

2条回答 默认 最新

  • douyi6290 2013-03-06 08:12
    关注

    Update: Maybe if you try to add an if statement it should work. So in your case perhaps this will work fine for you:

    $crud = new grocery_CRUD();
    $state_crud = $crud->getState();
    
    if ($state_crud == 'edit' || $state_crud == 'update') {
        $crud->callback_edit_field('id_company',array($this,'edit_field_callback_id_company_add'));
    } else {
        $crud->set_relation('id_company','company','name_company');
    }
    
    ...
    

    The logic that you are using the callback is wrong. So in your case you need something like this:

     $crud->callback_edit_field('id_company',
                         array($this,'edit_field_callback_id_company_add'));
    

    and then:

     function edit_field_callback_id_company_add($value, $primary_key){
    
        $this->db->where('id',$value); //Where id is the primary key for company table
        $company = $this->db->get('company')->row();
    
        return '<div id="field-id_company" class="readonly_label">'.$company->name_company.'</div>';
     }
    

    There is at the documentation an article that explains how to use callbacks at: http://www.grocerycrud.com/documentation/tutorial_using_callbacks

    If you like you can also use the field_type method ( http://www.grocerycrud.com/documentation/options_functions/field_type ) with field_type = readonly

    As for the default value functionality there is an issue at github for that: https://github.com/scoumbourdis/grocery-crud/issues/138

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?