Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update container's data-rf-row-count when removing row #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ This plugin requires [jQuery](http://jquery.com/) and [jQuery UI Sortable](https
<input type="text" class="move-steps" value="1" />
<span class="move-down">Move Down</span>
</td>

<td width="10%">An Input Field</td>

<td width="70%">
<input type="text" name="an-input-field[{{row-count-placeholder}}]" />
</td>

<td width="10%"><span class="remove">Remove</span></td>
</tr>
</tbody>
Expand Down Expand Up @@ -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}}',
```
Expand Down Expand Up @@ -133,4 +133,4 @@ row_count_placeholder: '{{row-count-placeholder}}',

<dt>row_count_placeholder</dt>
<dd>Specifies the row count placeholder to be used</dd>
</dl>
</dl>
32 changes: 23 additions & 9 deletions repeatable-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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}}',
};
Expand Down Expand Up @@ -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 : {};
Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -154,7 +168,7 @@
}
}
}

if(previous_row !== undefined) {
previous_row.before(current_row);
}
Expand Down Expand Up @@ -194,7 +208,7 @@

// Initialize all repeatable field wrappers
self.initialize(self);

return self;
};
})(jQuery);
})(jQuery);