Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top