日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JQuery Datatables editor 本地编辑功能

發布時間:2025/3/11 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JQuery Datatables editor 本地编辑功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

背景

很多項目需求中,不需要表格自己進行AJax操作,數據的交互需要另外寫代碼實現,表格只需要提供本地編輯即可,這該怎么辦呢?其實editor對這種實現很簡單,去掉editor中的ajax屬性即可。可以點擊這里查看editor官方網站對本地編輯的說明

我調用上一篇文章中的代碼作為演示,代碼有點長,只需要看到我注釋了editor中的ajax屬性即可:

$(document).ready(function () {editor = new $.fn.dataTable.Editor({//ajax: {// create: {// url: "/Home/AddData",// type: "Post",// dataType: "json",// data: function (data) {// var Person = {};// var id;// mydata = data.data;// for (var key in mydata)// {// console.log("check create row id:" + key);// id = key;// }// if (key == "0")//新增模式// {// console.log("entry create mode:");// Person.Name = mydata[0].Name;// Person.Position = mydata[0].Position;// Person.Office = mydata[0].Office;// Person.Age = mydata[0].Age;// Person.Salary = mydata[0].Salary;// Person.Gender = mydata[0].Gender;// Person.Level = mydata[0].Level;// Person.Date = mydata[0].Date;// }// else {//復制模式// console.log("entry duplicate mode:");// Person.Name = mydata[id].Name;// Person.Position = mydata[id].Position;// Person.Office = mydata[id].Office;// Person.Age = mydata[id].Age;// Person.Salary = mydata[id].Salary;// Person.Gender = mydata[id].Gender;// Person.Level = mydata[id].Level;// Person.Date = mydata[id].Date;// }// return Person;// },// success: function (json) {// var DT_RowId = GetRandomRowID();// console.log('new row id:' + DT_RowId);//前臺打印DT_RowId// json.data[0].DT_RowId = DT_RowId;//對新增的數據生成隨機字符串作為主鍵// },// },// edit: {// type: 'Post',// url: '/Home/UpdataData',// dataType: 'json',// data: function (data) {// var returndata = { data: [] };// var DT_RowId;// var datas = data.data;// for (var key in datas)// {// console.log('editor row id:'+ key);//前臺打印DT_RowId// DT_RowId = key;//獲取DT_RowId// var mydata = datas[DT_RowId];// var data = {};// data.DT_RowId = DT_RowId;// data.Name = mydata.Name;// data.Position = mydata.Position;// data.Office = mydata.Office;// data.Age = mydata.Age;// data.Salary = mydata.Salary;// data.Gender = mydata.Gender;// data.Level = mydata.Level;// data.Date = mydata.Date;// returndata.data.push(data);// }// return returndata;// },// success: function (json) {// //var returnData = {};// //returnData.data = json.data;// //return returnData;// },// },// remove: {// url: '/Home/DeleteData',// type: 'Post',// dataType: "json",// data: function (data) {// var returndata = {data:[]};// var DT_RowId;// var datas = data.data;// for (var key in datas) {// console.log('remove row id:' + key);//前臺打印DT_RowId// DT_RowId = key;//獲取DT_RowId// var mydata = datas[DT_RowId];// var data = {};// data.DT_RowId = DT_RowId;// data.Name = mydata.Name;// data.Position = mydata.Position;// data.Office = mydata.Office;// data.Age = mydata.Age;// data.Salary = mydata.Salary;// data.Gender = mydata.Gender;// data.Level = mydata.Level;// data.Date = mydata.Date;// returndata.data.push(data);// }// //var returndata = JSON.stringify(Persons)// return returndata;// },// success: function (json) {// //return json;// },// }//},table: "#example",//idSrc: 'Name',fields: [{ label: "Name", name: "Name" },{ label: "Position", name: "Position", multiEditable: false },//multiEditable:無法進行批量編輯{ label: "Office", name: "Office" },{ label: "Age", name: "Age" },{ label: "Salary", name: "Salary" },{label: "Gender", name: "Gender", type: "radio",options: [{ label: "男", value: "1" },{ label: "女", value: "0" },],def:"1"},{label: "Level", name: "Level", type: "select",options: [{ label: "1(highest)", value: "1" },{ label: "2", value: "2" },{ label: "3", value: "3" },{ label: "4", value: "4" },{ label: "5(lowest)", value: "5" },],def:"1" },{label: "Date", name: "Date", type: "datetime",def: function () { return new Date(); },fieldInfo: '請選擇時間',keyInput: false, //禁止用戶手動輸入}],i18n: {create: {button: "新建",title: "新增數據",submit: "保存"},edit: {button: "編輯",title: "修改數據",submit: "保存修改"},remove: {button: "刪除",title: "刪除數據",submit: "刪除",confirm: {_: "確定刪除這 %d 幾行數據?",1: "確定刪除這一行數據?"}},error: {system: "服務器或網絡發生錯誤,請聯系系統管理員或稍后再試..."},datetime: {previous: '上一頁',next: '下一頁',months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],weekdays: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天']}}});table = $('#example').DataTable({ajax: {url: "/Home/GetData",type: "Get",dataType: "json",data: {},dataSrc: function (json) {$.each(json, function (k, v) {v.DT_RowId = GetRandomRowID();//在前端對服務端返回的數據生成隨機字符串作為主鍵});return json;},error: function () {alert("服務器未正常響應,請重試");},},dom: 'Bfrtip',select: {style: 'os',selector: 'td:first-child'},buttons: [{extend: "create",editor: editor,formButtons: ["新增",{ text: "取消", action: function () { this.close();}}]},//{// extend: "edit",//原編輯功能代碼// editor: editor,// formButtons: [// "修改",// { text: "取消", action: function () { this.close(); }}// ]//},{extend: 'selected',text: '編輯',action: function () {var indexes = table.rows({ selected: true }).indexes();editor.edit(indexes, {title: '編輯',buttons: indexes.length === 1 ?backNext :'保存'});}},{extend: "remove",editor: editor,formButtons: ["刪除",{ text: "取消", action: function () { this.close();}}]},{extend: "selected",text: '復制',action: function (e, dt, node, config) {// Start in edit mode, and then change to createeditor.edit(table.rows({ selected: true }).indexes(), {title: '復制原數據',buttons: '新增復制'}).mode('create');}},],columns: [{data: null,defaultContent: '',orderable: false,className: 'select-checkbox',},{ title: "Name", data: "Name"},{ title: "Position", data: "Position",className:"canEditor"},{ title: "Office", data: "Office",className:"canEditor"},{ title: "Age", data: "Age",className: "canEditor" },{ title: "Salary", data: "Salary", className: "canEditor" },{title: "Gender", data: "Gender", className: "canEditor",render: function (val, type, row) {return val == "1" ? "男" : "女";}},{ title: "Level", data: "Level", className: "canEditor dt-body-center" },{ title: "Date", data: "Date", className: "canEditor",}],language: {processing: "正在獲取數據,請稍后...",search: "搜索",lengthMenu: "顯示 _MENU_ 條",info: "當前顯示的第是 _START_ 到 _END_ 行數據,共 _TOTAL_ 行數據",infoEmpty: "記錄數為0",infoFiltered: "((全部記錄數 _MAX_ 條))",infoPostFix: "",loadingRecords: "系統處理中,請稍等...",zeroRecords: "沒有您要搜索的內容",emptyTable: "沒有數據",paginate: {first: "第一頁",previous: "上一頁",next: "下一頁",last: "最后一頁"},aria: {sortAscending: "以升序排列此列",sortDescending: "以降序排列此列"}}});editor.on('preSubmit', function (e, o, action) {if (action !== 'remove') {var firstName = this.field('Name');// Only validate user input values - different values indicate that// the end user has not entered a valueif (!firstName.isMultiValue()) {if (!firstName.val()) {firstName.error('Name不能為空');}if (firstName.val().length >= 20) {firstName.error('Name不能超過20個字母!');}}// ... additional validation rules// If any error was reported, cancel the submission so it can be correctedif (this.inError()) {return false;}}});// Buttons array definition to create previous, save and next buttons in// an Editor formvar backNext = [{text: "&lt;",action: function (e) {// On submit, find the currently selected row and select the previous onethis.submit(function () {var indexes = table.rows({ search: 'applied' }).indexes();var currentIndex = table.row({ selected: true }).index();var currentPosition = indexes.indexOf(currentIndex);if (currentPosition > 0) {table.row(currentIndex).deselect();table.row(indexes[currentPosition - 1]).select();}// Trigger editing through the buttontable.button(1).trigger();}, null, null, false);}},'保存',{text: "&gt;",action: function (e) {// On submit, find the currently selected row and select the next onethis.submit(function () {var indexes = table.rows({ search: 'applied' }).indexes();var currentIndex = table.row({ selected: true }).index();var currentPosition = indexes.indexOf(currentIndex);if (currentPosition < indexes.length - 1) {table.row(currentIndex).deselect();table.row(indexes[currentPosition + 1]).select();}// Trigger editing through the buttontable.button(1).trigger();}, null, null, false);}}];});

去掉ajax屬性后,每次新增數據,editor便會調用自己的內部的方法對新增行生成一個唯一的(id)主鍵,存放在DT_RowId字段中,以便接下去的可以對數據進行編輯和刪除操作。可以通過如下方法獲取每行的DT_RowId的值:

var id = table.row(editCurrentRow).id();//獲取DT_RowId的值,editCurrentRow為行號

?

?

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的JQuery Datatables editor 本地编辑功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。