How Do I? Create a jquery plug-in : creating a jQuery plug-in that can convert JSON array into table
Download Plug-in with Samples
Download Plug-in with Asp.net Ajax Sample
This article is about learning how to create jQuery plug-in by actually creating one. I have prepared a sample plug-in that can convert a JSON array into a table. This table is a template table already present in the page and this plug-in will get this table and insert the values of variables in the desired places whether they are in table attributes or inside td’s.
Creating a plug-in is not a big task but creating a reusable plug-in needs deep understanding and better decisions at design time so it can be real reusable component later. If you want to simple know the syntax of creating a plug-in in jQuery, jQuery website has all the information and you can check it from this link
jQuery Plug-in Authoring
However this article is about creating jQuery plug-in, it’s about doing better decision while creating a plug-in. Here are some considerations
1- Plug-in should work even without binding it to jQuery
2- Plug-in should be able to maintain its state(data) for event handling
3- Plug-in should be able to attach to as much elements as we want in a page
(That means it should be object orient rather than procedural)
I am assuming that before reading this article you arefamiliar with javascript prototype programming. But if you are not, please read this article before proceedint "Object Oriented Programming in JavaScript"
In this article I will discuss in detail about all of these aspects of a plug-in. It’s a very simple plug-in and let start building it and while on it, I will discuss the above listed points. Following is the main constructor and possible listed options of table plug-in
- function Table(tbl, data)
- {
- //data for head and body
- this.Body = data.Body;
- // table object
- this.RTable = tbl;
- // template for head and body and footer
- this.BodyTemplate;
- // call back function to be called on row data bound
- this.OnRow_Bounded=data.OnRow_Bounded;
- this.GatherTemplate();
- if (this.Body != null)
- {
- this.Create();
- }
- }
This is the function that will gather the template from header/footer and body from the passed table object and this template will be used to bind data
- Table.prototype.GatherTemplate = function() {
- this.BodyTemplate = $("tbody", this.RTable).html();
- $("tbody tr:first", this.RTable).remove();
- }
After getting all the templates, this will save the required templates that will help in building table.
After gathering template, this next function will build the table and display it on the screen
- Table.prototype.Create = function()
- {
- var i = this.CurrentPage == 0 ? 0 : this.CurrentPage * this.PageSize;
- var limit = i + this.PageSize > this.Body.length ? this.Body.length : i + this.PageSize;
- for (; i < limit; i++)
- {
- this.RTable.append(this.CreateRow(this.BodyTemplate, this.Body[i]));
- }
- }
- Table.prototype.CreateRow = function(template, rowData)
- {
- for (var i in rowData)
- {
- var exp = new RegExp("{" + i + "}", "g");
- template = template.replace(exp, rowData[i]);
- }
- var row = $(template);
- if (typeof OnRow_Bounded == 'function')
- {
- row = OnRow_Bounded(rowData, row, "body");
- }
- return row;
- }
- Var table=new Table($(“table:first”),{Body:yourJSONData});
- $.fn.CreateTable = function(data)
- {
- //if (this.length > 0) {
- $.extend(options, data);
- var index = 0;
- var objArray = new Array();
- this.each(function()
- {
- objArray[index] = new Table($(this), options);
- index++;
- });
- return objArray;
- //}
- }
- $(“table”).CreateTable({options});
No comments:
Post a Comment