⚠ 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 dropdown selected index



KaBaDaBrA
  • profile picture
  • Member

Posted 20 March 2012 - 16:15 PM

Hi :D

I currently have a feature where the client can add models to a vehicle. For example when adding a new model, he inserts the model title and selects a vehicle from the dropdown. Kk...now the problem is the client wants the dropdown to remain on the previously selected item. So if he chose "BMW" and clicks save the selected should still be "BMW".

How can I achieve this?

I thought about using the "callback_after_insert" feature to save it as a session item and then using jQuery to set the selected index. Would this be a good way of doing it or is there an easier way using GCRUD?

TIA!!! :)

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 20 March 2012 - 18:54 PM

What a weird request! You can do it only with javascript. Just change the clearForm functionallity. So for each dropdown just don't clear it. So you will just go to:
[b]assets/grocery_crud/themes/flexigrid/js/flexigrid-add.js - line 92[/b]
(I have the changes with a big arrow)

function clearForm()
{
$('#crudForm').find(':input').each(function() {
switch(this.type) {
case 'password':
case 'select-multiple':
//case 'select-one': // <--------------------------------------------------------------------------------------------------------------
case 'text':
case 'textarea':
$(this).val('');
break;
case 'checkbox':
case 'radio':
this.checked = false;
}
});
/* Clear upload inputs */
$('.open-file,.gc-file-upload,.hidden-upload-input').each(function(){
$(this).val('');
});
$('.upload-success-url').hide();
$('.fileinput-button').fadeIn("normal");
/* -------------------- */
$('.remove-all').each(function(){
$(this).trigger('click');
});
/*
//CHANGE THIS <--------------------------------------------------------------------------------------------------------------
$('.chosen-multiple-select, .chosen-select, .ajax-chosen-select').each(function(){
$(this).trigger("liszt:updated");
});
*/
/* TO THIS */ <-----------------------------------------------------------------------------------------------------------------
$('.chosen-multiple-select').each(function(){
$(this).trigger("liszt:updated");
});
}

KaBaDaBrA
  • profile picture
  • Member

Posted 20 March 2012 - 20:13 PM

I know right?! Odd request but apparently needs to work that way :huh: Thanks muchos much!!!

Baldrick
  • profile picture
  • Member

Posted 18 September 2012 - 10:37 AM

This topic is kind of old, but I wanted to add my contribution in case anyone led here via search or google would find it useful.

I ran into something similar (so I guess it's not so odd :P ) and since I wanted to disturb core files as little as possible, this is how I did it:

In [b]assets/grocery_crud/themes/xxx/js/xxx-add.js [/b](xxx being datatables/flexigrid/whatever), find the clearForm() function, and change the first line:


$('#crudForm').find(':input').each(function() {




to


$('#crudForm').find(':input').not('.noclear').each(function() {




And then make sure the fields you don't want to reset have the "noclear" CSS class. For example, by inserting this into the view's HTML when getState == 'add':


<script type=\"text/javascript\">
$(document).ready(function(){
$('#field-whatever,#field-something_or_other,#field-thisoneaswell').addClass('noclear');
});
</script>


Or by using appropriate callbacks.