[Original] extjs4 universal curd Method

Source: Internet
Author: User
<SCRIPT> /*  ** Author: Xiaojiang * create_time: 2012-06-01 * Reason: General curd Method *  */  Ext. Define ( "App. util. commondoactionutil" ,{  /*  ** Add the insert and update functions for information merging in form * @ parse FORM {} corresponds to form * @ parse {} * parse. grid {} the grid * parse to be refreshed. the primary key ID of the form corresponding to the PK string is ID * parse by default. callback Function after callback function execution *  */  Doinsert:  Function (Form, parseobj ){  If (! Form) {alert ( "Invalid arguments" );  Return  ;} _ Form = Form. getform ();  If  (_ Form. isvalid ()){  VaR PK = parseobj. PK | 'id '; //  Obtain the corresponding primary key              VaR Type = form. getvalues () [PK]? 'Update': 'insert' ;  VaR Url = Parseobj. Grid. getstore (). getproxy (). API [type];  If (Url = Undefined) {console. dir ( 'Invalidurl '); Return  ;} Form. Submit ({URL: URL, submitemptytext:  False  , Waitmsg: 'Submit ...' , Success: Function  (Form, Action ){  VaR Ret = eval ('+ action. response. responsetext + ')' ); Ext. msg. Alert ( 'Hup' , Ret. Message); form. Reset ();  If (Parseobj. grid! = Undefined) parseobj. Grid. Store. Load ();  If ( Typeof (Parseobj. Callback) = 'function') parseobj. Callback (_ form ); //  Call the callback function }, Failure:  Function  (Form, Action ){  VaR Ret = eval ('+ action. response. responsetext + ')' ); Ext. msg. Alert ( 'Hup' , Ret. Message );}});}},  //  -- Grid begin --      /*  ** Delete grid selection information * @ parse {} corresponding to grid * @ parse {*. the primary key ID of the form corresponding to the PK string is ID * by default *. MSG 1 does not display pop-up information *}* */  Dodelete:  Function  (Grid, parseobj ){  If (! Grid) {alert ( "Invalid arguments" );  Return  ;}  VaR PK = (parseobj! = Undefined & parseobj. pk )? Parseobj. pk: 'id '; //  Obtain the corresponding primary key          VaR MSG = (parseobj! = Undefined & parseobj. MSG! = Undefined )? Parseobj. MSG: 'Are you sure you want to delete this record' ;  VaR Store = grid. getstore (); //  Get Data Set          VaR Records = Grid. getselectionmodel (). getselection ();  VaR Data = [];  VaR Parse = {}; Ext. array. Each (records,  Function (Model) {data. Push (model. Get (PK) ;}); parse [PK] = Data. Join (","); //  Stored Parameters          If (Data. length> 0 ){  If (MSG ){ //  Check whether a confirmation box is required Ext. MessageBox. Confirm ("prompt", MSG, Function  (BTN ){  If (BTN = 'yes') {Ext. Ajax. Request ({URL: store. getproxy (). API [ 'Delete' ], Params: parse, timeout: 4000 , Success:  Function  (Response, opts ){  VaR Ret = eval ("(" + response. responsetext + ")" ); Ext. msg. Alert ( "Prompt", Ret. Message); grid. Store. Load ();}})}});}  Else  {Ext. Ajax. Request ({URL: store. getproxy (). API [ 'Delete' ], Params: parse, timeout: 4000 , Success:  Function  (Response, opts ){  VaR Ret = eval ("(" + response. responsetext + ")"); Ext. msg. Alert ( "Prompt" , Ret. Message); grid. Store. Load ();}})  //  To be optimized  }}  Else  {Ext. msg. Alert ( "Prompt", "Please select a message" );}},  /*  ** Tree deletion operation * @ Param {} tree the tree to be operated * @ Param string PKV Unique Identifier value * @ Param string PK Unique Identifier PK  */ Dodeletet:  Function  (Tree, PKV, PK, callback ){  If (! Tree |! PKV) Return  ;  VaR Me = This , Param = {}, url = tree. getstore (). getproxy (). API ['delete']; //  Get Data Set Param [PK] = PKV; //  Set parameters Ext. Ajax. Request ({URL: URL, Params: Param, success:  Function  (Response, OPT ){  VaR Node = Me. getnodebypk (tree, PKV, PK );  VaR Parentnode = Node. parentnode;  Try  {  If  (Parentnode) {parentnode. removechild (node );  If (! Parentnode. getchildat (0 ) {Parentnode. Data [ 'Leaf'] = True  ; Parentnode. updateinfo ({leaf:  True  });}}}  Catch  (E) {console. Log (E );}  If ( Typeof (Callback) = 'function') callback (response ); //  Execute the callback function                 VaR Ret = eval ("(" + response. responsetext + ")" ); Ext. msg. Alert ( "Prompt" , Ret. Message );}});},  /*  ** Tree maintenance insert/update operation * @ Param {} tree * @ Param {* PK: primary key ID * PID: parent ID (PID) * Param: insert/more detailed data * callback: callback function (ID after successful insertion )*}  */  Doinsertt:  Function  (Tree, paramobj ){  VaR Me = This ;  If (! Tree) {alert ( "Incorrect Parameter transfer" );  Return  ;}  VaR PK = paramobj. PK | 'id', parseobj = paramobj. Param; //  Obtain the corresponding primary key           VaR Type = parseobj [PK]? 'Update': 'insert '; //  Update/insert data Ext. Ajax. Request ({URL: tree. getstore (). getproxy (). API [type], Params: parseobj, submitemptytext:  False  , Waitmsg: 'Submit ...' , Success:  Function  (Res, Action ){  VaR Ret = eval ('+ res. responsetext + ')' );  VaR Ret_arr = ret. Message. Split ('-' ); If ('Update' = Type ){  VaR Node = Me. getnodebypk (tree, parseobj [PK], PK );  VaR _ Node = Node; _ node. Data = Ext. Apply (_ node. Data, parseobj); node. parentnode. replaceChild (_ node, node );  //  Update a node } Else   If ('Insert' =Type ){  VaR PID = paramobj. PID | 'pid ', pidval = Parseobj [pid];  VaR Parentnode = Me. getnodebypk (tree, pidval, PK );  VaR Resobj = Ext. applyif ({ID: ret_arr [ 1 ], Text: parseobj [ "Text" ], Leaf:  True }, Parseobj );  If (Resobj [PK] = '') resobj [PK] = ret_arr [1]; //  When the primary key is the return type                              If (Parentnode ){ //  Determine whether a parent node exists                                  Try  {Parentnode. Data [ 'Leaf'] = False  ; Parentnode. updateinfo (); //  Add element to parentnode  Parentnode. appendchild (resobj); parentnode. Expand ();}  Catch  (E) {console. dir (e );}}  Else { //  No parent node, that is, the root node                                  VaR Rootnode =Tree. getstore (). getrootnode (); rootnode. appendchild (resobj);} Ext. msg. Alert ( 'Hs', ret_arr [0 ]);  If ( Typeof (Paramobj. Callback) = 'function') paramobj. Callback (ret_arr [1]); //  Call the callback function  }, Failure:  Function  (Form, Action ){  VaR Ret = eval ('+ action. response. responsetext + ')' ); Ext. msg. Alert ( 'Hup' , Ret. Message );}});},  /*  ** Get node * @ Param {} treeobj * @ Param string PKV primary key value through the primary key * @ Param string primary key ID  */  Getnodebypk:  Function  (Tree, PKV, PK ){  If ('Id' = PK ){ //  Default PK               VaR Node = Tree. getstore (). getnodebyid (PKV );}  Else { //  If it is not an ID, search nodes cyclically.                For (I In  Tree. getstore (). Tree. nodehash ){  If (PKV = Tree. getstore (). Tree. nodehash [I]. Data [PK]) {Node = Tree. getstore (). Tree. nodehash [I];  Break ;}}}  Return  Node ;}}); </SCRIPT>

The idea is based on uspcat and changed to what your project needs...

As long as the idea is to encapsulate grid and Tree operations into uitl. Similar to Java, the service layer is not very complicated technically. You can modify the settings for your applications.

Usage:

  

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.