Home Web Front-end JS Tutorial Detailed explanation of inline editing examples of dataGrid in EasyUI

Detailed explanation of inline editing examples of dataGrid in EasyUI

Jan 06, 2018 am 11:00 AM
easyui edit

This article mainly introduces the implementation code of inline editing of dataGrid in EasyUI. It is very good and has reference value. Friends who need it can refer to it. I hope it can help everyone.

This js code was written by someone else, and it may not be the best, but I personally think it would be particularly good if it can help others solve functional problems first. I modified it slightly and used it in my own project. I will post it here to share it. The TinkPHP used in the backend is relatively simple to check for additions, deletions and modifications, so I won’t post it here. I won’t post the front desk renderings because I’m lazy.


$(function () {
    var datagrid; //定义全局变量datagrid
    var editRow = undefined; //定义全局变量:当前编辑的行
    datagrid = TskupluAddPacket.datagrid({
        url: ThinkPHP['MODULE'] + '/Tskuplu/getPacketList', //请求的数据源
        iconCls: 'icon-save', //图标
        pagination: true, //显示分页
        pageSize: 15, //页大小
        pageList: [15, 30, 45, 60], //页大小下拉选项此项各value是pageSize的倍数
        fit: true, //datagrid自适应宽度
        fitColumn: false, //列自适应宽度
        striped: true, //行背景交换
        nowap: true, //列内容多时自动折至第二行
        border: false,
        idField: 'packetid', //主键
        sortName : 'packetid',                                  //排序字段
        sortOrder : 'desc',                  //排序方式
        columns: [[//显示的列
            {field: 'packetid', title: 'ID', width: 100, sortable: true, checkbox: true },
            { field: 'packunit', title: '包装单位', width: 100, sortable: true,
                editor: { type: 'validatebox', options: { required: true} }
            },
            { field: 'packqty', title: '包装细数', width: 100,
                editor: { type: 'validatebox', options: { required: true} }
            },
            { field: 'packspec', title: '包装规格', width: 100,
                editor: { type: 'validatebox', options: { required: true} }
            }
        ]],
        queryParams: { 
          pluid: $('#addpluid').val()
        }, //查询参数
        toolbar: [{ text: '添加', iconCls: 'icon-add', handler: function () {//添加列表的操作按钮添加,修改,删除等
            //添加时如果没有正在编辑的行,则在datagrid的第一行插入一行
            if (editRow == undefined) {                     
                datagrid.datagrid("insertRow", {
                    index: 0, // index start with 0
                    row: {}
                });          
                //将新插入的那一行开户编辑状态
                datagrid.datagrid("beginEdit", 0);
                //给当前编辑的行赋值
                editRow = 0;
            }
        }
        }, '-',
        { text: '删除', iconCls: 'icon-remove', 
          handler: function () {
             //删除时先获取选择行
             var rows = datagrid.datagrid("getSelections");
             //选择要删除的行
             if (rows.length > 0) {
                $.messager.confirm("提示", "你确定要删除吗?", function (r) {
                  if (r) {
                    var ids = [];
                    for (var i = 0; i < rows.length; i++) {
                      ids.push(rows[i].packetid);
                    }
                    //将选择到的行存入数组并用,分隔转换成字符串,
                    //本例只是前台操作没有与数据库进行交互所以此处只是弹出要传入后台的id
                    //alert(ids.join(&#39;,&#39;));
                    $.ajax({
                      url : ThinkPHP[&#39;MODULE&#39;] + &#39;/Tskuplu/deletePacket&#39;,
                      type : &#39;POST&#39;,
                      data : {
                        ids : ids.join(&#39;,&#39;)
                      },
                      beforeSend : function (){
                        $.messager.progress({
                          text : &#39;正在处理中...&#39;
                        });  
                      },
                      success : function (data){
                        $.messager.progress(&#39;close&#39;);
                        if (data >0){
                          datagrid.datagrid(&#39;reload&#39;);
                          $.messager.show({
                            title : &#39;操作提醒&#39;,
                            msg  : data + &#39;条数据被成功删除!&#39;
                          })
                        } else if( data == -999 ) {
                          $.messager.alert(&#39;删除失败&#39;, &#39;对不起,您没有权限!&#39;, &#39;warning&#39;);
                        } else {
                          $.messager.alert(&#39;删除失败&#39;, &#39;没有删除任何数据!&#39;, &#39;warning&#39;);
                        }
                      }
                    });                  
                  }
                });
             } else {
                $.messager.alert("提示", "请选择要删除的行", "error");
             } 
          }
        }, &#39;-&#39;,
        { text: &#39;修改&#39;, iconCls: &#39;icon-edit&#39;, 
          handler: function () {
            //修改时要获取选择到的行
            var rows = datagrid.datagrid("getSelections");
            //如果只选择了一行则可以进行修改,否则不操作
            if (rows.length == 1) {
              //当无编辑行时
              if (editRow == undefined) {
                //获取到当前选择行的下标
                var index = datagrid.datagrid("getRowIndex", rows[0]);
                //开启编辑
                datagrid.datagrid("beginEdit", index);
                //把当前开启编辑的行赋值给全局变量editRow
                editRow = index;
                //当开启了当前选择行的编辑状态之后,
                //应该取消当前列表的所有选择行,要不然双击之后无法再选择其他行进行编辑
                datagrid.datagrid("unselectAll");
              }
            }
          }
        }, &#39;-&#39;,
        { text: &#39;保存&#39;, iconCls: &#39;icon-save&#39;, 
          handler: function () {
             //保存时结束当前编辑的行,自动触发onAfterEdit事件如果要与后台交互可将数据通过Ajax提交后台
             datagrid.datagrid("endEdit", editRow); 
             editRow = undefined;
          }
        }, &#39;-&#39;,
        { text: &#39;取消编辑&#39;, iconCls: &#39;icon-redo&#39;, 
          handler: function () {
             //取消当前编辑行把当前编辑行罢undefined回滚改变的数据,取消选择的行
             editRow = undefined;
             datagrid.datagrid("rejectChanges");
             datagrid.datagrid("unselectAll");
          }
        }, &#39;-&#39;],
        onAfterEdit: function (rowIndex, rowData, changes) {
          //endEdit该方法触发此事件           
          //var row = datagrid.datagrid("getData").rows[rowIndex]; //获取某一行的值 
          var inserted = datagrid.datagrid(&#39;getChanges&#39;,&#39;inserted&#39;);
          var updated = datagrid.datagrid(&#39;getChanges&#39;,&#39;updated&#39;);
          if(inserted.length < 1 && updated.length <1){
            editRow = undefined;
            datagrid.datagrid(&#39;unselectAll&#39;);
            return;
          }
          var url = &#39;&#39;;
          if(inserted.length>0){
            url=ThinkPHP[&#39;MODULE&#39;] + &#39;/Tskuplu/addPacket&#39;;
          }
          if(updated.length>0){
            url=ThinkPHP[&#39;MODULE&#39;] + &#39;/Tskuplu/updatePacket&#39;;
          }
          $.ajax({
            url : url,
            type : &#39;POST&#39;,
            data : {
              &#39;pluid&#39;: $(&#39;#addpluid&#39;).val(),
              &#39;packetid&#39;:rowData.packetid,
              &#39;packunit&#39;:rowData.packunit,
              &#39;packqty&#39; :rowData.packqty,
              &#39;packspec&#39;:rowData.packspec
            },
            beforeSend : function (){
              $.messager.progress({
                text : &#39;正在处理中...&#39;
              })
            },
            success : function (data){
              $.messager.progress(&#39;close&#39;);
              if (data > 0){ 
                datagrid.datagrid("acceptChanges"); 
                $.messager.show({
                  title : &#39;操作提示&#39;,
                  msg : &#39;添加成功&#39;
                });      
                editRow = undefined;
                datagrid.datagrid("reload"); 
                $(&#39;#addcheck&#39;).val(1);
              } else if (data == -999) {
                $.messager.alert(&#39;添加失败&#39;, &#39;抱歉!您没有权限!&#39;, &#39;warning&#39;);
              } else {
                datagrid.datagrid("beginEdit",editRow); 
                $.messager.alert(&#39;警告操作&#39;, &#39;未知错误!请重新刷新后提交!&#39;, &#39;warning&#39;);
              }
              datagrid.datagrid("unselectAll"); 
            }
          });
          //////////////////                         
        },
        onDblClickRow: function (rowIndex, rowData) {
        //双击开启编辑行
          if (editRow == undefined) {
              datagrid.datagrid("beginEdit", rowIndex);
              editRow = rowIndex;
          }
        }
    });   
  });
Copy after login

Related recommendations:

Easyui Datagrid custom button column detailed explanation

Example detailed explanationEasyUI Add operation button to each row of data in DataGrid

Detailed explanation of Datagrid in EasyUi control

The above is the detailed content of Detailed explanation of inline editing examples of dataGrid in EasyUI. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to edit documents in Tencent Docs? -Tencent Document Editing Document Tutorial Guide How to edit documents in Tencent Docs? -Tencent Document Editing Document Tutorial Guide Mar 19, 2024 am 08:19 AM

Does anyone know how to edit documents in Tencent Docs? It doesn't matter if you don't know. Today, the editor will introduce detailed graphic explanations on how to edit documents in Tencent Docs. I hope it can help you. Detailed graphic explanation of editing documents in Tencent Documents 1. First, enter Tencent Documents directly (if you don’t have it, download it now!) and log in directly (QQ and TIM two login methods are supported) 2. After logging in, click Add in the upper right corner No., directly create online documents, online forms, new folders, etc.! 3. Then enter the information according to your needs!

How to restore the deleted hosts file How to restore the deleted hosts file Feb 22, 2024 pm 10:48 PM

Title: How to restore the hosts file after deletion Summary: The hosts file is a very important file in the operating system and is used to map domain names to IP addresses. If you accidentally delete the hosts file, you may be unable to access certain websites or have other network problems. This article will introduce how to recover accidentally deleted hosts file in Windows and Mac operating systems. Text: 1. Restore hosts file in Windows operating system. Hosts file in Windows operating system

How to edit home screen pages on iPhone How to edit home screen pages on iPhone Feb 14, 2024 pm 02:00 PM

Apple allows you to quickly change your home screen by rearranging your home screen pages at any time and deleting them freely. This way, you can easily hide multiple apps and widgets without dragging and deleting them one by one. In this article, we will explain how to edit pages on your iPhone home screen. CONTENTS[SHOW] Shows how to edit Home screen pages on iPhone You can edit the Home screen to rearrange pages, hide/unhide certain pages in the Home screen, and delete pages completely. To start editing your iPhone home screen, press and hold an empty area on your home screen. When your home screen enters jitter mode, tap the row of dots at the bottom of the screen. You should now see all your home screens displayed in a grid format. Option 1: On the home screen

What to do if word document cannot be edited What to do if word document cannot be edited Mar 19, 2024 pm 09:37 PM

After editing the document, we will save the document to provide convenience for editing and modifying the document next time. Sometimes we can modify it directly after clicking on the edited document, but sometimes for some unknown reason, there is no response no matter how we click on the word document, and the command will not be executed. , what should I do if the word document cannot be edited? Don’t worry, the editor will help you solve this problem. Let’s take a look at the operation process. After opening a Word document, when editing text, you will see a &quot;Restrict Editing&quot; prompt displayed on the right side of the page, as shown in the figure below. 2. You need to cancel editing and you need to know the set password. Click &quot;Stop Protection&quot; below the pop-up prompt, as shown in the figure below. 3. Then enter the password in the &quot;Unprotect Document&quot; dialog box and click OK, as shown in the figure below.

The specific method of editing vertical subtitles in Edius The specific method of editing vertical subtitles in Edius Mar 28, 2024 pm 02:52 PM

1. Make preparations. Import a piece of material into the material library and drag it to the timeline. 2. Click the [T] button on the timeline track, choose to add subtitles on the 1T track, and you will enter the subtitle editing page. The operation is as shown in the picture: 3. Here you can write the text content we want. It is obvious that the subtitles are written horizontally. Now let’s take a look at how to implement vertical subtitles. Don't write the content yet, select [Insert - Text - Vertical] as shown in the picture: 4. Now write the words and it will be arranged vertically. After adjusting the position, size, font, color and other information of the subtitles, click Save in the upper left corner of the window.

Is easyui a jquery plug-in? Is easyui a jquery plug-in? Jul 05, 2022 pm 06:08 PM

easyui is a jquery plug-in. easyui is a front-end UI interface plug-in based on JQuery, which is used to help web developers more easily create feature-rich and beautiful UI interfaces. easyui is a framework that perfectly supports HTML5 web pages, which can help developers save the time and scale of web development.

How to edit messages on iPhone How to edit messages on iPhone Dec 18, 2023 pm 02:13 PM

The native Messages app on iPhone lets you easily edit sent texts. This way, you can correct your mistakes, punctuation, and even autocorrect wrong phrases/words that may have been applied to your text. In this article, we will learn how to edit messages on iPhone. How to Edit Messages on iPhone Required: iPhone running iOS16 or later. You can only edit iMessage text on the Messages app, and then only within 15 minutes of sending the original text. Non-iMessage text is not supported, so they cannot be retrieved or edited. Launch the Messages app on your iPhone. In Messages, select the conversation from which you want to edit the message

In-depth analysis of PyCharm Chinese settings: improving code editing experience In-depth analysis of PyCharm Chinese settings: improving code editing experience Jan 27, 2024 am 10:30 AM

PyCharm is a powerful Python integrated development environment (IDE) that is widely used in Python development. It not only provides rich code editing functions, but also has powerful tools for intelligent prompts, debugging, version management, etc. In PyCharm, the Chinese setting can make our code editing smoother and more convenient. This article will introduce the Chinese settings in PyCharm in detail and provide some specific code examples. Install the language pack First, in the settings of PyCharm, we need

See all articles