django-smart-selects
django-smart-selects copied to clipboard
ChainedSelect not working in FormPreview
trafficstars
I got a working setup working with a CreateView, but I needed to change it to a FormPreview, but then my ChainedSelect stopped working (shows blank when selecting whatever it's chained to).
I went ahead and diffed the source, and I think I found the problem.
Usually, IDs in forms are id_field, but when using FormPreview, they are formpreview_field.
smart selects seems to be smart enough to change most of the IDs in the jquery to formtools_field, but there are a few that are id_field:
<script type="text/javascript">
//<![CDATA[
(function($) {
function fireEvent(element,event){
if (document.createEventObject){
// dispatch for IE
var evt = document.createEventObject();
return element.fireEvent('on'+event,evt)
}
else{
// dispatch for firefox + others
var evt = document.createEvent("HTMLEvents");
evt.initEvent(event, true, true ); // event type,bubbling,cancelable
return !element.dispatchEvent(evt);
}
}
function dismissRelatedLookupPopup(win, chosenId) {
var name = windowname_to_id(win.name);
var elem = document.getElementById(name);
if (elem.className.indexOf('vManyToManyRawIdAdminField') != -1 && elem.value) {
elem.value += ',' + chosenId;
} else {
elem.value = chosenId;
}
fireEvent(elem, 'change');
win.close();
}
$(document).ready(function(){
function fill_field(val, init_value){
if (!val || val==''){
options = '<option value="">---------<'+'/option>';
$("#formtools_model").html(options);
$('#formtools_model option:first').attr('selected', 'selected');
$("#formtools_model").trigger('change');
return;
}
$.getJSON("/chaining/filter/cars/CarModel/manufacturer/"+val+"/", function(j){
var options = '<option value="">---------<'+'/option>';
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].value + '">' + j[i].display + '<'+'/option>';
}
var width = $("#formtools_model").outerWidth();
$("#formtools_model").html(options);
if (navigator.appVersion.indexOf("MSIE") != -1)
$("#formtools_model").width(width + 'px');
$('#formtools_model option:first').attr('selected', 'selected');
var auto_choose = false;
if(init_value){
$('#formtools_model option[value="'+ init_value +'"]').attr('selected', 'selected');
}
if(auto_choose && j.length == 1){
$('#formtools_model option[value="'+ j[0].value +'"]').attr('selected', 'selected');
}
$("#formtools_model").trigger('change');
})
}
if(!$("#id_manufacturer").hasClass("chained")){
var val = $("#id_manufacturer").val();
fill_field(val, "None");
}
$("#id_manufacturer").change(function(){
var start_value = $("#formtools_model").val();
var val = $(this).val();
fill_field(val, start_value);
})
})
if (typeof(dismissAddAnotherPopup) !== 'undefined') {
var oldDismissAddAnotherPopup = dismissAddAnotherPopup;
dismissAddAnotherPopup = function(win, newId, newRepr) {
oldDismissAddAnotherPopup(win, newId, newRepr);
if (windowname_to_id(win.name) == "id_manufacturer") {
$("#id_manufacturer").change();
}
}
}
})(jQuery || django.jQuery);
//]]>
</script>
Thanks, I think smart select now have not handled the preview case, maybe we can add this feature in next release.