/** * Created by dev on 07.09.2015. */ $(document).ready(function () { $('select').select2({ width: 'element', allowClear: true }); $('#id_country').change(function () { $.get( "/admin/ajax_city/", {'id': $(this).val()}, function (j) { $('#id_city').html(j); $('#id_city').attr('disabled', false); }); }); $('#id_place').select2({ placeholder:"Место проведения", widht: 400 }); $('#id_city').select2({ placeholder: "Город", width: 300, ajax: { url: "/admin/city/search/", dataType: "json", quietMillis: 200, data: function (term, page, country) { var country = $('#id_country').val() return { term: term, page: page, country: country }; }, results: function (data) { var results = []; $.each(data, function (index, item) { results.push({ id: item.id, text: item.label }); }); return {results: results}; } }, initSelection: function (element, callback) { var id = $(element).val(); var text = $(element).attr('data-init-text'); callback({id: id, text: text}); } }); $('#id_0-periodic').select2({ placeholder: "Периодичность", width: '350px' }); $('#id_tag').select2({ placeholder: "Теги", width: '350px', multiple: true, ajax: { url: "/admin/theme/tag/search/", dataType: "json", quietMillis: 200, multiple: true, data: function (term, page, theme) { var theme = $('#id_theme').serialize().replace(/0-theme/g, 'theme'); return { term: term, page: page, theme: theme }; }, results: function (data) { var results = []; $.each(data, function (index, item) { results.push({ id: item.id, text: item.label }); }); return {results: results}; } }, initSelection: function (element, callback) { var data = []; $(element.val().split(",")).each(function (i) { var item = this.split(':'); data.push({ id: item[0], text: item[1] }); }); callback(data); } }); // end selects /* */ // theme change $('#id_theme').change(function () { $.get( "/admin/ajax_tag/", {'id': $(this).serialize().replace(/0-theme/g, 'theme')}, function (data) { var optionValues = []; var getValues = []; var selectedValues = []; //push values sended from server in array $.each(data, function (i, elem) { getValues.push(elem[0].toString()) }); //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 += ''; } }); $('#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 console.log("hello from wizard.js"); });