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条)

报告相同问题?

悬赏问题

  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用