forked from DubFriend/jquery.repeater
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brian Detering
authored and
Brian Detering
committed
Oct 9, 2016
1 parent
9259896
commit 7dc4fea
Showing
9 changed files
with
156 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// jquery.repeater version 1.2.0 | ||
// jquery.repeater version 1.2.1 | ||
// https://github.com/DubFriend/jquery.repeater | ||
// (MIT) 08-10-2016 | ||
// (MIT) 09-10-2016 | ||
// Brian Detering <[email protected]> (http://www.briandetering.net/) | ||
(function ($) { | ||
'use strict'; | ||
|
@@ -797,6 +797,8 @@ $.fn.repeaterVal = function () { | |
$.fn.repeater = function (fig) { | ||
fig = fig || {}; | ||
|
||
var setList; | ||
|
||
$(this).each(function () { | ||
|
||
var $self = $(this); | ||
|
@@ -915,58 +917,87 @@ $.fn.repeater = function (fig) { | |
}); | ||
} | ||
|
||
|
||
|
||
var appendItem = (function () { | ||
var setItemsValues = function ($item, values, repeaters) { | ||
if(values) { | ||
var setItemsValues = function ($item, data, repeaters) { | ||
if(data || fig.defaultValues) { | ||
var inputNames = {}; | ||
$filterNested($item.find('[name]'), repeaters).each(function () { | ||
var key = $(this).attr('name').match(/\[([^\]]*)(\]|\]\[\])$/)[1]; | ||
inputNames[key] = $(this).attr('name'); | ||
}); | ||
|
||
$item.inputVal(map(values, identity, function (name) { | ||
return inputNames[name]; | ||
})); | ||
$item.inputVal(map( | ||
filter(data || fig.defaultValues, function (val, name) { | ||
return inputNames[name]; | ||
}), | ||
identity, | ||
function (name) { | ||
return inputNames[name]; | ||
} | ||
)); | ||
} | ||
|
||
|
||
$foreachRepeaterInItem(repeaters, $item, function (nestedFig) { | ||
var $repeater = $(this); | ||
$filterNested( | ||
$repeater.find('[data-repeater-item]'), | ||
nestedFig.repeaters | ||
) | ||
.each(function () { | ||
setItemsValues( | ||
$(this), | ||
nestedFig.defaultValues, | ||
nestedFig.repeaters || [] | ||
); | ||
var fieldName = $repeater.find('[data-repeater-list]').data('repeater-list'); | ||
if(data && data[fieldName]) { | ||
var $template = $(this).clone(); | ||
$repeater.find('[data-repeater-item]').remove(); | ||
foreach(data[fieldName], function (data) { | ||
var $item = $template.clone(); | ||
setItemsValues( | ||
$item, | ||
data, | ||
nestedFig.repeaters || [] | ||
); | ||
$repeater.find('[data-repeater-list]').append($item); | ||
}); | ||
} | ||
else { | ||
setItemsValues( | ||
$(this), | ||
nestedFig.defaultValues, | ||
nestedFig.repeaters || [] | ||
); | ||
} | ||
}); | ||
}); | ||
|
||
}; | ||
|
||
return function ($item) { | ||
return function ($item, data) { | ||
$list.append($item); | ||
setIndexes($items(), getGroupName(), fig.repeaters); | ||
$item.find('[name]').each(function () { | ||
$(this).inputClear(); | ||
}); | ||
setItemsValues($item, fig.defaultValues, fig.repeaters); | ||
setItemsValues($item, data || fig.defaultValues, fig.repeaters); | ||
}; | ||
}()); | ||
|
||
var addItem = function () { | ||
var addItem = function (data) { | ||
var $item = $itemTemplate.clone(); | ||
appendItem($item); | ||
appendItem($item, data); | ||
if(fig.repeaters) { | ||
initNested($item); | ||
} | ||
show.call($item.get(0)); | ||
}; | ||
|
||
$filterNested($self.find('[data-repeater-create]'), fig.repeaters).click(addItem); | ||
setList = function (rows) { | ||
$items().remove(); | ||
foreach(rows, addItem); | ||
}; | ||
|
||
$filterNested($self.find('[data-repeater-create]'), fig.repeaters).click(function () { | ||
addItem(); | ||
}); | ||
|
||
$list.on('click', '[data-repeater-delete]', function () { | ||
var self = $(this).closest('[data-repeater-item]').get(0); | ||
|
@@ -977,6 +1008,8 @@ $.fn.repeater = function (fig) { | |
}); | ||
}); | ||
|
||
this.setList = setList; | ||
|
||
return this; | ||
}; | ||
|
||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters