this.cleanTemplate();
var initValues = this.element.data('values');
- $.each(initValues, function (k, v) {
- $this.initItem(v);
- });
+
if (initValues.length === 0) {
this.initItem();
+ } else {
+ $.each(initValues, function (k, v) {
+ $this.initItem(v);
+ });
}
this.element.on('click', '.additem' + this.bmidselector, function () {
this.initItem();
}
- console.log('removed', item);
-
var $this = this;
setTimeout(function () {
$this.updateAndChange();
},
initItem: function (values) {
+ console.log('initItem', values);
var $this = this;
var collapsed = true;
var isNew = false;
$(item).addClass('collapsed');
}
// Handle nested multiple bunch
+ var nested = [];
$(item).find('.subform>.bunchmultiple').each(function () {
var newrand = $this.id + '_' + Math.round(Math.random() * 10000000);
var sampleId = $(this).attr('id');
var html = replaceAll(sampleId, newrand, $(this).html());
$(this).html(html);
- $(this).attr('id', newrand)
+ $(this).attr('id', newrand);
+ nested.push(newrand);
});
$(this.element).find(".bunchmultiple__items" + this.bmidselector).append($(item));
this.setValues(item, values);
- $(item).find('.subform>.bunchmultiple').bunchmultiple();
+
+ $.each(nested, function (k, v) {
+ if (isNew) {
+ $('#' + v).find('.item:not(.sample)').remove();
+ }
+ $("#" + v).bunchmultiple();
+ });
+
$(item).find('.form-group').find('input, select, .dropzone, textarea').each(function () {
if ($(this).closest('.bunchmultiple').attr('id') !== $this.id) {