jquery Easyui Datagrid实现批量操作(编辑,删除,添加)


Posted in Javascript onFebruary 20, 2017

有时候我们的后台系统表单比较复杂,做过进销存或者一些销售订单的都应该有过感觉。

虽然Easyui Datagrid提供了行内编辑,但是不够灵活,但是我们稍微修改一下来达到批量编辑,批量删除,批量添加的效果。

现在我们来看看原的编辑:来自Easyui 1.5.1的Demo <demo/datagrid/rowediting.html>

jquery Easyui Datagrid实现批量操作(编辑,删除,添加)

接下来,我们主要是要高度自由的编辑实现:

1.可以同时追加多行

2.追加的行可以是任何位置

3.可以随时进行编辑任意位置的行

4.保存再统一验证

实现

在原有的rowediting.html进行修改!

第一:修改行的点击事件(点击行的时候进入编辑状态)

function onClickCell(index, field){
   if (editIndex != index) {
    if (endEditing()) {
     $('#dg').datagrid('selectRow', index)
       .datagrid('beginEdit', index);
     var ed = $('#dg').datagrid('getEditor', { index: index, field: field });
     if (ed) {
      ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
     }
     editIndex = index;
    } else {
     setTimeout(function () {
      $('#dg').datagrid('selectRow', editIndex);
     }, 0);
    }
   }
  }

jquery Easyui Datagrid实现批量操作(编辑,删除,添加)

第二:删除事件(点击顶部菜单Remove删除选中的行,点击列表的-号,删除减号行)

function removeit(){
   if (editIndex == undefined){return}
   $('#dg').datagrid('selectRow', editIndex);

    $('#dg').datagrid('cancelEdit', editIndex)
     .datagrid('deleteRow', editIndex);
   editIndex = undefined;
  }

jquery Easyui Datagrid实现批量操作(编辑,删除,添加)

第三:添加事件,点击菜单的Append和+号

function append(){
   var index = $('#dg').datagrid('getRowIndex', $('#dg').datagrid('getSelected'));
   if (index == -1)
    index = 0;
   $("#dg").datagrid("insertRow", {
    index: index+1,
    row: {oper: "<a href='javascript:append()'>+<a> <a href='javascript:removeit()'>-<a>",status:'P'}
    });
  }

jquery Easyui Datagrid实现批量操作(编辑,删除,添加)

第四:保存(获得操作的记录,包括,增加,修改,删除中的记录)

function accept(){
   if (endEditing()){
    var $dg = $('#dg');
    var rows = $dg.datagrid('getChanges');
    if (rows.length) {
     var inserted = $dg.datagrid('getChanges', "inserted");
     var deleted = $dg.datagrid('getChanges', "deleted");
     var updated = $dg.datagrid('getChanges', "updated");
     var effectRow = new Object();
     if (inserted.length) {
      effectRow["inserted"] = JSON.stringify(inserted);
     }
     if (deleted.length) {
      effectRow["deleted"] = JSON.stringify(deleted);
     }
     if (updated.length) {
      effectRow["updated"] = JSON.stringify(updated);
     }
     //alert(inserted);
     //alert(deleted);
     //alert(updated);
    }
   }
   //$.post("/Home/Commit", effectRow, function (rsp) {
   // if (rsp) {
   //  $dg.datagrid('acceptChanges');
   //  bindData();
   // }
   //}, "JSON").error(function () {
   // $.messager.alert("提示", "提交错误了!");
   //});
  }

最后我们可以获得,上面操作的,所有:添加的行,删除的行,更新的行!把数据传入到数据后台进行处理!

最后你还需要对数据进行循环校验,可以获得数据,在控制台输出:

console.log(inserted);
console.log(deleted);
console.log(updated);

jquery Easyui Datagrid实现批量操作(编辑,删除,添加)

总结:

最后完整代码:(替换Easyui的rowediting.html可运行效果)

<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <title>Row Editing in DataGrid - jQuery EasyUI Demo</title>
 <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css" rel="external nofollow" >
 <link rel="stylesheet" type="text/css" href="../../themes/icon.css" rel="external nofollow" >
 <link rel="stylesheet" type="text/css" href="../demo.css" rel="external nofollow" >
 <script type="text/javascript" src="../../jquery.min.js"></script>
 <script type="text/javascript" src="../../jquery.easyui.min.js"></script>
</head>
<body>

 

 <h2>Row Editing in DataGrid</h2>
 <p>Click the row to start editing.</p>
 <div style="margin:20px 0;"></div>
 
 <table id="dg" class="easyui-datagrid" title="Row Editing in DataGrid" style="width:800px;height:auto"
   data-options="
    iconCls: 'icon-edit',
    singleSelect: true,
    toolbar: '#tb',
    url: 'datagrid_data1.json',
    method: 'get',
    onClickCell: onClickCell,
    onEndEdit: onEndEdit
   ">
  <thead>
   <tr>
    <th data-options="field:'oper',width:80">操作</th>
    <th data-options="field:'itemid',width:80">Item ID</th>
    <th data-options="field:'productid',width:100,
      formatter:function(value,row){
       return row.productname;
      },
      editor:{
       type:'combobox',
       options:{
        valueField:'productid',
        textField:'productname',
        method:'get',
        url:'products.json',

       }
      }">Product</th>
    <th data-options="field:'listprice',width:80,align:'right',editor:{type:'numberbox',options:{precision:1}}">List Price</th>
    <th data-options="field:'unitcost',width:80,align:'right',editor:'numberbox'">Unit Cost</th>
    <th data-options="field:'attr1',width:250,editor:'textbox'">Attribute</th>
    <th data-options="field:'status',width:60,align:'center',editor:{type:'checkbox',options:{on:'P',off:''}}">Status</th>
   </tr>
  </thead>
 </table>

 <div id="tb" style="height:auto">
  <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="append()">Append</a>
  <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeit()">Remove</a>
  <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="accept()">Accept</a>
  <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject()">Reject</a>
  <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="getChanges()">GetChanges</a>
 </div>
 
 <script type="text/javascript">
  //编辑的行
  var editIndex = undefined;
  function endEditing() {
   if (editIndex == undefined){return true}
   $('#dg').datagrid('endEdit', editIndex);
   editIndex = undefined;
   return true;
  }
  
  function onClickCell(index, field){
   if (editIndex != index) {
    if (endEditing()) {
     $('#dg').datagrid('selectRow', index)
       .datagrid('beginEdit', index);
     var ed = $('#dg').datagrid('getEditor', { index: index, field: field });
     if (ed) {
      ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
     }
     editIndex = index;
    } else {
     setTimeout(function () {
      $('#dg').datagrid('selectRow', editIndex);
     }, 0);
    }
   }
  }
  function onEndEdit(index, row){
   var ed = $(this).datagrid('getEditor', {
    index: index,
    field: 'productid'
   });
   row.productname = $(ed.target).combobox('getText');
  }
  function append(){
   var index = $('#dg').datagrid('getRowIndex', $('#dg').datagrid('getSelected'));
   if (index == -1)
    index = 0;
   $("#dg").datagrid("insertRow", {
    index: index+1,
    row: {oper: "<a href='javascript:append()'>+<a> <a href='javascript:removeit()'>-<a>",status:'P'}
    });
  }
  function removeit(){
   if (editIndex == undefined){return}
   $('#dg').datagrid('selectRow', editIndex);

    $('#dg').datagrid('cancelEdit', editIndex)
     .datagrid('deleteRow', editIndex);
   editIndex = undefined;
  }
  function accept(){
   if (endEditing()){
    var $dg = $('#dg');
    var rows = $dg.datagrid('getChanges');
    if (rows.length) {
     var inserted = $dg.datagrid('getChanges', "inserted");
     var deleted = $dg.datagrid('getChanges', "deleted");
     var updated = $dg.datagrid('getChanges', "updated");
     var effectRow = new Object();
     if (inserted.length) {
      effectRow["inserted"] = JSON.stringify(inserted);
     }
     if (deleted.length) {
      effectRow["deleted"] = JSON.stringify(deleted);
     }
     if (updated.length) {
      effectRow["updated"] = JSON.stringify(updated);
     }
     //alert(inserted);
     //alert(deleted);
     //alert(updated);
    }
   }
   //$.post("/Home/Commit", effectRow, function (rsp) {
   // if (rsp) {
   //  $dg.datagrid('acceptChanges');
   //  bindData();
   // }
   //}, "JSON").error(function () {
   // $.messager.alert("提示", "提交错误了!");
   //});
  }
  function reject(){
   $('#dg').datagrid('rejectChanges');
   editIndex = undefined;
  }
  function getChanges(){
   var rows = $('#dg').datagrid('getChanges');
   alert(rows.length+' rows are changed!');
  }

  function contains(arr, obj) {
   var i = arr.length;
   while (i--) {
    if (arr[i] === obj) {
     return true;
    }
   }
   return false;
  }
 </script>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
juqery 学习之四 筛选查找
Nov 30 Javascript
动态创建样式表在各浏览器中的差异测试代码
Sep 13 Javascript
JQuery控制Radio选中方法分析
May 29 Javascript
JavaScript中利用Array和Object实现Map的方法
Jul 27 Javascript
js实现文字在按钮上滚动的方法
Aug 20 Javascript
javascript HTML5 Canvas实现圆盘抽奖功能
Apr 11 Javascript
JS中递归函数
Jun 17 Javascript
使用JSON作为函数的参数的优缺点
Oct 27 Javascript
AngularJS的ng Http Request与response格式转换方法
Nov 07 Javascript
javascript监听页面刷新和页面关闭事件方法详解
Jan 09 Javascript
JavaScript数组操作详解
Feb 04 Javascript
Node.js中使用mongoose操作mongodb数据库的方法
Sep 12 Javascript
原生node.js案例--前后台交互
Feb 20 #Javascript
jQuery实现单击按钮遮罩弹出对话框效果(1)
Feb 20 #Javascript
jQuery实现链接的title快速出现的方法
Feb 20 #Javascript
Angular企业级开发——MVC之控制器详解
Feb 20 #Javascript
原生JS实现《别踩白块》游戏(兼容IE)
Feb 20 #Javascript
javascript基础练习之翻转字符串与回文
Feb 20 #Javascript
JS实现动态修改table及合并单元格的方法示例
Feb 20 #Javascript
You might like
星际争霸 Starcraft 发展史
2020/03/14 星际争霸
PHP缓存集成库phpFastCache用法
2014/12/15 PHP
Yii框架实现记录日志到自定义文件的方法
2017/05/23 PHP
详解Laravel服务容器的绑定与解析
2019/11/05 PHP
jQuery-Tools-overlay 使用介绍
2012/07/14 Javascript
jquery获取css中的选择器(实例讲解)
2013/12/02 Javascript
js使用html()或text()方法获取设置p标签的显示的值
2014/08/01 Javascript
基于js实现投票的实例代码
2015/08/04 Javascript
15个常用的jquery代码片段
2015/12/19 Javascript
jquery对复选框(checkbox)的操作汇总
2016/01/13 Javascript
javascript 内置对象及常见API详细介绍
2016/11/01 Javascript
jquery+ajax实现省市区三级联动 (封装和不封装两种方式)
2017/05/15 jQuery
详解JS数据类型的值拷贝函数(深拷贝)
2017/07/13 Javascript
jQuery Validate格式验证功能实例代码(包括重名验证)
2017/07/18 jQuery
Angular4如何自定义首屏的加载动画详解
2017/07/26 Javascript
Javascript实现时间倒计时功能
2018/11/17 Javascript
vue中的 $slot 获取插槽的节点实例
2019/11/12 Javascript
[04:48]DOTA2上海特锦赛小组赛第三日 TOP10精彩集锦
2016/02/28 DOTA
[02:37]2018DOTA2亚洲邀请赛赛前采访 VP.no[o]ne心中最强SOLO是谁
2018/04/04 DOTA
[01:08:44]NB vs VP 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
复制粘贴功能的Python程序
2008/04/04 Python
python async with和async for的使用
2019/06/20 Python
python3 图片 4通道转成3通道 1通道转成3通道 图片压缩实例
2019/12/03 Python
python生成13位或16位时间戳以及反向解析时间戳的实例
2020/03/03 Python
python根据完整路径获得盘名/路径名/文件名/文件扩展名的方法
2020/04/22 Python
Python爬虫自动化爬取b站实时弹幕实例方法
2021/01/26 Python
美国珠宝网上商店:Jeulia
2016/09/01 全球购物
英国天然保健品网站:Simply Supplements
2017/03/22 全球购物
Java面试中常遇到的问题,也是需要注意的几点
2013/08/30 面试题
新电JAVA笔试题目
2014/08/31 面试题
财务部总监岗位职责
2014/03/12 职场文书
商务英语专业求职信
2014/06/26 职场文书
2015年计划生育责任书
2015/05/08 职场文书
家长会感言
2015/08/01 职场文书
springboot集成springCloud中gateway时启动报错的解决
2021/07/16 Java/Android
JavaScript异步操作中串行和并行
2021/11/20 Javascript