Skip to content

Commit

Permalink
closes DubFriend#13 (Retrieve repeater data without form submit)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Detering authored and Brian Detering committed Nov 7, 2015
1 parent 2d3868f commit 3a3e4b5
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,21 @@ Names get reindexed if an item is added or deleted.
});
</script>
```

## repeaterVal

Get a structured object of repeater values, without submitting the form.

The rewritten name attributes of the form `group[index][name]` work well
when submitting to a server that knows how to parse this format, but not as well
when trying to grab the values via javascript.

The `repeaterVal` method can be called on a repeater group and will parse the
renamed attributes into something more easily digestible

```javascript
// setup the repeater
$('.repeater').repeater();
//get the values of the inputs as a formatted object
$('.repeater').repeaterVal();
```
30 changes: 29 additions & 1 deletion jquery.repeater.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// jquery.repeater version 1.1.0
// jquery.repeater version 1.1.2
// https://github.com/DubFriend/jquery.repeater
// (MIT) 07-11-2015
// Brian Detering <[email protected]> (http://www.briandetering.net/)
Expand Down Expand Up @@ -697,6 +697,33 @@ $.fn.inputClear = function () {

}(jQuery));

$.fn.repeaterVal = function () {
var rawData = $(this).inputVal();
var mapped = {};

foreach(rawData, function (val, key) {
var group, index, name;
var matches;
if(key !== "undefined") {
matches = key.match(/^([^\[]+)\[([0-9]+)\]\[([^\]]+)/);
group = matches[1];
index = matches[2];
name = matches[3];
if(!mapped[group]) {
mapped[group] = [];
}

if(!mapped[group][index]) {
mapped[group][index] = {};
}

mapped[group][index][name] = val;
}
});

return mapped;
};

$.fn.repeater = function(fig) {
fig = fig || {};

Expand Down Expand Up @@ -802,6 +829,7 @@ $.fn.repeater = function(fig) {
});
});


});

return this;
Expand Down
4 changes: 2 additions & 2 deletions jquery.repeater.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jquery.repeater",
"version": "1.1.1",
"version": "1.1.2",
"description": "repeatable form input interface",
"main": "jquery.repeater.js",
"directories": {
Expand Down
28 changes: 28 additions & 0 deletions src/repeater.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
$.fn.repeaterVal = function () {
var rawData = $(this).inputVal();
var mapped = {};

foreach(rawData, function (val, key) {
var group, index, name;
var matches;
if(key !== "undefined") {
matches = key.match(/^([^\[]+)\[([0-9]+)\]\[([^\]]+)/);
group = matches[1];
index = matches[2];
name = matches[3];
if(!mapped[group]) {
mapped[group] = [];
}

if(!mapped[group][index]) {
mapped[group][index] = {};
}

mapped[group][index][name] = val;
}
});

return mapped;
};

$.fn.repeater = function(fig) {
fig = fig || {};

Expand Down Expand Up @@ -103,6 +130,7 @@ $.fn.repeater = function(fig) {
});
});


});

return this;
Expand Down
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,27 @@ QUnit.asyncTest('has ready callback option and setIndexes', function (assert) {
}
});
});

QUnit.test('repeaterVal', function (assert) {
this.$repeater.repeater();
assert.deepEqual(this.$repeater.repeaterVal(), {
"group-a": [
{
"text-input": "A",
"textarea-input": "A",
"select-input": "A",
"multiple-select-input": ["A", "B"],
"radio-input": "A",
"checkbox-input": ["A"]
},
{
"text-input": "B",
"textarea-input": "B",
"select-input": "B",
"multiple-select-input": ["A", "B"],
"radio-input": "B",
"checkbox-input": ["B"]
}
]
});
});

0 comments on commit 3a3e4b5

Please sign in to comment.