From 53143481cfba1e4fc7b714936c845e145d4e908c Mon Sep 17 00:00:00 2001 From: Jen Wachter Date: Sun, 12 Nov 2017 11:25:03 -0500 Subject: [PATCH] Removed `startWith` option and added `min` option -- closes #7 New `min` option initializes the form with the specified number of elements (what `startWith` did), but also ensures that the specified number of elements are always in the form (cannot be deleted). --- README.md | 2 +- jquery.repeatable.js | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index b6ac2de..6feda03 100644 --- a/README.md +++ b/README.md @@ -64,9 +64,9 @@ When a user clicks on the `.add` button, the script will render a new `.field-gr * __deleteTrigger__: _Optional_. (string) The selector that when clicked, the field group the delete selector is within will be removed from the form. Default: ".delete" * __itemContainer__: _Optional_. (string) The selector that represents each field group container. Default: ".field-group" * __max__: _Optional_. (integer) The maximum number of field group elements that may be added to the repeatable item container. Default: null. +* __min__: _Optional_. (integer) The minimum number of field group elements that may be present in the repeatable item container. The form is prepopulated with this amount of field group elements. Default: 0. * __beforeAdd__: _Optional_. (function) A function to run before an item is added to the repeatable item container. Default: none * __afterAdd__: _Optional_. (function) A function to run after an item is added to the repeatable item container. Default: none * __beforeDelete__: _Optional_. (function) A function to run before an item is deleted from the repeatable item container. Default: none * __afterDelete__: _Optional_. (function) A function to run after an item is deleted from the repeatable item container. Default: none -* __startWith__: _Optional_. (integer) The number of field group elements to prepopulate the form with. Default: 0 * __template__: _Required_. (string) The selector that contains the form field group template. diff --git a/jquery.repeatable.js b/jquery.repeatable.js index f017d5c..3eb681d 100644 --- a/jquery.repeatable.js +++ b/jquery.repeatable.js @@ -10,7 +10,7 @@ addTrigger: ".add", deleteTrigger: ".delete", max: null, - startWith: 0, + min: 0, template: null, itemContainer: ".field-group", beforeAdd: function () {}, @@ -25,20 +25,20 @@ * @type {Number} */ var i = 0; - + /** * DOM element into which repeatable * items will be added * @type {jQuery object} */ var target = $(this); - + /** * Blend passed user settings with defauly settings * @type {array} */ var settings = $.extend({}, defaults, userSettings); - + /** * Total templated items found on the page * at load. These may be created by server-side @@ -49,7 +49,7 @@ return $(target).find(settings.itemContainer).length; }(); - + /** * Add an element to the target * and call the callback function @@ -72,6 +72,7 @@ var deleteOne = function (e) { e.preventDefault(); settings.beforeDelete.call(this); + if (total === settings.min) return; $(this).parents(settings.itemContainer).first().remove(); total--; maintainAddBtn(); @@ -126,12 +127,12 @@ $("form").on("click", settings.deleteTrigger, deleteOne); if (!total) { - var toCreate = settings.startWith - total; + var toCreate = settings.min - total; for (var j = 0; j < toCreate; j++) { createOne(); } } - + })(); };