You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
6.2 KiB

1 year ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #Repeater
  2. Creates an interface to add and remove a repeatable group of input elements.
  3. ###[Demo](http://briandetering.net/repeater)
  4. `bower install jquery.repeater --save`
  5. `npm install jquery.repeater --save`
  6. ##Templates
  7. Repeater uses the first "data-repeater-item" as a template for added items.
  8. ##Rewritten Name Attributes.
  9. Repeater rewrites your name attributes to avoid collisions within the same form.
  10. (since the name attributes will be repeated). In the example below, the
  11. name attributes would be renamed `group-a[0][text-input]` and `group-a[1][text-input]`.
  12. Checkbox inputs and Multiple Select inputs will have an additional `[]` appended. So for example a checkbox
  13. with name `foo` would be mapped to `group-a[0][foo][]`.
  14. Names get reindexed if an item is added or deleted.
  15. ##Example
  16. ```html
  17. <form class="repeater">
  18. <!--
  19. The value given to the data-repeater-list attribute will be used as the
  20. base of rewritten name attributes. In this example, the first
  21. data-repeater-item's name attribute would become group-a[0][text-input],
  22. and the second data-repeater-item would become group-a[1][text-input]
  23. -->
  24. <div data-repeater-list="group-a">
  25. <div data-repeater-item>
  26. <input type="text" name="text-input" value="A"/>
  27. <input data-repeater-delete type="button" value="Delete"/>
  28. </div>
  29. <div data-repeater-item>
  30. <input type="text" name="text-input" value="B"/>
  31. <input data-repeater-delete type="button" value="Delete"/>
  32. </div>
  33. </div>
  34. <input data-repeater-create type="button" value="Add"/>
  35. </form>
  36. <script src="path/to/jquery.js"></script>
  37. <script src="path/to/jquery.repeater/jquery.repeater.js"></script>
  38. <script>
  39. $(document).ready(function () {
  40. $('.repeater').repeater({
  41. // (Optional)
  42. // start with an empty list of repeaters. Set your first (and only)
  43. // "data-repeater-item" with style="display:none;" and pass the
  44. // following configuration flag
  45. initEmpty: true,
  46. // (Optional)
  47. // "defaultValues" sets the values of added items. The keys of
  48. // defaultValues refer to the value of the input's name attribute.
  49. // If a default value is not specified for an input, then it will
  50. // have its value cleared.
  51. defaultValues: {
  52. 'text-input': 'foo'
  53. },
  54. // (Optional)
  55. // "show" is called just after an item is added. The item is hidden
  56. // at this point. If a show callback is not given the item will
  57. // have $(this).show() called on it.
  58. show: function () {
  59. $(this).slideDown();
  60. },
  61. // (Optional)
  62. // "hide" is called when a user clicks on a data-repeater-delete
  63. // element. The item is still visible. "hide" is passed a function
  64. // as its first argument which will properly remove the item.
  65. // "hide" allows for a confirmation step, to send a delete request
  66. // to the server, etc. If a hide callback is not given the item
  67. // will be deleted.
  68. hide: function (deleteElement) {
  69. if(confirm('Are you sure you want to delete this element?')) {
  70. $(this).slideUp(deleteElement);
  71. }
  72. },
  73. // (Optional)
  74. // You can use this if you need to manually re-index the list
  75. // for example if you are using a drag and drop library to reorder
  76. // list items.
  77. ready: function (setIndexes) {
  78. $dragAndDrop.on('drop', setIndexes);
  79. },
  80. // (Optional)
  81. // Removes the delete button from the first list item,
  82. // defaults to false.
  83. isFirstItemUndeletable: true
  84. })
  85. });
  86. </script>
  87. ```
  88. ## Nested Example
  89. ```html
  90. <!-- outer repeater -->
  91. <form class="repeater">
  92. <div data-repeater-list="outer-list">
  93. <div data-repeater-item>
  94. <input type="text" name="text-input" value="A"/>
  95. <input data-repeater-delete type="button" value="Delete"/>
  96. <!-- innner repeater -->
  97. <div class="inner-repeater">
  98. <div data-repeater-list="inner-list">
  99. <div data-repeater-item>
  100. <input type="text" name="inner-text-input" value="B"/>
  101. <input data-repeater-delete type="button" value="Delete"/>
  102. </div>
  103. </div>
  104. <input data-repeater-create type="button" value="Add"/>
  105. </div>
  106. </div>
  107. </div>
  108. <input data-repeater-create type="button" value="Add"/>
  109. </form>
  110. <script src="path/to/jquery.js"></script>
  111. <script src="path/to/jquery.repeater/jquery.repeater.js"></script>
  112. <script>
  113. $(document).ready(function () {
  114. $('.repeater').repeater({
  115. // (Required if there is a nested repeater)
  116. // Specify the configuration of the nested repeaters.
  117. // Nested configuration follows the same format as the base configuration,
  118. // supporting options "defaultValues", "show", "hide", etc.
  119. // Nested repeaters additionally require a "selector" field.
  120. repeaters: [{
  121. // (Required)
  122. // Specify the jQuery selector for this nested repeater
  123. selector: '.inner-repeater'
  124. }]
  125. });
  126. });
  127. </script>
  128. ```
  129. ## repeaterVal
  130. Get a structured object of repeater values, without submitting the form.
  131. The rewritten name attributes of the form `group[index][name]` work well
  132. when submitting to a server that knows how to parse this format, but not as well
  133. when trying to grab the values via javascript.
  134. The `repeaterVal` method can be called on a repeater group and will parse the
  135. renamed attributes into something more easily digestible
  136. ```javascript
  137. // setup the repeater
  138. $('.repeater').repeater();
  139. //get the values of the inputs as a formatted object
  140. $('.repeater').repeaterVal();
  141. ```
  142. ## setList
  143. You can set repeater list data after it has been initialized.
  144. ```javascript
  145. var $repeater = $('.repeater').repeater();
  146. $repeater.setList([
  147. {
  148. 'text-input': 'set-a',
  149. 'inner-group': [{ 'inner-text-input': 'set-b' }]
  150. },
  151. { 'text-input': 'set-foo' }
  152. ]);
  153. ```