Jump to content


Setting a default value in a dropdown?

dropdown default

  • Please log in to reply
2 replies to this topic

#1 fireproofsocks

fireproofsocks

    Newbie

  • Members
  • Pip
  • 1 posts

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.

#2 Nico

Nico

    Member

  • Members
  • PipPip
  • 11 posts
  • LocationQuebec, Canada

Posted 02 August 2012 - 09: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 ^_^
-- Nico --

#3 Mir

Mir

    Newbie

  • Members
  • Pip
  • 1 posts

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!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users