Easy Grid simple and Easy-to-use javascript table plug-in

Source: Internet
Author: User

In daily webpage design, tables are one of the most commonly used html controls. For convenience and beauty, Easy appears. grid, this table is Easy to use, and the symbol is Easy.

Users only need to use html and Javascript, and read the Demo of Easy. Grid, and use it freely. If you are a programmer, read the source code (the common method is encapsulated in the Easy core library ):

Copy to ClipboardReference: [www.bkjia.com] Easy. data. Store = function (cfg ){
Var me = this;
Easy. apply (me, cfg );
Me. callback = function (){};
Var pubs = {
Start: 0,
Load: function (params ){
Var pms = Easy. applyIf (params | |{}, me. baseParams );
Me. start = pms. start;
Var mk = new Easy. UI. Mask ({
Message: 'loading data ...'
});
Mk. show ();
Me. loader = Easy. Ajax. post ({
Url: me. url,
Params: pms,
Callback: function (rsp, cfg ){
Try {
If (this. status = 200 ){
Me. callback ();
Var json = Easy. decode (rsp );
If (json. exception ){
Alert (json. message );
Return false;
}
Me. processResponse. call (me, rsp );
} Else {
Me. exception. call (me, rsp, this. statue );
}
} Catch (e ){
Mk. close ();
} Finally {
Mk. close ();
}
}
});
},
On: function (en, fn, scope ){
Var me = this;
(Scope | me) ["on" + Easy. string. toPascal (en)] = Easy. delegate (fn, scope | me, Easy. slice (arguments, 3 ));
},
FireEvent: function (eventName, scope ){
Return (scope ["on" + Easy. String. toPascal (eventName)] | Easy. emptyFn). apply (scope, Easy. slice (arguments, 2 ));
},
ProcessResponse: function (resText, option ){
Var o = Easy. decode (resText );
If (o &&! O. exception)
Me. drawRow (o );
Else {
Me. exception. call (resText, me. loader. statue );
}
Me. fireEvent ('load', me );
},
Exception: function (resT, state ){

Me. fireEvent ('exception', me );
}
};

Easy. apply (me, pubs );
Return me;
}

Easy. UI. Grid = Easy. extend (Easy. UI. Base ,{
Url: "/base. ashx ",
Root: 'records ',
Count: "Count ",
Tpl :"",
NoPaging: false,
MutilSelect: false,
Model: 'remote ',
Rows: [],
Checkboxs: [],
HeaderTDs: [],
MutilSelectTip: "You have selected multiple data records! ",
NoSelectTip: "You have not selected the data to be operated! ",
Constructor: function (cfg ){
Var me = this;
Easy. apply (me, cfg );
If (! Me. store & me. model = "remote "){
If (me. url ){
Me. store = new Easy. data. Store ({
Url: me. url,
BaseParams: Easy. apply ({
ClassName: me. className,
MethodName: me. methodName | "PageData ",
Start: 0, limit: me. limit | 17, sort: '', dir: 'desc'
}, Me. baseParams)
});
}
}
If (me. store)
Me. store. drawRow = Easy. delegate (me. drawRow, me );
Easy. UI. Grid. superclass. constructor. call (this );
},
InitComponent: function (){
Var me = this;
Var tpl = "<table class = 'e-grid' border = '0' cellpadding = '0' cellspacing = '0'>" +
"<Tbody>" +
(Easy. isArray (me. header )? Me. header. join ("\ n"): me. header) +
"</Tbody>" +
"</Table> <div class = 'clear'> </div> ";
Var wrap = Easy. get (me. renderTo );
Wrap. innerHTML = tpl;
If (wrap. firstChild & Easy. isElement (wrap. firstChild) & wrap. firstChild. tagName = "TABLE ")
Me. table = wrap. firstChild;
If (me. model = 'remote ')
Me. store. load ();
Me. initGrid = true;
Me. wrap = wrap;
Me. headerTDs = [];
Me. clearDiv = me. table. nextSibling;
Easy. each (me. table. firstChild. rows [0]. cells, function (td, I ){
Var w = Easy. DOM. getStyle (td, "width ");
If (Easy. DOM. hasClass (td, "e-row "))
W = 30;
W = w | (td. offsetWidth-10 );
Me. headerTDs. push (w );
})
},
DrawRow: function (rows ){
Var me = this, wrap = Easy. get (me. renderTo );
Me. data = {};
Me. getCount = function (){
Return rows [me. Count];
};
Me. tplStr = Easy. isArray (me. tpl )? Me. tpl. join ("\ n"): me. tpl;
Wrap. innerHTML = "";
Me. rows = [];
Me. checkboxs = [];
Var tpl = "<table class = 'e-grid' border = '0' cellpadding = '0' cellspacing = '0'>" +
"<Tbody>" +
(Easy. isArray (me. header )? Me. header. join ("\ n"): me. header) +
(Function (){
Var sb = [], tmp, cutIndex = 0, orow = me. root? Rows [me. root]: rows;
Easy. each (orow, function (it ){
Me. data [it ["FID"] = it;
Tmp = me. tplStr;

Var optn = // {(\ w +) \} [.] (\ w +)/I, m = optn.exe c (tmp );
While (m! = Null ){
Tmp = tmp. replace (m [0], it [m [1] [m [2]);
M = optn.exe c (tmp );
}
Easy. each (it, function (v, k ){
V = null? "": V;
If (Easy. isDate (v )){
V = Easy. Date. format (v, "yyyy-MM-dd hh: mm ");
}
Tmp = tmp. replace (new RegExp ("{" + k + "}", "g"), v );
});
CutIndex + = 1 + (me. model = 'remote '? Me. store. baseParams. start: 0 );
If (tmp. indexOf ("{fn:")>-1 ){
Var ptn = // {fn :(. *) \/fn \}/I, match = ptn.exe c (tmp );
While (match! = Null ){
Var fn = match [1];
Var rel = (new Function (fn ));
Tmp = tmp. replace (match [0], rel );

Match = ptn.exe c (tmp );
}
}

Tmp = tmp. replace ("{rowIndex}", cutIndex). replace (/\ {\ w * \}/ig ,"");
Sb. push (tmp );
});
Return sb. join ("");
}) () +
"</Tbody>" +
"</Table> <div class = 'clear'> </div> ";
Wrap. innerHTML = tpl;
If (wrap. firstChild & Easy. isElement (wrap. firstChild) & wrap. firstChild. tagName = "TABLE ")
Me. table = wrap. firstChild;
Me. initEvent. call (me );
If (! Me. noPaging ){
Var p = new Easy. UI. Pagingbar (Easy. apply (me. pagingbar | | {},{
RenderTo: wrap,
Data: rows,
PageSize: me. store. baseParams. limit,
Start: me. store. baseParams. start,
Store: me. store,
Width: wrap. scrollWidth | wrap. offsetWidth | "auto"
}));
P. currentPageCtl. value = Math. ceil (me. store. start/me. store. baseParams. limit + 1 );
}
Me. clearDiv = me. table. nextSibling;
Me. wrap = wrap;
Me. fireEvent ("render", me );
},
On: function (en, fn, scope ){
Var me = this;
If (en = "rowdblclick "){
Easy. each (me. rows, function (r, I ){
Easy. DOM. on (r, "dblclick", Easy. delegate (fn, scope | me, [r, I]);
})
}
(Scope | me) ["on" + Easy. string. toPascal (en)] = Easy. delegate (fn, scope | me, Easy. slice (arguments, 3 ));
},
OnRender: function (){
Var me = this, style = {};
If (me. height ){
Style. height = Math. max (me. height-me. table. offsetHeight-75, 0 );
}
Easy. DOM. setStyle (me. clearDiv, style );
},
InitEvent: function (){
Var me = this, tb = me. table. firstChild;
Var ths = tb. rows [0]. cells, rows = tb. childNodes;
Me. checkboxs = [];
For (var I = 0, len = ths. length; I <len; I ++ ){
Var cel = ths [I];
If (cel & cel. sort ){
Easy. DOM. on (cel, "click", Easy. delegate (me. sort, me, [cel. sort, cel]);
If (me. initGrid ){
Var sortFlag = Easy. DOM. create ({
Tag: "font ",
Cls: 'e-sort-asc'
}, Cel );
Easy. DOM. addClass (cel, "e-cansort ");
}
}
}
Var ck = rows [0]. getElementsByTagName ("input") [0];
If (ck & ck. type. toLowerCase () = "checkbox "){
// Me. checkboxs. push (ck );
Easy. DOM. on (ck, "click", Easy. delegate (me. selectAll, me, [ck]);
}
For (var I = 1, len = rows. length; I <len; I ++ ){
Var r = rows [I];
Me. rows. push (r );
Var ckb = r. getElementsByTagName ("input") [0];
If (ckb & ckb. type. toLowerCase () = "checkbox "){
Me. checkboxs. push (ckb)
Easy. DOM. on (ckb, "click", Easy. delegate (me. checkClick, me, [ckb, r]);
}
Easy. DOM. on (r, "mouseover", Easy. delegate (me. toogleClass, me, [r, "e-row-over"]);
Easy. DOM. on (r, "mouseout", Easy. delegate (me. toogleClass, me, [r, "e-row-over"]);
Easy. DOM. on (r, "click", Easy. delegate (me. rowClick, me, [r, I]);
Easy. DOM. on (r, "dblclick", Easy. delegate (me. rowDblClick, me, [r, I]);

Easy. each (r. cells, function (c, colIndex ){
Var sp = c. firstChild;
If (sp & sp. tagName. toLowerCase () = "font "){
Easy. DOM. setStyle (sp ,{
Width: me. headerTDs [colIndex] | "auto"
});
}
Easy. DOM. on (c, "click", Easy. delegate (me. cellClick, me, [c, r, colIndex, I]);
Easy. DOM. on (c, "dblclick", Easy. delegate (me. cellDblClick, me, [c, r, colIndex, I]);
});
}
},
Load: function (params ){
This. store. load (params );
},
Refresh: function (p ){
This. store. load (p );
},
Sort: function (sort, cel ){
Var me = this;
Me. initGrid = false;
Easy. DOM. toogleClass (cel. lastChild, "e-sort-desc ");
Var params = {sort: sort, dir: "desc "};
If (Easy. DOM. hasClass (cel. lastChild, "e-sort-desc ")){
Params ["dir"] = "desc ";
} Else {
Params ["dir"] = "asc ";
}
Me. header = me. table. firstChild. firstChild. outerHTML;
Me. load (params );
},
SelectAll: function (ck ){
Var me = this;
Easy. each (me. checkboxs, function (c, I ){
C. checked = ck. checked;
Easy. DOM [ck. checked? "AddClass": "removeClass"] (me. rows [I], "e-row-select ");
});
},
CheckClick: function (c, r ){
Easy. DOM. toogleClass (r, "e-row-select ");
Easy. Event. stopPropagation (window. event );
This. fireEvent ('check', this, r, c );
},
RowClick: function (r, I, cked ){
If (! Window. event. ctrlKey &&! This. mutilSelect)
Easy. each (this. rows, function (r ){
Easy. DOM. removeClass (r, "e-row-select ");
R. cells (0). firstChild. checked = false;
});
Easy. DOM. addClass (r, "e-row-select ");
This. rows [I-1]. cells (0). firstChild. checked = cked! = False? True: cked;
This. fireEvent ('click', this, r, I, cked );
},
RowDblClick: function (row, idex ){
This. fireEvent ('rowdblclick', this, row, idex );
},
CellClick: function (cell, row, colIndex, rowIndex ){
This. fireEvent ('cellclick', this, cell, row, colIndex, rowIndex );
},
CellDblClick: function (cell, row, colIndex, rowIndex ){
This. fireEvent ('celldblclick', this, cell, row, colIndex, rowIndex );
},
ToogleClass: function (r, cls ){
Easy. DOM. toogleClass (r, cls );
},
GetSelection: function (){
Var me = this, sel = [];
Easy. each (me. rows, function (r ){
Var ck = r. cells (0). firstChild;
If (ck. checked === true ){
Sel. push (me. data [ck. value]);
}
});
Return sel;
},
GetValues: function (key, canempty ){
Var me = this, v = [];
Var sls = me. getSelection. call (me );
Easy. each (sls, function (s ){
V. push (s [key]);
});
If (v. length = 0 &&! Canempty ){
Alert (me. noSelectTip );
V = false;
}
Return v;
},
GetValue: function (key, canempty ){
Var me = this, v = [];
Var sls = me. getSelection. call (me );
If (sls. length = 1 ){
Return sls [0] [key];
} Else if (! Canempty ){
Alert (me [sls. length = 0? "NoSelectTip": "mutilSelectTip"]);
Return false;
}
}
});

After reading the long source code, let's see what the grid can do. Now let's look at the effect,

As follows:

After seeing the effect, let's look at the code for wearing the grid:

Copy to ClipboardReference: [www.bkjia.com] var grid = new Easy. UI. Grid ({
Id: "roleGrid ",
RenderTo: 'grid ',
BaseParams :{
ClassName: "RoleServer ",
MethodName: "PageData"
},
Header: ["<tr class = 'e-header'> ",
"<Td class = 'e-checkbox'> <input type = 'checkbox'/> </td> ",
"<Td style = 'width: 150px; 'sort = 'fname'> <font> name </font> </td> ",
"<Td style = 'width: 200px; 'sort = 'forgid'> <font> Organization </font> </td> ",
"<Td style = 'width: 150px; 'sort = 'fsort '> <font> sort </font> </td> ",
"<Td> <font> description </font> </td> ",
"</Tr>"],
Tpl: ["<tr> ",
"<Td> <input type = 'checkbox' value = '{FID}'/> </td> ",
"<Td> <font> {FName} </font> </td> ",
"<Td> <font> {OrgName} </font> </td> ",
"<Td> <font style = 'width: 100px; '> {FSort} </font> </td> ",
"<Td> <font> {FRemark} </font> </td> ",
"</Tr>"]
});

The code for wearing the grid is nothing more than that. The effect is okay!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.