Easy Grid Table Control simple usage

Source: Internet
Author: User
Tags constructor join tagname
The code is as follows Copy Code

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 chosen more than one data!",


Noselecttip: "You do not choose the data to be manipulated!" ",


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 | | The 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.exec (TMP);


while (M!= null) {


TMP = Tmp.replace (m[0], it[m[1]][m[2]);


m = optn.exec (TMP);


}


Easy.each (it, function (V, k) {


v = 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.EXEC (TMP);


while (match!= null) {


var fn = match[1];


var rel = (new Function (FN));


TMP = Tmp.replace (match[0], rel);

Match = PTN.EXEC (TMP);
}
}

TMP = Tmp.replace ("{rowIndex}", Cutindex). Replace (/{w*}/ig, "& #160;");


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: "Span",


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 () = "span") {


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;


}


}


});

The code is as follows Copy Code

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 ' ><span> name </span></td>",


"<td style= ' width:200px; ' sort= ' Forgid ' ><span> affiliated organization </span></td>",


"<td style= ' width:150px; ' sort= ' fsort ' ><span> sort </span></td>",


"<td><span> Description </span></td>",


"</tr>"],


TPL: ["<tr>",


"<td><input type= ' checkbox ' value= ' {FID} '/></td>",


"<td><span>{FName}</span></td>",


"<td><span>{OrgName}</span></td>",


"<td><span style= ' width:100px; ' >{FSort}</span></td> ",


"<td><span>{FRemark}</span></td>",


"</tr>"]


});

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.