三.jquery.datatables.js表格編輯與刪除


1.為了使用如圖效果(即將按鈕放入行內http://www.datatables.net/examples/ajax/null_data_source.html)

采用了另一個數據格式

2.后台php,取表格數據變為:

public function initable(){
    	$db = M('yanfa_project')->select();
    	// 取$db的長度
    	// $len =count($db);
    	$item=array();
    	// 循環將$db二維數組每一項的value取出放一個數組里
    	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,
		];
		// 本地數據測試
		// $data =[
		//   "data"=> [
		//     [
		//       "Michael Bruce",
		//       "Javascript Developer",
		//       "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_encode($data);
	}

 3.前台js代碼

//表格初始化化
        var tables=$('.dataTables-example').DataTable({
            "processing": true,
            // "serverSide": true,
            "autoWidth":false,
            "ajax":{
                 "url":"initable",
            },
            "columnDefs": [{
                    "targets": -2,//編輯
                    "data": null,
                    "defaultContent": "<button id='editrow' class='btn btn-primary' type='button'><i class='fa fa-edit'></i></button>"
            },{
                    "targets": -1,//刪除
                    "data": null,
                    "defaultContent": "<button id='delrow' class='btn btn-primary' type='button'><i class='fa fa-trash-o'></i></button>"
            },
            {
                "targets": 0,//第一列隱藏
                "visible": false,
                "searchable": false
            }
            ]
        });

數據刪除

// 數據刪除
        $('.dataTables-example tbody').on( 'click', 'button#delrow', function () {
            var data = 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");
            });
        });

數據編輯

// 數據編輯
        $('.dataTables-example tbody').on( 'click', 'button#editrow', function () {
            var data = tables.row( $(this).parents('tr') ).data();
            var fields = $("#add-form").serializeArray();
            jQuery.each( fields, function(i, field){
                //jquery根據name屬性查找
                $(":input[name='"+field.name+"']").val(data[i]);
            });
            $(":input[name='mark']").val("edit");
            $("#modal-form").modal("show");//彈出框show
            
        });

 

為了標記傳入后台的是編輯還是刪除,使用了<input name='mark' type="hidden" value="add">隱形input在form里。

后台php代碼為:

public function addtable(){
        $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");
            }
        }
    }

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM