jQuery Easyui datagrid行內實現【添加】、【編輯】、【上移】、【下移】,jqueryeasyui

來源:互聯網
上載者:User

jQuery Easyui datagrid行內實現【添加】、【編輯】、【上移】、【下移】,jqueryeasyui

前幾天項目中遇到一個需求用到了Easyui datagrd行內添加和編輯資料,同時對行內資料上移下移,所以對這幾個功能做個總結。

1、首先大概說下這幾個功能裡用到的主要方法,行內添加資料主要是添加列的editor屬性, 行內編輯主要使用beginEdit(), endEdit(),同時一個關鍵就是拿到當前的操作行索引editIndex.

2、撤銷用到了rejectChanges().

3、儲存時使用getRows()或者getChanges(). getChanges()主要是擷取添加或編輯的資料,getRows()擷取到本頁所有資料,主要是配合【上移】【下移】方法使用。

4、在做這個功能中我使用了一個序列化前台對象組件【json.js】,這個組件可以很方便的把前台的對象轉化成json字串,然後傳到後台,實在是方便至極讓我眼前一亮,要知道就在這個功能前面我還手動處理數組,使用join()拼字串,當找到這個組件時速度效率一下幾提起來了,實在是相見恨晚。

5、在做這個功能,用到這些方法時遇到的問題,剛開始時我是看easyui的官方demo,我發現添加資料後點儲存,再點擷取資料時就擷取不到了,後經過測試發現好像是調用了acceptChanges()引起的問題。

function GetTable() {  var editRow = undefined;   $("#Student_Table").datagrid({    height: 300,    width: 450,    title: '學生表',    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);        //如果調用acceptChanges(),使用getChanges()則擷取不到編輯和新增的資料。        //使用JSON序列化datarow對象,發送到後台。        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];       //後台拿到字串時直接還原序列化。根據需要自己處理   var list = JsonConvert.DeserializeObject<List<Student>>(result);    return Json(true); } 

:

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.