0

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?

Alessandro Minoccheri
  • 35,521
  • 22
  • 122
  • 171

2 Answers2

2

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

John Skoumbourdis
  • 3,041
  • 28
  • 34
  • Thanks for your help, but my select is always the same, the selected element isn't the id_company – Alessandro Minoccheri Mar 06 '13 at 08:22
  • Then you just need to add the id_company at your fields method. For example $crud->fields('field1','field2','id_company'); . This should work for you ;-) – John Skoumbourdis Mar 06 '13 at 10:28
  • uhm sorry but this string isn't put inside the callback but after the call for example, in this case I have many field into my view ("notes,id_order ...) but only the field id_company must have a default value – Alessandro Minoccheri Mar 06 '13 at 10:35
  • Can you please update your Question with the full function that you use please, so I can understand better? Thanks. – John Skoumbourdis Mar 06 '13 at 11:56
  • I have update my question, I have cut most of part because is very long but I think this is the main part – Alessandro Minoccheri Mar 06 '13 at 12:05
  • Ok I've updated my answer. For now there is not such functionality in grocery CRUD to actually have "set_relation" field AND "readonly" the same time. Hope it works. – John Skoumbourdis Mar 06 '13 at 16:16
  • No errors it doesn't show.. I ahve called a function into the model to retrieve information like your code (because query is better to do in the model) but nothing – Alessandro Minoccheri Mar 06 '13 at 16:51
  • Did you replace with: $crud->callback_edit_field('id_company', array($this,'edit_field_callback_id_company_add')); as well? Weird!!! – John Skoumbourdis Mar 06 '13 at 17:43
  • Ok last turn, I've updated my answer as well, I hope this will work as expected this time ;-) – John Skoumbourdis Mar 06 '13 at 22:59
  • Something is better, I haven't the select but I have an inout text ok, but the input is empty, I have already the name of the company intio a variable but seems that doesn't call the callback to insert the name into the value – Alessandro Minoccheri Mar 07 '13 at 08:18
  • What version of grocery CRUD do you use and what Codeigniter version? – John Skoumbourdis Mar 07 '13 at 11:43
  • Aha! There you go. The problem is your grocery CRUD version. I was sure that this bug was fixed long time ago! I think if you download the latest version 1.3.3 you will not have this problem. I think from version 1.2 to 1.3.3 they are not any renames so it will probably just work for you without any issues. Of course make sure that you have a back-up first before the update :-) – John Skoumbourdis Mar 07 '13 at 11:53
  • I don't think that you will have any problems with the update. The update will probably be smoothly. But of course I am NOT Guaranteeing anything, that's why it is always better to have a backup before the update. Just make sure that the new assets folder,model,library and config is included as well. – John Skoumbourdis Mar 07 '13 at 13:16
-1

here decided this problem. i hope it help for you. http://www.grocerycrud.com/forums/topic/1846-setting-default-value-for-field-type-and-set-relation/

Baku Mn
  • 1
  • 1
  • please don't answer questions with only links; if the link ever rots, your answer becomes useless. links are great but answers should stand on their own. – Eevee Nov 05 '13 at 08:40
  • OK i see your idea. but, i test. report is successful. it is happy code for Grocery crud. – Baku Mn Nov 11 '13 at 05:00