jQuery Easyui datagrid行內(nèi)實(shí)現(xiàn)【添加】、【編輯】、【上移】、【下移】
前幾天項(xiàng)目中遇到一個(gè)需求用到了Easyui datagrd行內(nèi)添加和編輯數(shù)據(jù),同時(shí)對(duì)行內(nèi)數(shù)據(jù)上移下移,所以對(duì)這幾個(gè)功能做個(gè)總結(jié)。
1、首先大概說(shuō)下這幾個(gè)功能里用到的主要方法,行內(nèi)添加數(shù)據(jù)主要是添加列的editor屬性, 行內(nèi)編輯主要使用beginEdit(), endEdit(),同時(shí)一個(gè)關(guān)鍵就是拿到當(dāng)前的操作行索引editIndex.
2、撤銷用到了rejectChanges().
3、保存時(shí)使用getRows()或者getChanges(). getChanges()主要是獲取添加或編輯的數(shù)據(jù),getRows()獲取到本頁(yè)所有數(shù)據(jù),主要是配合【上移】【下移】方法使用。
4、在做這個(gè)功能中我使用了一個(gè)序列化前臺(tái)對(duì)象組件【json.js】,這個(gè)組件可以很方便的把前臺(tái)的對(duì)象轉(zhuǎn)化成json字符串,然后傳到后臺(tái),實(shí)在是方便至極讓我眼前一亮,要知道就在這個(gè)功能前面我還手動(dòng)處理數(shù)組,使用join()拼字符串,當(dāng)找到這個(gè)組件時(shí)速度效率一下幾提起來(lái)了,實(shí)在是相見(jiàn)恨晚。
5、在做這個(gè)功能,用到這些方法時(shí)遇到的問(wèn)題,剛開(kāi)始時(shí)我是看easyui的官方demo,我發(fā)現(xiàn)添加數(shù)據(jù)后點(diǎn)保存,再點(diǎn)獲取數(shù)據(jù)時(shí)就獲取不到了,后經(jīng)過(guò)測(cè)試發(fā)現(xiàn)好像是調(diào)用了acceptChanges()引起的問(wèn)題。
function GetTable() { var editRow = undefined; $("#Student_Table").datagrid({ height: 300, width: 450, title: '學(xué)生表', collapsible: true, singleSelect: true, url: '/Home/StuList', idField: 'ID', columns: [[ { field: 'ID', title: 'ID', width: 100 }, { field: 'Name', title: '姓名', width: 100, editor: { type: 'text', options: { required: true } } }, { field: 'Age', title: '年齡', width: 100, align: 'center', editor: { type: 'text', options: { required: true } } }, { field: 'Address', title: '地址', width: 100, align: 'center', editor: { type: 'text', options: { required: true } } } ]], toolbar: [{ text: '添加', iconCls: 'icon-add', handler: function () { if (editRow != undefined) { $("#Student_Table").datagrid('endEdit', editRow); } if (editRow == undefined) { $("#Student_Table").datagrid('insertRow', { index: 0, row: {} }); $("#Student_Table").datagrid('beginEdit', 0); editRow = 0; } } }, '-', { text: '保存', iconCls: 'icon-save', handler: function () { $("#Student_Table").datagrid('endEdit', editRow); //如果調(diào)用acceptChanges(),使用getChanges()則獲取不到編輯和新增的數(shù)據(jù)。 //使用JSON序列化datarow對(duì)象,發(fā)送到后臺(tái)。 var rows = $("#Student_Table").datagrid('getChanges'); var rowstr = JSON.stringify(rows); $.post('/Home/Create', rowstr, function (data) { }); } }, '-', { text: '撤銷', iconCls: 'icon-redo', handler: function () { editRow = undefined; $("#Student_Table").datagrid('rejectChanges'); $("#Student_Table").datagrid('unselectAll'); } }, '-', { text: '刪除', iconCls: 'icon-remove', handler: function () { var row = $("#Student_Table").datagrid('getSelections'); } }, '-', { text: '修改', iconCls: 'icon-edit', handler: function () { var row = $("#Student_Table").datagrid('getSelected'); if (row !=null) { if (editRow != undefined) { $("#Student_Table").datagrid('endEdit', editRow); } if (editRow == undefined) { var index = $("#Student_Table").datagrid('getRowIndex', row); $("#Student_Table").datagrid('beginEdit', index); editRow = index; $("#Student_Table").datagrid('unselectAll'); } } else { } } }, '-', { text: '上移', iconCls: 'icon-up', handler: function () { MoveUp(); } }, '-', { text: '下移', iconCls: 'icon-down', handler: function () { MoveDown(); } }], onAfterEdit: function (rowIndex, rowData, changes) { editRow = undefined; }, onDblClickRow:function (rowIndex, rowData) { if (editRow != undefined) { $("#Student_Table").datagrid('endEdit', editRow); } if (editRow == undefined) { $("#Student_Table").datagrid('beginEdit', rowIndex); editRow = rowIndex; } }, onClickRow:function(rowIndex,rowData){ if (editRow != undefined) { $("#Student_Table").datagrid('endEdit', editRow); } } }); }
<br><br>//上移 function MoveUp() { var row = $("#Student_Table").datagrid('getSelected'); var index = $("#Student_Table").datagrid('getRowIndex', row); mysort(index, 'up', 'Student_Table'); } //下移 function MoveDown() { var row = $("#Student_Table").datagrid('getSelected'); var index = $("#Student_Table").datagrid('getRowIndex', row); mysort(index, 'down', 'Student_Table'); } function mysort(index, type, gridname) { if ("up" == type) { if (index != 0) { var toup = $('#' + gridname).datagrid('getData').rows[index]; var todown = $('#' + gridname).datagrid('getData').rows[index - 1]; $('#' + gridname).datagrid('getData').rows[index] = todown; $('#' + gridname).datagrid('getData').rows[index - 1] = toup; $('#' + gridname).datagrid('refreshRow', index); $('#' + gridname).datagrid('refreshRow', index - 1); $('#' + gridname).datagrid('selectRow', index - 1); } } else if ("down" == type) { var rows = $('#' + gridname).datagrid('getRows').length; if (index != rows - 1) { var todown = $('#' + gridname).datagrid('getData').rows[index]; var toup = $('#' + gridname).datagrid('getData').rows[index + 1]; $('#' + gridname).datagrid('getData').rows[index + 1] = todown; $('#' + gridname).datagrid('getData').rows[index] = toup; $('#' + gridname).datagrid('refreshRow', index); $('#' + gridname).datagrid('refreshRow', index + 1); $('#' + gridname).datagrid('selectRow', index + 1); } } }
[HttpPost] public ActionResult Create() { string result = Request.Form[0]; //后臺(tái)拿到字符串時(shí)直接反序列化。根據(jù)需要自己處理 var list = JsonConvert.DeserializeObject<List<Student>>(result); return Json(true); }
截圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery表格行上移下移和置頂?shù)膶?shí)現(xiàn)方法
- jQuery實(shí)現(xiàn)表格行上移下移和置頂?shù)姆椒?/a>
- JQuery實(shí)現(xiàn)Table的tr上移下移功能
- JS與jQuery實(shí)現(xiàn)ListBox上移,下移,左移,右移操作功能示例
- JQuery實(shí)現(xiàn)table中tr上移下移的示例(超簡(jiǎn)單)
- jQuery基于排序功能實(shí)現(xiàn)上移、下移的方法
- jQuery Easyui Datagrid實(shí)現(xiàn)單行的上移下移及保存移動(dòng)的結(jié)果
- jquery實(shí)現(xiàn)標(biāo)簽上移、下移、置頂
- JQuery實(shí)現(xiàn)表格數(shù)據(jù)行上移與下移
相關(guān)文章
jquery實(shí)現(xiàn)鼠標(biāo)滑過(guò)顯示提示框的方法
這篇文章主要介紹了jquery實(shí)現(xiàn)鼠標(biāo)滑過(guò)顯示提示框的方法,以兩個(gè)不同實(shí)例形式分析了jQuery鼠標(biāo)滑過(guò)顯示提示框的實(shí)現(xiàn)技巧與功能代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02js制作的鼠標(biāo)懸浮時(shí)產(chǎn)生的下拉框效果
js制作的鼠標(biāo)懸浮時(shí)產(chǎn)生的下拉框效果,需要的朋友可以參考下2012-10-10jQuery實(shí)現(xiàn)左側(cè)導(dǎo)航模塊的顯示與隱藏效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)左側(cè)導(dǎo)航模塊的顯示與隱藏效果,涉及jQuery相應(yīng)鼠標(biāo)事件動(dòng)態(tài)操作頁(yè)面元素樣式的相關(guān)技巧,需要的朋友可以參考下2016-07-07jquery ui 實(shí)現(xiàn) tab標(biāo)簽功能示例【測(cè)試可用】
這篇文章主要介紹了jquery ui 實(shí)現(xiàn) tab標(biāo)簽功能,結(jié)合完整實(shí)例形式分析了jquery ui 實(shí)現(xiàn) tab標(biāo)簽切換功能的相關(guān)操作技巧,需要的朋友可以參考下2019-07-07jQuery使用prepend()方法在元素前添加內(nèi)容用法實(shí)例
這篇文章主要介紹了jQuery使用prepend()方法在元素前添加內(nèi)容的方法,實(shí)例分析了prepend方法追加內(nèi)容的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03使用jQuery.wechat構(gòu)建微信WEB應(yīng)用
本期要講的就是我痛苦中掙扎徘徊后寫的jQuery.wechat,一個(gè)提供了統(tǒng)一API的、基于jQuery.promise的jQuery.plugin。希望能多少幫助到大家。2014-10-10通過(guò)Jquery的Ajax方法讀取將table轉(zhuǎn)換為Json
這篇文章主要介紹了通過(guò)Jquery的Ajax方法讀取將table轉(zhuǎn)換為Json,需要的朋友可以參考下2014-05-05