Example of extjs editorgridpanel: adding, deleting, modifying, and querying data in front and back of store in JSON format

Source: Internet
Author: User

Example of data transmission in JSON format, which is displayed on the HTML page:

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br/> <HTML> <br/> <pead> <br/> <title> using jsonreader to edit user data </title> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = GBK "/> <br/> <meta http-equiv =" author "content =" hoojo "> <br/> <meta http-equiv =" email "content = "hoojo_@126.com"> <br/> <meta http-equiv = "blog" content = "http://blog.csdn.net/IBM_hoojo"> <br/> <LINK rel = "stylesheet" type = "text/CSS "href = ".. /ext2/resources/CSS/ext-all.css "> </link> <br/> <SCRIPT type =" text/JavaScript "src = ".. /ext2/adapter/EXT/ext-base.js "> </SCRIPT> <br/> <SCRIPT type =" text/JavaScript "src = ".. /ext2/ext-all.js "> </SCRIPT> <br/> <SCRIPT type =" text/JavaScript "src = ".. /ext2/build/locale/ext-lang-zh_CN-min.js "> </SCRIPT> <br/> <SCRIPT type =" text/JavaScript "src =" jsonpagingeditorgridpanel. JS "> </SCRIPT> <br/> <SCRIPT type =" text/JavaScript "> <br/> Ext. onready (function () {<br/> Ext. quicktips. init (); <br/> // Ext. form. field. prototype. msgtarget = "side"; </P> <p> Ext. blank_image_url = ".. /ext2/resources/images/default/s.gif "; <br/> New jsonpagingeditorgridpanel (); <br/> }); <br/> </SCRIPT> <br/> </pead> <br/> <body> </P> <p> </body> <br/> </ HTML>

 

Jsonpagingeditorgridpanel. JS:

/** <Br/> * JSON format: ext. data. store, ext. data. jsonreader parser, achieve editorgrid addition, deletion and modification query <br/> * Author: hoojo <br/> * Email: hoojo_@126.com <br/> * blog: http://blog.csdn.net/IBM_hoojo <br/> * ext-Lib: v2.2 <br/> */<br/> /**************************** **********************************/<br/ >/******** jsonpagingeditorgridpanel ********/<br/> /***************** **************************************** * ******/<B R/> jsonpagingeditorgridpanel = ext. extend (ext. grid. editorgridpanel, {<br/> sexcombo: NULL, <br/> inserted: [], <br/> constructor: function () {</P> <p> This. sexcombo = new Ext. form. comboBox ({<br/> mode: "local", <br/> value: "All", <br/> readonly: True, <br/> triggeraction: "All", <br/> displayfield: "sex", <br/> // listalign: "Bl-Tl ", // The drop-down list is displayed at the top, while the TL-BL is displayed at the bottom. <br/> store: New Ext. data. simplest Ore ({<br/> data: ["male", "female", "all"], <br/> expanddata: True, <br/> fields: ["sex"] <br/>}), <br/> listeners :{< br/> "select" :{< br/> FN: This. filtersex, <br/> scope: This <br/>}< br/> }); </P> <p> This ["Store"] = new Ext. data. store ({<br/> URL: jsonpagingeditorgridpanel. user_store_url, <br/> reader: New Ext. data. jsonreader ({<br/> ID: "ID", // maintain the uniqueness of the current data (similar to the primary key of the database to avoid data duplication-filter duplicate data) <br/> root: "users", <br/> Totalproperty: "totals" <br/>}, <br/> Ext. data. record. create (["ID", "name", <br/> {name: "Age", type: "int"}, "sex", <br/> {Name: "Birthday", type: "date", dateformat: "Y-m-d"} <br/>]) <br/>), <br/> sortinfo: {field: "ID", direction: "ASC"} // sort <br/>}); </P> <p> jsonpagingeditorgridpanel. superclass. constructor. call (this, {<br/> renderto: ext. getbody (), <br/> width: 480, <br/> Height: 300, <br/> frame: True, <br />/Loadmask: True, // display the loaded rotating bar <br/> striperows: True, // The row is discolored and the table rows are distinguished. <br/> clickstoedit: 2, // indicates the number of clicks to edit the table <br/> collapsible: True, // whether to display the shrink button in the upper right corner <br/> animcollapse: True, // It indicates that the animation effect is displayed when the Panel is reduced (Closed). <br/> trackmouseover: True, // highlight when the mouse moves on the row <br/> enablecolumnmove: false, // prohibit users from dragging the header <br/> // disableselection: True, <br/> autoexpandcolumn: "name", // the ID of the name specified here, this attribute can be filled with blank areas based on the current column <br/> title: "user data editor", <br/> tbar: [<Br/> "->", "view method:", <br/> This. sexcombo, <br/>{< br/> text: "add data", <br/> handler: This. onaddclick, <br/> scope: This <br/>}, "-", {<br/> text: "delete data", <br/> handler: This. onremoveclick, <br/> scope: This <br/>}, "-", {<br/> text: "save data", <br/> handler: This. oncommitstore, <br/> scope: This <br/>}< br/>], <br/> bbar: New Ext. pagingtoolbar ({<br/> // width: 480, // If pagingtoolbar is not in bbar [] brackets, width is not required; otherwise, the width must be specified Display displayinfo <br/> pagesize: 5, <br/> store: This. store, <br/> displayinfo: True, <br/> displaymsg: "displays {0}-{1} records. A total of {2} records ", <br/> emptymsg: "No records" <br/>}), <br/> columns: [{<br/> ID: "name ", <br/> header: "name", <br/> align: "center", <br/> dataindex: "name", <br/> Editor: New Ext. form. textfield ({<br/> allowblank: false, <br/> blanktext: "The name cannot be blank. You must enter" <br/>}) <br/> }, {<br/> header: "Age", <br/> align: "center", <Br/> dataindex: "Age", <br/> Editor: New Ext. form. numberfield ({<br/> allowblank: false, <br/> allownegative: false, // it can only be a positive number <br/> // maxvalue: 1000000000, <br/> grow: true, // The first half shows the data being changed, and the second half shows the old data. <br/> selectonfocus: True, // when the focus is obtained, select all text content <br/> minvalue: 1 <br/>}) <br/>},{ <br/> header: "gender ", <br/> align: "center", <br/> dataindex: "sex", <br/> Editor: New Ext. form. comboBox ({<br/> mode: "local", <br/> Value: "male", <br/> readonly: True, <br/> displayfield: "sex", <br/> triggeraction: "All ", <br/> store: New Ext. data. simplestore ({<br/> data: ["male", "female"], <br/> expanddata: True, <br/> fields: ["sex"] <br/>}) <br/>},{ <br/> header: "Birthday", <br/> align: "center", <br/> sortable: True, <br/> dataindex: "Birthday", <br/> Renderer: ext. util. format. daterenderer ('Y-m-d'), <br/> Editor: New Ext. form. datefield ({< Br/> Format: "Y-m-d", <br/> minvalue: "1950-01-01", <br/> disableddays: [0, 7], // 0th columns of datefield: Sunday and 7th columns: cannot be edited on Saturday <br/> disableddaystext: "cannot be selected on weekends" <br/>}) <br/>}], <br/> SM: New Ext. grid. rowselectionmodel ({<br/> singleselect: True <br/>}) </P> <p >}); <br/> This. store. load ({Params: {start: 0, limit: 5 }}); <br/> Ext. ajax. on ("requestcomplete", function (Conn, response, options) {<br/> // alert (response. responsetex T); <br/> Ext. example. MSG ('click', 'you clicked on "Action 1 ". '); </P> <p >}); <br/>}, <br/> filtersex: function (COB) {<br/> If (COB. getvalue () = "all") {<br/> This. store. clearfilter (); <br/>}< br/> else <br/> This. store. filter ("sex", COB. value); <br/>}, <br/> onaddclick: function () {<br/> var rs = new Ext. data. record ({ID: "", name: "", age: 1, sex: "", birthday: 0000-00-00}); <br/> This. getstore (). add (RS ); <Br/> Rs. set ("name", "Ext"); <br/> Rs. set ("Age", 22); <br/> Rs. set ("sex", "male"); <br/> Rs. set ("Birthday", new date (); <br/> This. inserted. push (RS); <br/> This. startediting (this. store. getcount ()-1, 0); <br/>}, <br/> saveinsertdata: function (Conn, response) {<br/> var xml = response. responsexml; <br/> var root = xml.doc umentelement; <br/> for (VAR I = 0; I <root. childnodes. length; I ++) {<br/> This. inser Ted [I]. set ("ID", Root. childnodes [I]. text); <br/>}< br/> This. getstore (). commitchanges (); <br/> This. inserted = []; <br/>}, <br/> oncommitstore: function () {<br/> var mf = This. getstore (). modified; <br/> var temp = []; <br/> for (VAR I = 0; I <MF. length; I ++) {<br/> If (MF [I]. get ("ID") = "") {<br/> continue; <br/>}< br/> var data = {}; <br/> for (var j in MF [I]. modified) {<br/> data [J] = mf [I]. get (j); <BR/>}< br/> temp. push (ext. apply (data, {ID: MF [I]. get ("ID")}); <br/>}</P> <p> for (VAR I = 0; I <this. inserted. length; I ++) {<br/> temp. push (this. inserted [I]. data); <br/>}< br/> Ext. ajax. on ("requestcomplete", function (Conn, response, options) {<br/> alert (response. responsetext); <br/>}); <br/> // Ext. ajax. on ("requestcomplete", this. saveinsertdata, this); <br/> // you want the server to send a request to pass the modified data (only the modified data is included) <br/> Ext. aja X. Request ({URL: "../serviceservlet? Method = edit ", Params: {content: ext. util. JSON. encode (temp) }}); <br/> This. store. commitchanges (); <br/> This. filtersex (this. sexcombo); <br/>}, <br/> onremoveclick: function () {<br/> var rs = This. getselectionmodel (); <br/> try {<br/> If (RS. getcount () = 0) {<br/> Ext. MSG. alert ("system prompt", "No data is selected. Select a data row! "); </P> <p >}else {<br/> Ext. msg. Confirm (" system prompt "," Are you sure you want to delete the current data? ", This. removeuserinfo, this); <br/>}< br/>} catch (ER) {<br/> Ext. MSG. alert ("system prompt", er. discription); <br/>}< br/>}, <br/> removeuserinfo: function (btntext) {<br/> If (btntext = "yes ") {<br/> var rs = This. getselectionmodel (). getselected (); <br/> This. getstore (). remove (RS); </P> <p> If (RS. get ("ID ")! = "") {<Br/> Ext. ajax. on ("requestcomplete", function (Conn, response, options) {<br/> If (response. responsetext = "success") {<br/> alert ("success"); <br/>}else {<br/> alert ("failure "); <br/>}< br/>}); <br/> Ext. ajax. request ({URL :".. /serviceservlet? Method = remove ", Params: {ID: Rs. get ("ID") }}); <br/>}else {<br/> This. inserted. remove (RS); <br/> // This. getstore (). modified. romove (RS); <br/>}</P> <p >}); <br/> jsonpagingeditorgridpanel. user_store_url = "http: // localhost: 8080/demo3/serviceservlet? Method = JSON ";

 

Background Java code:

Import Java. io. printwriter; <br/> Import Java. util. list; <br/> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; <br/> Import COM. hoo. dao. iuser; <br/> Import COM. hoo. dao. impl. userdao; <br/> Import COM. hoo. entity. userinfo; </P> <p> @ suppresswarnings ({"unchecked", "serial "}) <br/> public class serviceservlet extends httpservlet {</P> <p> private iuser Dao = new userdao (); </P> <p> Public void doget (httpservletrequest request, httpservletresponse response) <br/> throws servletexception, ioexception {<br/> response. setcharacterencoding ("GBK"); </P> <p> printwriter out = response. getwriter (); </P> <p> stringbuilder builder = new stringbuilder (); <br/> string method = request. getparameter ("method"); <br/> If ("JSON ". equals (method) {<br/> int start = integer. parseint (request. getparameter ("START"); <br/> int Limit = integer. parseint (request. getparameter ("Limit"); </P> <p> object [] OBJ = Dao. loaduserinfo (START, limit); <br/> List <userinfo> List = (list <userinfo>) OBJ [1]; </P> <p> builder. append ("{totals :"). append (OBJ [0]). append (", users: ["); <br/> for (INT I = 0; I <list. size (); I ++) {<br/> userinfo u = (userinfo) list. get (I); <br/> builder. append ("{ID :/""). append (U. GETID () <br/>. append ("/", name :/""). append (U. getname () <br/>. append ("/", age :"). append (U. getage () <br/>. append (", sex :/""). append (U. getsex () <br/>. append ("/", birthday :/""). append (U. getbirthday () <br/>. append ("/"} "); <br/> if (I <list. size ()-1) {<br/> builder. append (","); <br/>}< br/> builder. append ("]}"); </P> <p> out. write (builder. tostring (); <br/>}< br/> If ("edit ". equals (method) {<br/> string content = request. getparameter ("content"); </P> <p> out. print (content); <br/>}</P> <p> If ("Remove ". equals (method) {<br/> integer id = integer. parseint (request. getparameter ("ID"); <br/> If (Dao. removeuserinfo (ID) {<br/> out. print ("success"); <br/>}else {<br/> out. print ("failure"); <br/>}< br/> out. flush (); <br/> out. close (); <br/>}</P> <p> Public void dopost (httpservletrequest request, httpservletresponse response) <br/> throws servletexception, ioexception {<br/> This. doget (request, response); <br/>}</P> <p>}

 

 

So far, all the examples of interaction methods in extjs 3 are here. Among them, JSON format interaction is the most common.

The reason is that the XML format is too large and contains too many labels. While files in array format are submitted very small.

Fast transmission speed. However, when the data volume is large, the data is almost readable. JSON,

It simplifies the massive submission of XML files, provides excellent readability, fast transmission in the network, and is easy to use and learn.

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.