|
1 | 1 | /* |
2 | | - * AddRemoveTextbox v1.0 |
| 2 | + * AddRemoveTextbox v1.1 |
3 | 3 | * https://www.github.com/kloverde/jquery-AddRemoveTextbox |
4 | 4 | * |
5 | | - * This software is licensed under the 3-clause BSD license. |
| 5 | + * Donations: https://paypal.me/KurtisLoVerde/5 |
6 | 6 | * |
7 | | - * Copyright (c) 2016 Kurtis LoVerde |
8 | | - * All rights reserved |
| 7 | + * Copyright (c) 2016, Kurtis LoVerde |
| 8 | + * All rights reserved. |
9 | 9 | * |
10 | | - * Donations: https://paypal.me/KurtisLoVerde/5 |
| 10 | + * Redistribution and use in source and binary forms, with or without |
| 11 | + * modification, are permitted provided that the following conditions are met: |
| 12 | + * |
| 13 | + * 1. Redistributions of source code must retain the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer. |
| 15 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | + * notice, this list of conditions and the following disclaimer in the |
| 17 | + * documentation and/or other materials provided with the distribution. |
| 18 | + * 3. Neither the name of the copyright holder nor the names of its |
| 19 | + * contributors may be used to endorse or promote products derived from |
| 20 | + * this software without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 23 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 24 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 26 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 28 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 30 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
11 | 32 | */ |
12 | 33 |
|
| 34 | + |
13 | 35 | (function( $ ) { |
14 | 36 | "use strict"; |
15 | 37 |
|
|
32 | 54 |
|
33 | 55 | // A limit on the number of fields which may exist under the applicable ID prefix. Default |
34 | 56 | // is null (no limit). If a value is specified, it must be an integer greater than 1. |
35 | | - maxFields : null |
| 57 | + maxFields : null, |
| 58 | + |
| 59 | + // If true, renumber the id and name attributes upon initialization and when a row is removed. |
| 60 | + // Renumbering is based on DOM order, not `id` or `name` values, and starts on the value |
| 61 | + // specified by `startingNumber`. This setting is disabled by default because it can break applications. |
| 62 | + contiguous : false, |
| 63 | + |
| 64 | + // This setting is used only when 'contiguous' is set to true. |
| 65 | + startingNumber : 0 |
36 | 66 | }, options ); |
37 | 67 |
|
38 | 68 | var ID_PREFIX = this.attr( "id" ).replace( /\[?[0-9]*\]?$/, "" ); |
|
59 | 89 | } |
60 | 90 | } |
61 | 91 |
|
| 92 | + if( settings.contiguous === true ) { |
| 93 | + if( settings.startingNumber != null ) { |
| 94 | + var errMsg = "startingNumber (" + settings.startingNumber + ") must be an integer greater than or equal to 0"; |
| 95 | + |
| 96 | + if( isNaN(settings.startingNumber) ) { |
| 97 | + throwException( errMsg ); |
| 98 | + } else { |
| 99 | + settings.startingNumber = parseInt( settings.startingNumber ); |
| 100 | + |
| 101 | + if( settings.startingNumber < 0 || settings.startingNumber % 1 !== 0 ) { |
| 102 | + throwException( errMsg ); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
62 | 108 | init( this ); |
63 | 109 |
|
64 | 110 | function throwException( msg ) { |
|
83 | 129 | makeAddButton().appendTo( parentRow ); |
84 | 130 | } |
85 | 131 | } ); |
| 132 | + |
| 133 | + rebuild(); |
86 | 134 | } |
87 | 135 |
|
88 | 136 | function makeButton( className, title ) { |
|
214 | 262 | } |
215 | 263 |
|
216 | 264 | rowToRemove.remove(); |
| 265 | + rebuild(); |
217 | 266 | } else { |
218 | 267 | $( escape("#" + inputId) ).val( "" ); |
219 | 268 | } |
220 | 269 | } |
221 | 270 |
|
| 271 | + function rebuild() { |
| 272 | + if( settings.contiguous === true ) { |
| 273 | + var currNum = settings.startingNumber; |
| 274 | + |
| 275 | + $( "input[id^=" + ID_PREFIX + "]" ).each( function() { |
| 276 | + var field = $(this); |
| 277 | + var newId = IS_ARRAY_NOTATION ? ID_PREFIX + "[" + currNum + "]" |
| 278 | + : ID_PREFIX + currNum; |
| 279 | + field.attr( "id", newId ); |
| 280 | + field.attr( "name", newId ); |
| 281 | + |
| 282 | + currNum++; |
| 283 | + } ); |
| 284 | + } |
| 285 | + } |
| 286 | + |
222 | 287 | function escape( selector ) { |
223 | 288 | return selector.replace( /(\[|\])/g, "\\$1" ); |
224 | 289 | } |
|
0 commit comments