mazondo / formalizedata Goto Github PK
View Code? Open in Web Editor NEWjQuery plugin for turning form data into a js object which can then be turned into json
License: MIT License
jQuery plugin for turning form data into a js object which can then be turned into json
License: MIT License
Could register functions like this:
$.formalize.fn.toNumber = function(s){ return parseInt(s); }
$.formalize.fn.fromNumber = function(s){ return "" + s; }
Then the input field with data-formalize="number"
could map to toNumber
and fromNumber
, or just number
if it's available.
and users could add their own:
$.formalize.fn.toTimecode = function(s){ return (new TimecodeFromSeconds(s).timecode(); };
$.formalize.fn.fromTimecode = function(s){ return (new Timecode(s)).seconds(); };
name and id are both common ones, and doing that would require us to support something like ["name", "id"]
anyways, so then we could let people pass in attribute: ["data-name", "data-whatever-here"]
I'd like to be able to set a function option that lets me modify each value as it's coming in, or going out:
$("form").formaize({
toData: function(value, options){ return parseInt(value)+1; },
toForm: function(value, options){ return value-1; }
});
Good idea.
I do suggest a small change though, for usability purposes.
Get rid of
if (!name) {
throw "Elements must be named!";
}
Change your .find() to include the attribute
.find("input[name], textarea[name]").each(
I also recommend passing the actual attribute name as a param.
$.fn.formalizeData = function(atName) {
atName = atName || "name";
in which case your .find() would be something like
.find("input[" + atName + "], textarea[" + atName + "]").each(
this way, if they want to use the default they can still call
.formalizeData()
and it would check for the 'name' attribute.
but if being used in a program that already uses a name attribute (as it is a common attribute)
they could say,
.formalizeData('data-name')
and it would look for the data-name attribute.
they could also create different combos of json data from a given form by utilizing different attributes
numbers should be numbers, bools should be bools. May warrant becoming it's own library.
Once I have the data object, I'd like to be able to change it, and push it back to the form.
Maybe this interface:
// get the data
dataObj = $("form").formalize();
// do something with the data
// now set it
$("form").formaize(dataObj);
NOTE: It would have to know how to handle arrays, i.e. copy elements x times etc.
I would like to be able to get data from a range of elements, decided by the selector on the active jQuery object.
Some examples:
var name = $("ul li.name input").formalize();
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.