Complete demo of flex3 crud interaction with Java background

Source: Internet
Author: User

There are very few examples of flex Java curd on the Internet. Official documents are incomplete and there are errors. Today I made my own crud example,
Without paging (there are many examples on the paging network ).
:

Mxml:

<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute"> <mx: SCRIPT> <! [CDATA [include "product. as ";]> </MX: SCRIPT> <mx: httpservice id =" productservice "url =" http: // localhost: 8888/flex2/productservlet "resultformat =" E4X "useproxy =" false "/> <! -- Change the URL to your data request address --> <mx: viewstack id = "viewstack1" width = "731" Height = "473" x = "86.5" Y = "10"> <! -- Index 0 --> <mx: canvas label = "form View" width = "100%" Height = "100%"> <mx: form horizontalcenter = "0" verticalcenter = "0" backgroundcolor = "# 18e1cc" width = "124" Height = "56"> <mx: button label = "enter" Click = "fill ()" width = "100"/> </MX: Form> </MX: canvas> <! -- Index 1 --> <mx: Panel label = "advanceddatagrid" width = "100%" Height = "100%" layout = "absolute"> <mx: advanceddatagrid id = "grid1" width = "666" Height = "380" dataprovider = "{_ result. product} "editable =" true "itemeditend =" updatehandler (event) "x =" 10 "Y =" 10 "> <mx: columns> <mx: advanceddatagridcolumn datafield = "ID" headertext = "ID" editable = "false"/> <mx: advanceddatagridcolumn datafield = "productname" headert EXT = "Product Name"/> <mx: advanceddatagridcolumn datafield = "remark" headertext = "Remarks"/> </MX: columns> </MX: advanceddatagrid> <mx: button x = "60" Y = "401" label = "add" Click = "{viewstack1.selectedindex = 2}"/> <mx: button label = "delete" x = "180" Y = "401" Click = "remove ()"/> </MX: Panel> <! -- Index 2 --> <mx: canvas label = "Add new record" width = "100%" Height = "100%" id = "canvas3"> <mx: form backgroundcolor = "# ffffff" verticalcenter = "-91" horizontalcenter = "-138"> <mx: formitem label = "" width = "189" Height = "20"> <mx: button label = "return" Click = "this. viewstack1.selectedindex = 1 "/> <mx: textinput id =" hidden_id "visible =" false "/> </MX: formitem> <mx: formitem label = "Product Name"> <mx: textinput id = "productname"/> </MX: formitem> <mx: formitem label = "Remarks"> <mx: textinput id = "remark"/> </MX: formitem> <mx: button label = "save" Click = "insertproduct () "id =" BTN "/> </MX: Form> </MX: canvas> </MX: viewstack> </MX: Application>

as:

Import MX. collections. xmllistcollection; import MX. controls. alert; import MX. controls. textinput; import MX. events. advanceddatagridevent; import MX. events. closeevent; import MX. RPC. events. resultevent; private var Params: Object = new object (); // Private var ld: xmllistcollection; xmllistcollection in official documents is unavailable. In this example, [Bindable] private VaR _ result: XML; // note the file name conflict prevention./*** XML Data Rendering **/Public Function resulthandle R (Event: resultevent): void {_ result = XML (event. result);}/*** query button events for all products **/Public Function insertitemhandler (Event: resultevent): void {fill ();} /*** Method for querying all products **/Public Function fill (): void {// rebind the listener (query) productservice for productservice (httpservice. removeeventlistener (resultevent. result, insertitemhandler); productservice. addeventlistener (resultevent. result, resulthandler); productservice. metho D = "get"; // The Params ['method'] = "findall"; productservice. cancel (); productservice. send (Params); // switch to the grid view viewstack1.selectedindex = 1;}/*** insert product **/Public Function insertproduct (): void {// bind a new listener (insert) productservice. removeeventlistener (resultevent. result, resulthandler); productservice. addeventlistener (resultevent. result, insertitemhandler); productservice. method = "Post"; // pass the form parameter par AMS = {"method": "save", "ID": Nan, "productname": productname. text, "remark": remark. text}; productservice. cancel (); productservice. send (Params); clearinputfields ();}/***** event processing function for updating records ***/Public Function updatehandler (Event: advanceddatagridevent ): void {// if canceled, if (event. reason = "canceled") {return;} // re-register productservice. removeeventlistener (resultevent. result, resulthandler); productserv Ice. addeventlistener (resultevent. result, insertitemhandler); // get the new data var newdata: String = (textinput (event. currenttarget. itemeditorinstance )). text; // obtain the VaR _ ID: Int = This. grid1.selecteditem ["ID"]; VaR _ productname: String = This. grid1.selecteditem ["productname"]; VaR _ remark: String = This. grid1.selecteditem ["remark"]; // The second column is the product name if (event. columnindex = 1) {_ productname = newdata ;} // The third column is the remarks if (event. columnindex = 2) {_ remark = newdata;} Params = {"method": "Update", "ID": _ id, "productname": _ productname, "remark": _ remark}; productservice. cancel (); productservice. send (Params);}/*** Delete method **/Public Function remove (): void {var index: Int = This. grid1.selectedindex; If (Index =-1) {alert. show ("You have not selected any records", "prompt"); return;} alert. yeslabel = "OK"; alert. cancellabel = "cancel"; Al ERT. Show ("are you sure you want to delete it? "," Prompt ", alert. yes | alert. cancel, this, defaultclosehandler);}/*** process the events after deletion **/Public Function defaultclosehandler (Event: closeevent ): void {// if you click OK if (event. detail = alert. yes) {productservice. removeeventlistener (resultevent. result, resulthandler); productservice. addeventlistener (resultevent. result, insertitemhandler); var ID: String = This. grid1.selecteditem ["ID"]; Params = {"method": "Remove", "ID": Id}; productservice. cancel (); productservice. send (Params) ;}}/*** clear attribute values in form **/private function clearinputfields (): void {productname. TEXT = ""; remark. TEXT = "";}

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.