From aeff3d0d18fb1c515f64db6d5e6052074264937e Mon Sep 17 00:00:00 2001 From: Marco Germani Date: Tue, 6 Aug 2019 17:41:28 +0200 Subject: [PATCH] Fix kartik Select2 and DepDrop Fix kartik Select2 and DepDrop for multiple instances --- src/assets/yii2-dynamic-form.js | 57 ++++++++++----------------------- 1 file changed, 17 insertions(+), 40 deletions(-) diff --git a/src/assets/yii2-dynamic-form.js b/src/assets/yii2-dynamic-form.js index 1fbdec1..7b6a44f 100644 --- a/src/assets/yii2-dynamic-form.js +++ b/src/assets/yii2-dynamic-form.js @@ -39,11 +39,11 @@ }, addItem: function (widgetOptions, e, $elem) { - _addItem(widgetOptions, e, $elem); + _addItem(widgetOptions, e, $elem); }, deleteItem: function (widgetOptions, e, $elem) { - _deleteItem(widgetOptions, e, $elem); + _deleteItem(widgetOptions, e, $elem); }, updateContainer: function () { @@ -66,30 +66,18 @@ }); $template.find('input, textarea, select').each(function() { - if ($(this).is(':checkbox') || $(this).is(':radio')) { - var type = ($(this).is(':checkbox')) ? 'checkbox' : 'radio'; - var inputName = $(this).attr('name'); - var $inputHidden = $template.find('input[type="hidden"][name="' + inputName + '"]').first(); - var count = $template.find('input[type="' + type +'"][name="' + inputName + '"]').length; - - if ($inputHidden && count === 1) { - $(this).val(1); - $inputHidden.val(0); - } + $(this).val(''); + }); - $(this).prop('checked', false); - } else if($(this).is('select')) { - $(this).find('option:selected').removeAttr("selected"); - } else { - $(this).val(''); + $template.find('input[type="checkbox"], input[type="radio"]').each(function() { + var inputName = $(this).attr('name'); + var $inputHidden = $template.find('input[type="hidden"][name="' + inputName + '"]').first(); + if ($inputHidden) { + $(this).val(1); + $inputHidden.val(0); } }); - // remove "error/success" css class - var yiiActiveFormData = $('#' + widgetOptions.formId).yiiActiveForm('data'); - $template.find('.' + yiiActiveFormData.settings.errorCssClass).removeClass(yiiActiveFormData.settings.errorCssClass); - $template.find('.' + yiiActiveFormData.settings.successCssClass).removeClass(yiiActiveFormData.settings.successCssClass); - return $template; }; @@ -199,7 +187,7 @@ matches[2] = matches[2].substring(1, matches[2].length - 1); var identifiers = matches[2].split('-'); identifiers[0] = index; - + if (identifiers.length > 1) { var widgetsOptions = []; $elem.parents('div[data-dynamicform]').each(function(i){ @@ -207,7 +195,7 @@ }); widgetsOptions = widgetsOptions.reverse(); - for (var i = identifiers.length - 1; i >= 1; i--) { + for (var i = identifiers.length - 2; i >= 1; i--) { identifiers[i] = $elem.closest(widgetsOptions[i].widgetItem).index(); } } @@ -225,7 +213,7 @@ $(this).removeClass('field-' + id).addClass('field-' + newID); }); // update "for" attribute - $elem.closest(widgetOptions.widgetItem).find("label[for='" + id + "']").attr('for',newID); + $elem.closest(widgetOptions.widgetItem).find("label[for='" + id + "']").attr('for',newID); } return newID; @@ -447,30 +435,19 @@ $(this).select2('destroy'); } - var configDepdrop = $(this).data('depdrop'); - if (configDepdrop) { - configDepdrop = $.extend(true, {}, configDepdrop); - $(this).removeData().off(); - $(this).unbind(); - _restoreKrajeeDepdrop($(this)); - } - - $.when($('#' + id).select2(configSelect2)).done(initSelect2Loading(id, '.select2-container--krajee')); + var s2LoadingFunc = typeof initSelect2Loading != 'undefined' ? initSelect2Loading : initS2Loading; + var s2OpenFunc = typeof initSelect2DropStyle != 'undefined' ? initSelect2Loading : initS2Loading; + $.when($('#' + id).select2(configSelect2)).done(s2LoadingFunc(id, '.select2-container--krajee')); var kvClose = 'kv_close_' + id.replace(/\-/g, '_'); $('#' + id).on('select2:opening', function(ev) { - initSelect2DropStyle(id, kvClose, ev); + s2OpenFunc(id, kvClose, ev); }); $('#' + id).on('select2:unselect', function() { window[kvClose] = true; }); - - if (configDepdrop) { - var loadingText = (configDepdrop.loadingText) ? configDepdrop.loadingText : 'Loading ...'; - initDepdropS2(id, loadingText); - } }); } };