parent
64d873a167
commit
954178c4b1
3 changed files with 87 additions and 29 deletions
@ -1,15 +1,51 @@ |
|||||||
$(document).ready(function(){ |
$(document).ready(function(){ |
||||||
$('#id_theme').change(function(){ |
$('#id_theme').change(function(){ |
||||||
$.get( |
$.get( |
||||||
"/admin/ajax_tag/", {'id': $(this).serialize()}, function(j){ |
"/admin/ajax_tag/", {'id': $(this).serialize()}, function(data){ |
||||||
if (j!='error'){ |
var optionValues = []; |
||||||
$('#id_tag').html(j); |
var getValues = []; |
||||||
$('#id_tag').attr('disabled', false); |
var selectedValues = [] |
||||||
} |
//push values sended from server in array
|
||||||
else { |
$.each(data, function(i, elem){ |
||||||
//delete all options
|
getValues.push(elem[0].toString()) |
||||||
$('#id_tag').find('option').remove(); |
}); |
||||||
} |
//delete options if they aren't in getvalues
|
||||||
}); |
//otherwise push it in array
|
||||||
}); |
//also push in array already selected values
|
||||||
}); |
$('#id_tag option').each(function() { |
||||||
|
var check = $.inArray($(this), getValues); |
||||||
|
if ($(this).is(':selected') ){ |
||||||
|
selectedValues.push($(this).val()) |
||||||
|
} |
||||||
|
if (check == -1){ |
||||||
|
$(this).remove() |
||||||
|
} |
||||||
|
else{ |
||||||
|
optionValues.push($(this).val()); |
||||||
|
} |
||||||
|
}); |
||||||
|
//generate new options
|
||||||
|
//old options unchanged
|
||||||
|
var html = '' |
||||||
|
$.each(data, function(i, elem){ |
||||||
|
var check = $.inArray(elem[0].toString(), optionValues); |
||||||
|
|
||||||
|
if (check == -1){ |
||||||
|
html += '<option value="'; |
||||||
|
html += elem[0]; |
||||||
|
html += '">'; |
||||||
|
html += elem[1]; |
||||||
|
html += '</option>'; |
||||||
|
} |
||||||
|
}); |
||||||
|
$('#id_tag').append(html); |
||||||
|
//select previous selected values
|
||||||
|
$('#id_tag option').each(function() { |
||||||
|
var check = $.inArray($(this).val(), selectedValues) |
||||||
|
if (check != -1){ |
||||||
|
$(this).attr('selected', 'selected'); |
||||||
|
} |
||||||
|
}); |
||||||
|
});//end get
|
||||||
|
});//end change
|
||||||
|
});//end ready
|
||||||
Loading…
Reference in new issue