⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Setting a default value in a dropdown?



fireproofsocks
  • profile picture
  • Member

Posted 01 August 2012 - 03:31 AM

I have a users table that has a relation to a countries table. So I can create and edit users fine, but I want to know if it's possible to set a default value for the dropdown field for country?



$crud->set_table('users');
$crud->set_subject('User');
$crud->required_fields('username','email');
$crud->columns('username', 'email');
$crud->set_relation('country_id', 'countries', 'name');


It seems that the dropdown is always empty and I have to manually select the country.

ravenwish
  • profile picture
  • Member

Posted 02 August 2012 - 21:30 PM

I'm still newbie in codeigniter and GroceryCrud but I think I could try to help if I don't give you a too bad answer ;)

Why don't you use callback_edit_field(or callback_field...depending what exactly you want to do with it) and use form_dropdown() (which is a codeigniter function in which you can select an element)

CodeIgniter documentation for form_dropdown()

Except if someone sees a better solution to your problem...obviously ^_^

Mir Rodriguez
  • profile picture
  • Member

Posted 08 November 2012 - 11:34 AM

Hello all and thanks web-johnny for a great library.

I just want to share my solution to setting default values in a dropdown using grocery CRUD. Surely there is a more efficient way but this works for me.

First you have to pass the variables from your controller to your view:

// On controller


$header = Array(
'defaultplace' => 4
);

$this->load->view('yourview', $header);


Then you read the values from your view and set the value of the dropdown using the javascript trigger for the Chosen plugin (https://github.com/harvesthq/chosen) which is used by grocery CRUD for dropdowns:

// Somewhere in your view


script type="text/javascript">
$(document).ready(function() {

document.getElementById("field-placeid").value = <?php echo $defaultplace?>;
$("#field-placeid").trigger("liszt:updated");

});

That's it!