diff --git a/src/Form/Field/HasMany.php b/src/Form/Field/HasMany.php index e8f4b09ed8..e08ec46322 100644 --- a/src/Form/Field/HasMany.php +++ b/src/Form/Field/HasMany.php @@ -78,7 +78,7 @@ class HasMany extends Field /** * Create a new HasMany field instance. * - * @param $relationName + * @param $relationName * @param array $arguments */ public function __construct($relationName, $arguments = []) @@ -270,7 +270,6 @@ protected function resetInputKey(array &$input, array $column) * in the HasMany relation, has many data/field set, $set is field set in the below */ foreach ($input[$this->column] as $index => $set) { - /* * foreach the field set to find the corresponding $column */ @@ -489,6 +488,11 @@ protected function setupScriptForDefaultView($templateScript) */ $script = <<column} input.{$removeClass}[name^="{$this->column}[new_"]'); +if (old_new_inputs.length) { + var old_new_input_key_index = old_new_inputs.last().attr('name').match(/\[new_(\d+)\]/); + index += old_new_input_key_index ? old_new_input_key_index[1] : 0; +} $('#has-many-{$this->column}').off('click', '.add').on('click', '.add', function () { var tpl = $('template.{$this->column}-tpl'); @@ -544,6 +548,11 @@ protected function setupScriptForTabView($templateScript) }); var index = 0; +var old_new_inputs = $('#has-many-{$this->column} input.{$removeClass}[name^="{$this->column}[new_"]'); +if (old_new_inputs.length) { + var old_new_input_key_index = old_new_inputs.last().attr('name').match(/\[new_(\d+)\]/); + index += old_new_input_key_index ? old_new_input_key_index[1] : 0; +} $('#has-many-{$this->column} > .header').off('click', '.add').on('click', '.add', function(){ index++; var navTabHtml = $('#has-many-{$this->column} > template.nav-tab-tpl').html().replace(/{$defaultKey}/g, index); @@ -589,6 +598,11 @@ protected function setupScriptForTableView($templateScript) */ $script = <<column} input.{$removeClass}[name^="{$this->column}[new_"]'); +if (old_new_inputs.length) { + var old_new_input_key_index = old_new_inputs.last().attr('name').match(/\[new_(\d+)\]/); + index += old_new_input_key_index ? old_new_input_key_index[1] : 0; +} $('#has-many-{$this->column}').on('click', '.add', function () { var tpl = $('template.{$this->column}-tpl');