Project use springmvc4.x spring4.x hibernate4.x easyui
To facilitate development and extend the DataGrid function of easyui, the following extension code is directly attached:
Specific implementation project visible: https://git.oschina.net/alexgaoyh/alexgaoyh.git
/*** Context is specified as the project context * index if multiple groups of DataGrid are defined, index is specified as the corresponding parameter: A group of DataGrid inclusion (DataGrid; toorbar; dialog; button) * templateurl is specified as the controller path for this access operation * crud is specified as the corresponding toorbar. Crud: add, modify, and delete; C: Add; U: Modify; D: Delete; */function datagrideasyui (context, index, templateurl, crud) {This. context = context; this. index = index; this. templateurl = templateurl; this. crud = crud; // 'C', 'R', 'U', 'D', 'all' this. saving = false; // Processing}; $. extend (maid. prototype, {/*** added: Add; Modify: Edit; Delete: Remove; */init: function () {This. dataGrid = $ ("# DG-" + this. index); this. toolbar = $ ("# toolbar-" + this. index); this. DLG = $ ("# DLG-" + this. index); this. dlgbtn = $ ("# DLG-buttons-" + this. index); var FNS = [this. proxy (this. add, this, this. toolbar. find (". add "), this. proxy (this. edit, this, this. toolbar. f Ind (". edit "), this. proxy (this. remove, this, this. toolbar. find (". remove ")]; // toolbar response function if (this. crud = 'crud ') {This. toolbar. find (". add "). BIND ('click', FNS [0]); this. toolbar. find (". edit "). BIND ('click', FNS [1]); this. toolbar. find (". remove "). BIND ('click', FNS [2]);} else if (this. crud = 'C') {This. toolbar. find (". add "). BIND ('click', FNS [0]);} else if (this. crud = 'U') {This. toolbar. find (". edit "). bi Nd ('click', FNS [1]);} else if (this. crud = 'D') {This. toolbar. find (". remove "). BIND ('click', FNS [2]);} // DLG-buttons response function if (this. crud = 'crud '| this. crud = 'C' | this. crud = 'U') {This. dlgbtn. find ('. save '). BIND ('click', this. proxy (this. save, this, this. dlgbtn. find ('. save '); this. dlgbtn. find ('. cancel '). BIND ('click', this. proxy (this. cancel, this, this. dlgbtn. find ('. cancel ') ;}},/*** change function scope * * @ Param FN * @ Param scope * @ returns */Proxy: function (FN, scope, BTN) {return function () {If (BTN. is ("[class * = 'Disabled ']") {// disabled, do not need to respond to event return;} return fn. call (scope, arguments [0], BTN) ;}},/*** initialize the Form in the dialog */forminit: function (){}, /*** Add button for toorbar */Add: function () {$ ('# DLG-' + this. index ). dialog ('open '). dialog ('settitle', 'add'); this. resetform (this. DLG. find ("form: eq (0)"); this. form Init. call (this) ;},/*** toorbar modify button */edit: function () {var rows = This. dataGrid. dataGrid ('getselections'); If (! Rows | rows. Length = 0) {$. Messager. Alert ('hprompt ', 'select record! ');} Else {If (rows. length = 1) {This. DLG. dialog ('open '). dialog ('settitle', 'modify'); this. DLG. find ("form "). form ('clear '). form ('load', rows [0]); this. formloaddata (rows [0]);} else {$. messager. alert ('hs', 'select a single row record! ') ;}},/*** Toorbar deletion button */remove: function () {var this _ = this; vaR rows = $ ('# DG-' + this. index ). dataGrid ('getselections'); If (! Rows | rows. length> 0) {$. Messager. Confirm ('confirmed', 'Are you sure you want to delete the selected record? ', Function (r) {If (r) {$. post (this _. getcontroller ("logicdelete"), {PIDs: $. map (rows, function (ROW) {return row. PID ;}). join (":")}, function (result) {If (result. success) {This _. reload. call (this _); $. messager. show ({// show // tipstitle: 'hprompt ', MSG: result. MSG});} else {$. messager. alert ('error', result. MSG) ;}}, 'json') ;}}) ;}else {$. messager. alert ('hs', 'select record! ') ;},/*** Resets the Form in the dialog */resetform: function (form) {var form =$ (form); form [0]. reset (); form. find ("[type = hidden]"). val ("") ;},/*** form to load data */formloaddata: function (data) {// process the hidden domain this. DLG. find ("form: eq (0) input [type! = Radio] [type! = Checkbox] [name * = '. '] "). each (function () {var name = $ (this) [0]. name; VaR value = data [name]; If (value) {$ (this ). val (value); return;} If (name. indexof (". ")! =-1) {var names = Name. split (". "); value = data; for (VAR I = 0, L = names. length; I <L; I ++) {value = value [Names [I]; If (! Value) {return ;}$ (this ). val (value) ;}}); // process multiple choice select this. DLG. find ("form: eq (0) input [type = radio]"). each (function () {var name = $ (this) [0]. name; VaR value = data [name]; If (value) {if ($ (this ). val () = value) {$ (this) [0]. checked = "checked";} return;} If (name. indexof (". ")! =-1) {var names = Name. split (". "); value = data; for (VAR I = 0, L = names. length; I <L; I ++) {try {value = value [Names [I];} catch (e) {return ;}}} else {value = data [name];} if ($ (this ). val () = value) {$ (this) [0]. checked = "checked" ;}}); // process multiple choices. Select this. DLG. find ("form: eq (0) input [type = checkbox]"). each (function () {var name = $ (this) [0]. name; VaR value = data [name]; var this _ = this; if (value) {$ (value ). each (function (index, ITEM) {if ($ (this _). val () = item) {$ (this _) [0]. checked = "checked" ;}}); Return ;}if (name. indexof (". ")! =-1) {var names = Name. split (". "); value = data; for (VAR I = 0, L = names. length-1; I <L; I ++) {try {value = value [Names [I];} catch (e) {return ;}} if ($. isarray (value) {for (VAR I = 0, L = value. length; I <L; I ++) {If (value [I] [Names [names. length-1] = $ (this ). val () {$ (this) [0]. checked = "checked"; return ;}}} else {value = data [name];} if ($ (this ). val () = value) {$ (this) [0]. checked = "checked" ;}}); this. DLG. find ("form: eq (0) Select "). each (function () {var name = $ (this) [0]. name; VaR value = data [name]; If (value) {$ (this ). val (value); return;} If (name. indexof (". ")! =-1) {var names = Name. split (". "); value = data; for (VAR I = 0, L = names. length; I <L; I ++) {value = value [Names [I]; If (! Value) {return ;}} else {value = data [name] ;}$ (this ). val (value) ;}) ;}, reload: function () {This. dataGrid. dataGrid ('reload'); // reload},/*** Form Verification */validateform: function (form) {return true ;}, /*** DLG-buttons Save button */save: function () {If (this. saving = true) {// avoid repeated submission of return;} var this _ = This; var form = This. DLG. find ('form: eq (0) '); var URL; If (Form [0]. PID. value) {url = This. getcontroller ("doupdate ");} Else {url = This. getcontroller ("dosave");} form. form ('submit ', {URL: context _ + "/" + URL, onsubmit: function () {var validate = $ (this ). form ('validate') & this _. validateform (form); If (validate) {This _. saving = true;} return validate;}, success: function (result) {This _. saving = false; var result; try {result = jquery. parsejson (result)} catch (e) {$. messager. alert ('error', "server error! "); // Show errorreturn;} If (result. success) {This _. DLG. dialog ('close'); this _. reload. call (this _); $. messager. show ({// show tipstitle: 'hprompt ', MSG: result. MSG});} else {$. messager. alert ('error', result. MSG); // Show ERROR }}, onloaderror: function () {This _. saving = false});},/*** DLG-buttons cancel button */cancel: function () {This. DLG. dialog ('close');},/*** Get Response Method */getcontroller: function (method) {return this. templateurl + "/" + method ;}})
The following list. jsp page shows how to use this plug-in:
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %> <% string context = request. getcontextpath (); pagecontext. setattribute ("context _", context); %> <% @ taglib prefix = "C" uri = "http://java.sun.com/jstl/core_rt" %> <! Doctype HTML>
Create an object on the page and call the init method;
var dg1 = new DataGridEasyui(context_, 1 , templateUrl, ‘crud‘);dg1.init();
The list page corresponds to the DataGrid + id = "dg-1" toolbar for id = "toolbar-1"
Add/modify the corresponding element as a button for the dialog + id = "dlg-1" id = "dlg-buttons-1"
As shown above, the specific code is visible git: https://git.oschina.net/alexgaoyh/alexgaoyh.git
Easyui DataGrid plunges Extension