├── MIT-LICENSE.txt ├── README.md ├── example.html └── jquery.form-repeater.js /MIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2011 Corey Ballou and other contributors 2 | http://www.coreyballou.com 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining 5 | a copy of this software and associated documentation files (the 6 | "Software"), to deal in the Software without restriction, including 7 | without limitation the rights to use, copy, modify, merge, publish, 8 | distribute, sublicense, and/or sell copies of the Software, and to 9 | permit persons to whom the Software is furnished to do so, subject to 10 | the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be 13 | included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 19 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 21 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | jQuery Form Element Repeater Plugin 2 | =================================== 3 | 4 | A jQuery plugin for creating repeatable form elements, i.e. an array of input elements with add/remove capabilities. In other words, handling an array of objects (better known as a collection). 5 | 6 | [Official Documentation Website](https://www.coreyballou.com/projects/jquery-form-element-repeater-plugin/) 7 | 8 | ## Example Usage ## 9 | 10 | To use the plugin, we have a few conventions in place. One of the main conventions is the usage of jQuery data elements which dictate how element names and IDs get generated as well as how label names get generated. 11 | 12 | #### Label Naming #### 13 | Labels for form elements generally have clear naming conventions. 14 | 15 | To dynamically add and remove repeatable elements and groups, we need a method for incrementing the `for` attribute name. We accomplish this via the `data-pattern-text` attribute. 16 | 17 | The `data-pattern-text` attribute accepts a string as well as template keyword indicating the increment method: 18 | 19 | * **+=** This operator will increment by the numeric number following **+=**, i.e. `+=2`. 20 | * **++** This operator will increment by 1. 21 | 22 | #### Form Element Naming/Tagging #### 23 | 24 | The `data-pattern-id` attribute is used to dynamically determine the id of each form element. 25 | The `data-pattern-name` attribute is used to dynamically determine the name of each form element. 26 | 27 | These two data attributes accept the same template keywords as `data-pattern-text`. 28 | 29 | #### Example Code #### 30 | 31 | ```html 32 |
33 |
34 |

35 | 36 | 37 |

38 | 39 |

40 | 41 | 42 |

43 | 44 |

45 | 46 | 47 | 48 |

49 |
50 | 51 | 52 | 53 |
54 | 55 | 56 | 57 | 74 | ``` 75 | 76 | ## Config Options/Params ## 77 | 78 | * **btnAddClass:** The class name of the add button for creating new repeatable groups/items. _(string)_ 79 | * **btnRemoveClass:** The class name of the remove button for deleting a repeatable group/item. _(string)_ 80 | * **groupClass:** The class name of a newly created group/item DIV wrapper. _(string)_ 81 | * **minItems:** The minimum number of items to display on load. _(integer, default 1)_ 82 | * **maxItems:** The maximum number of allowable items/groups. 0 means unlimited. _(integer, default 0)_ 83 | * **startingIndex:** The starting index for group items. _(integer, default 0)_ 84 | * **showMinItemsOnLoad** Repeat container *{minItems}* times on first-load _(boolean, default false)_ 85 | * **reindexOnDelete:** Force re-index all group items on delete. _(boolean, default true)_ 86 | * **repeatMode:** The type of insertion mode for new group items. _(string, default append)_ 87 | * **animation:** Uh, I forgot. _(default null)_ 88 | * **animationSpeed:** The default animation speed in milliseconds. _(integer, default 400)_ 89 | * **animationEasing:** The easing animation effect. _(string, default 'swing')_ 90 | * **clearValues:** Whether values should be cleared out when cloning. _(boolean, default true)_ 91 | 92 | ## Passing in a data object ## 93 | 94 | The plugin supports automatically cloning the repeatable object *n* times based on the content of a data object. 95 | 96 | The data object should contain an array for each item which should be filled into the repeatable container, each inputs name should be provided in full. 97 | 98 | ```javascript 99 | ... 100 | $('.container').repeater({ 101 | ... 102 | }, [ 103 | { 104 | "vehicle[0][name]": "name1", 105 | "vehicle[0][type]": "type1" 106 | }, 107 | { 108 | "vehicle[1][name]": "name2", 109 | "vehicle[1][type]": "type2" 110 | } 111 | ]); 112 | ... 113 | ``` 114 | 115 | Using the configuration in the first example, this would add an extra clone to the repeatable form and fill both entries with the supplied data, the first entry would follow the "minItems" option and have no removal button, whereas the second item would be removable. 116 | 117 | 118 | [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/cballou/jquery-form-element-repeater-plugin/trend.png)](https://bitdeli.com/free "Bitdeli Badge") 119 | -------------------------------------------------------------------------------- /example.html: -------------------------------------------------------------------------------- 1 | 2 | Test 3 | 4 |

jQuery Form Repeater Example

5 |

Example 1: Create a Group of Inputs

6 |
7 |
8 |

9 | 10 | 11 |

12 |

13 | 14 | 15 |

16 |

17 | 18 | 19 |

20 |
21 | 22 |
23 | 24 |

Code:

25 |
26 | $('#example1').repeater({
27 | 	btnAddClass: 'r-btnAdd',
28 | 	btnRemoveClass: 'r-btnRemove',
29 | 	groupClass: 'r-group',
30 | 	minItems: 1,
31 | 	maxItems: 0,
32 | 	startingIndex: 0,
33 |     showMinItemsOnLoad: true,
34 | 	reindexOnDelete: true,
35 | 	repeatMode: 'append',
36 | 	animation: null,
37 | 	animationSpeed: 400,
38 | 	animationEasing: 'swing',
39 | 	clearValues: true
40 | }, [
41 |     {
42 |         "vehicle[0][name]":"test",
43 |         "vehicle[0][type]":"test"
44 |     },
45 |     {
46 |         "vehicle[1][name]":"test2",
47 |         "vehicle[1][type]":"test2"
48 |     }
49 | ]);
50 | 
51 | 52 | 53 | 54 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /jquery.form-repeater.js: -------------------------------------------------------------------------------- 1 | /** 2 | * jQuery Form Repeater Plugin 0.1.0 3 | * 4 | * Copyright (c) 2011 Corey Ballou 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining 7 | * a copy of this software and associated documentation files (the 8 | * "Software"), to deal in the Software without restriction, including 9 | * without limitation the rights to use, copy, modify, merge, publish, 10 | * distribute, sublicense, and/or sell copies of the Software, and to 11 | * permit persons to whom the Software is furnished to do so, subject to 12 | * the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be 15 | * included in all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 | * 25 | * Example Usage: 26 | * 27 |
28 |
29 |

30 | 31 | 32 |

33 |

34 | 35 | 36 |

37 |

38 | <-- Add a remove button for the item. If one didn't exist, it would be added to overall group --> 39 | 40 |

41 |
42 | 43 |
44 | 60 | */ 61 | (function($) { 62 | 63 | $.fn.repeater = function(options, data) { 64 | var $container = $(this), 65 | $btnAdd, $btnRemove, patternName, patternId, patternText, 66 | idVal, nameVal, labelText, labelFor, $elem, elemName, 67 | $label, row, $newClone, $formElems; 68 | 69 | $container.opts = $.extend({}, $.fn.repeater.defaults, options); 70 | $container.repeatCount = 0; 71 | 72 | $btnAdd = $container.find('.' + $container.opts.btnAddClass); 73 | if (!$btnAdd.length) { 74 | alert('You must specify a valid jQuery selector for the add button option in Form Repeater.'); 75 | return false; 76 | } 77 | 78 | // parse out group details 79 | $container.group = $('.' + $container.opts.groupClass); 80 | if (!$container.group.length) { 81 | alert('You must specify a valid jQuery selector for the form element grouping option in Form Repeater.'); 82 | return false; 83 | } 84 | 85 | // ensure the remove button exists 86 | $btnRemove = $container.group.find('.' + $container.opts.btnRemoveClass); 87 | if (!$btnRemove.length) { 88 | $btnRemove = $('