Three. jquery.datatables.js form editing and deletion

Source: Internet
Author: User

1. In order to use the effect (the button will be placed in the line http://www.datatables.net/examples/ajax/null_data_source.html)

Another data format was used

2. Background php, take the table data into:

public function initable () {$db = M (' Yanfa_project ')->select ();    Take the length of the $db//$len =count ($DB);    $item =array (); The loop $db the value of each item in a two-dimensional array into an array of foreach ($db as & $value) {Array_push ($item, Array_values ($value));}    Array_push ($item, array_values ($db [0]), array_values ($db [1]);    echo Json_encode ($item); $data =["Data" = $item,];//Local data test//$data =[//"Data" = [//[//"Michael Bruce",//"Javascript D Eveloper ",//" Singapore ",//" 5384 ",//" 2011/06/27 ",//" $183,000 ",//" Javascript Developer ",/       /"Singapore",//"5384",//"2011/06/27",//"$183,000"//],//[//"Donna Snider",//  "Customer support",//"New York",//"4226",//"2011/01/25",//"$112,000",//"Javascript Developer ",//" Singapore ",//" 5384 ",//" 2011/06/27 ",//" $183,000 "//]//]//];echo Json_enc Ode ($data);} 

3. Front-desk JS code

//Initialization of Tables        vartables=$ ('. Datatables-example ').DataTable ({"Processing":true,//"ServerSide": true,"Autowidth":false, "Ajax":{                 "url": "Initable",            }, "Columndefs": [{                    "Targets":-2,//Edit"Data":NULL, "defaultcontent": "<button id= ' editrow ' class= ' btn btn-primary ' type= ' button ' ><i class= ' FA F A-edit ' ></i></button> "            },{                    "Targets":-1,//Delete"Data":NULL, "defaultcontent": "<button id= ' delrow ' class= ' btn btn-primary ' type= ' button ' ><i class= ' FA fa -trash-o ' ></i></button> "            },            {                "Targets": 0,//first column Hidden"Visible":false, "searchable":false            }            ]        });

Data deletion

//Data Deletion$ ('. Datatables-example tbody '). On (' Click ', ' Button#delrow ',function () {            vardata = Tables.row ($ (this). Parents (' tr ')).data (); $.Ajax ({URL: ' Deltable ',type: ' POST ',DataType: ' JSON ',Data: {id:data[0]},            })            . Done(function(data) {if(data== "Success") {Tables. Ajax.Reload ();            }; })            . Fail (function() {alert ("Error");        }); });

Data editing

//Data Editing$ ('. Datatables-example tbody '). On (' Click ', ' Button#editrow ',function () {            vardata = Tables.row ($ (this). Parents (' tr ')).data (); varFields = $ ("#add-form").Serializearray (); JQuery. each(Fields,functionIfield) {                //jquery finds by Name property$ (": Input[name= '" "+field.name+" "]").Val (Data[i]);            }); $(": Input[name= ' Mark ']"). Val ("Edit"); $("#modal-form"). Modal ("show");//Popup Box Show});

In order to mark whether the incoming background is edit or delete, use <input name= ' mark ' type= "hidden" value= "add" > Invisible input in the form.

The background PHP code is:

 Public functionaddtable () {$data=$_post; $mark=$data[' Mark ']; unset($data[' Mark ']); if($mark= = ' Add ') {            if(M (' Yanfa_project ')->add ($data)){                $this->ajaxreturn ("Success"); }        }Else{            if(M (' Yanfa_project ')->where (Array(' id ' = =$data[' ID '])) ->save ($data)){                $this->ajaxreturn ("Success"); }        }    }

 

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.