From 453ef58b3d8ef33d0eb01612832e288d4022478c Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 6 Sep 2023 20:29:28 +0200 Subject: [PATCH] wip #6250 @0.5 --- src/app/Middleware/BunchRequest.php | 2 +- src/public/bunchmultiple/bunchmultiple.js | 52 ++++++++++++------- src/resources/views/fields/bunch.blade.php | 1 + .../views/fields/bunch_multiple.blade.php | 1 + 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/app/Middleware/BunchRequest.php b/src/app/Middleware/BunchRequest.php index 00e16b7..d481668 100644 --- a/src/app/Middleware/BunchRequest.php +++ b/src/app/Middleware/BunchRequest.php @@ -49,7 +49,7 @@ class BunchRequest extends CubistMiddleware $this->normalizeBunchInBunch($v); } if (stristr($k, '___')) { - $e = explode('___', $k); + $e = explode('___', $k, 2); $varname = $e[0]; $sub = $e[1]; if (!isset($data[$varname])) { diff --git a/src/public/bunchmultiple/bunchmultiple.js b/src/public/bunchmultiple/bunchmultiple.js index 7bebb08..558a524 100644 --- a/src/public/bunchmultiple/bunchmultiple.js +++ b/src/public/bunchmultiple/bunchmultiple.js @@ -295,29 +295,22 @@ }, setValues(item, values) { + let $this = this; var id = this.element.attr('id'); try { $.each(values, function (k, v) { var input = $(item).find('[name="' + id + '___' + k + '"]'); - - if (input.is('.hiddenfield')) { - $(input).parent().attr('data-values', JSON.stringify(v)); - } else if ($(input).nextAll('[type="checkbox"]').length > 0) { - var cb = $(input).nextAll('[type="checkbox"]'); - cb.attr('name', cb.prev('[type="hidden"]').attr('name')).attr('value', '1'); - cb.prop('checked', v === true || v === '1' || v === 1); - } else if ($(input).is('.select2_from_array')) { - try { - if ($(input).is("[data-ajax]") && $(input).find('option[value="' + k + '"]').length === 0) { - var options = $(input).data('options'); - var newOption = new Option(options[v], v, false, false); - $(input).append(newOption).trigger('change'); + if (input.length === 0) { + var bunch = $(item).find('[bp-field-name="' + id + '___' + k + '"][bp-field-type="bunch"]'); + var bunchName = $(bunch).attr('data-name'); + $.each(v, function (kk, vv) { + let bunchInput = $(bunch).find('[name="' + bunchName + '___' + kk + '"]'); + if (bunchInput.length > 0) { + $this.setInputVal(bunchInput, kk, vv); } - } catch (e) { - } - input.val(v); - } else if (input) { - input.val(v); + }); + } else { + $this.setInputVal(input, k, v); } }); } catch (e) { @@ -325,6 +318,29 @@ } this.updateAndChange(); }, + + setInputVal: function (input, k, v) { + if (input.is('.hiddenfield')) { + $(input).parent().attr('data-values', JSON.stringify(v)); + } else if ($(input).nextAll('[type="checkbox"]').length > 0) { + var cb = $(input).nextAll('[type="checkbox"]'); + cb.attr('name', cb.prev('[type="hidden"]').attr('name')).attr('value', '1'); + cb.prop('checked', v === true || v === '1' || v === 1); + } else if ($(input).is('.select2_from_array')) { + try { + if ($(input).is("[data-ajax]") && $(input).find('option[value="' + k + '"]').length === 0) { + var options = $(input).data('options'); + var newOption = new Option(options[v], v, false, false); + $(input).append(newOption).trigger('change'); + } + } catch (e) { + + } + input.val(v); + } else if (input) { + input.val(v); + } + }, }; jQuery.fn.bunchmultiple = function () { diff --git a/src/resources/views/fields/bunch.blade.php b/src/resources/views/fields/bunch.blade.php index 98d1983..f961948 100644 --- a/src/resources/views/fields/bunch.blade.php +++ b/src/resources/views/fields/bunch.blade.php @@ -7,6 +7,7 @@ if ($value == '') { $value = $empty; } + if (is_string($value)) { $value = json_decode($value, true); } diff --git a/src/resources/views/fields/bunch_multiple.blade.php b/src/resources/views/fields/bunch_multiple.blade.php index 4f901cd..8bb9cac 100644 --- a/src/resources/views/fields/bunch_multiple.blade.php +++ b/src/resources/views/fields/bunch_multiple.blade.php @@ -9,6 +9,7 @@ if (!is_string($value)) { $value = json_encode($value); } + @endphp
-- 2.39.5