From 057445dd9f251cdee3aa9126877cd35a3c305527 Mon Sep 17 00:00:00 2001 From: Codice Ovvio Date: Sun, 4 Jul 2021 02:44:17 +0200 Subject: [PATCH 1/2] Add after_remove function - decrement 'data-rf-row-count' when a row is deleted --- repeatable-fields.js | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/repeatable-fields.js b/repeatable-fields.js index f77289b..1a6a3d9 100644 --- a/repeatable-fields.js +++ b/repeatable-fields.js @@ -25,7 +25,21 @@ $(container).attr('data-rf-row-count', row_count); }; - + + self.after_remove = function(container, del_row) { + var row_total = $(container).attr('data-rf-row-count'); + + row_total--; + + $('*', del_row).each(function() { + $.each(this.attributes, function() { + this.value = this.value.replace(self.settings.row_count_placeholder, row_total - 1); + }); + }); + + $(container).attr('data-rf-row-count', row_total); + }; + self.default_settings = { wrapper: '.wrapper', container: '.container', @@ -41,7 +55,7 @@ before_add: null, after_add: self.after_add, before_remove: null, - after_remove: null, + after_remove: self.after_remove, sortable_options: null, row_count_placeholder: '{{row-count-placeholder}}', }; @@ -104,7 +118,7 @@ self.settings.after_remove(container); } }); - + if(self.settings.is_sortable === true) { if(typeof $.ui !== 'undefined' && typeof $.ui.sortable !== 'undefined') { var sortable_options = self.settings.sortable_options !== null ? self.settings.sortable_options : {}; @@ -120,10 +134,10 @@ } var steps = 1; - + if($(event.target).siblings(self.settings.move_steps).length === 1) { var custom_steps = parseInt($(event.target).siblings(self.settings.move_steps).val(), 10); - + if(isNaN(custom_steps) === false && (custom_steps > 0 || custom_steps === -1)) { steps = custom_steps; } @@ -135,7 +149,7 @@ if($(event.target).is(self.settings.move_up) === true) { var previous_row; - + for(i = 0; steps === -1 ? true : i < steps; i++) { if(previous_row === undefined) { if(current_row.prev().not(self.settings.template).length === 1) { @@ -154,7 +168,7 @@ } } } - + if(previous_row !== undefined) { previous_row.before(current_row); } @@ -194,7 +208,7 @@ // Initialize all repeatable field wrappers self.initialize(self); - + return self; }; -})(jQuery); \ No newline at end of file +})(jQuery); From 7d0dbbb46335770ff93528d3fbc8f605267af9b3 Mon Sep 17 00:00:00 2001 From: Codice Ovvio Date: Sun, 4 Jul 2021 02:47:16 +0200 Subject: [PATCH 2/2] Update README.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3bd07b6..e0eb577 100644 --- a/README.md +++ b/README.md @@ -39,13 +39,13 @@ This plugin requires [jQuery](http://jquery.com/) and [jQuery UI Sortable](https Move Down - + An Input Field - + - + Remove @@ -77,7 +77,7 @@ is_sortable: true, before_add: null, after_add: self.after_add, before_remove: null, -after_remove: null, +after_remove: self.after_remove, sortable_options: null, row_count_placeholder: '{{row-count-placeholder}}', ``` @@ -133,4 +133,4 @@ row_count_placeholder: '{{row-count-placeholder}}',
row_count_placeholder
Specifies the row count placeholder to be used
- \ No newline at end of file +