Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion src/jquery.formset.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Licensed under the New BSD License
* See: http://www.opensource.org/licenses/bsd-license.php
*/

;(function($) {
$.fn.formset = function(opts)
{
Expand Down Expand Up @@ -211,6 +212,43 @@
});
}

// adding multiple rows bindings
if (options.addMultRows) {
var addMultRowsButton = options.addMultRows.button;
var addMultRowsFunction = options.addMultRows.function;
var addMultRowsPostFunction = options.addMultRows.post_function;
var currentDataRow = undefined;

// we got the data & insert a row for every element on it
addMultRowsButton.click(function () {
var url = addMultRowsFunction();
// we got the data of the group agentes
$.get(url).done(function(data) {
data.forEach(function (elem) {
currentDataRow = elem;
addButton.click();
currentDataRow = undefined;
});
if (addMultRowsPostFunction) {
addMultRowsPostFunction(data);
}
});
});

// add row_data to the row to allow customize row values
// decorating 'added' callback
var previousAdded = options.added;
options.added = function (row) {
if (currentDataRow) {
row.attr('data', JSON.stringify(currentDataRow));
}
if (previousAdded) {
// in 'added' callback you can use the data in the row
previousAdded(row);
}
};
}

return $$;
};

Expand All @@ -226,6 +264,13 @@
extraClasses: [], // Additional CSS classes, which will be applied to each form in turn
keepFieldValues: '', // jQuery selector for fields whose values should be kept when the form is cloned
added: null, // Function called each time a new form is added
removed: null // Function called each time a form is deleted
removed: null, // Function called each time a form is deleted
addMultRows: null // Button for add multiple rows programatically and function for assigns data to it
// The function should return an url that can be accesed using GET, and then it use the generated data to add
// to each row. Note that the url returned by the function can be fixed or not, in order to add more flexibility
// obtaining data sources.
// The data returned by the API should be a list of JS objects and also can be passed to another callback option called
// 'post_function' that would be call when data sources has been obtained and new data rows were created.
// For example addMultRows: {'button': $('#mybutton'), 'function': function() {return 'https://restcountries.eu/rest/v2/all'}, 'post_function': function(data) {console.log(data);}}
};
})(jQuery);