Home Web Front-end JS Tutorial How to implement basic single-line editing function

How to implement basic single-line editing function

Oct 02, 2017 am 09:45 AM
Base edit

1.html code

<table id="tableList" style="text-align:center;"></table>
<p id=tablePager></p>
Copy after login

2.script code

<script type="text/javascript">
            var lastId;//行编辑时用来存放行的id
            $(function(){
                showTable();    //显示jqgrid表格    
                $(&#39;.btn-update&#39;).click(updateRowData);//编辑按钮
                $(&#39;.btn-save&#39;).click(saveRowData);//保存按钮添加事件,默认不可用
                $(&#39;.btn-cancel&#39;).click(cancelRowData);//取消按钮,取消编辑操作
            });            function showTable(){
                layer.load(2);
                $("#tableList").jqGrid({ 
                    url:&#39;myList.action&#39;,
                    mtype: "POST",
                    styleUI : &#39;AmazeUI&#39;,
                    datatype: "json",
                    height:"auto", 
                    autowidth:true,
                    rownumbers: true,
                    multiselect: true,
                    colNames:[&#39;id&#39;,&#39;数量&#39;],   
                    colModel:[
                               {name:&#39;id&#39;,index:&#39;id&#39;,hidden:true},      
                                /*实现行编辑功能需要添加属性editable:true,edittype:&#39;text&#39;,其中&#39;text&#39;与input中的type属性对应,
                                比如checkbox,radio,password等*/                                                           
                               {name:&#39;num&#39;,index:&#39;num&#39;,align: "center",editable: true, edittype: &#39;text&#39;}                             
                              ], 
                            sortable:true,
                            sortname:&#39;bc.serialNumber&#39;,
                            sortorder:&#39;asc&#39;,
                            rowNum:10, 
                    rowList:[10,20,30,90],                    //显示记录数的格式
                    recordtext : "记录 {0} - {1} 总记录数 {2}",                    //页数显示格式
                    pgtext : "第 {0}页       共  {1} 页",

                    viewrecords:true,             
                    jsonReader: {                            // 数据行(默认为:rows)
                            root:"rows",                
                            repeatitems : false,       
                            page: "page",           // 当前页
                            records:"records",    // 总记录数
                            total: "total" 
                    },
                    onSelectRow: function(id){

                     },
                     onSelectAll:function(id){
                     },                     //加载完成(初始加载),回调函数
                     loadComplete: function(){ 
                         layer.closeAll(&#39;loading&#39;);                         
                         var page = $(&#39;#tableList&#39;).getGridParam(&#39;page&#39;);
                         layer.msg(&#39;第&#39;+page+&#39;页&#39;, {
                            time: 1000, //1s后自动关闭
                          });
                    },                    /*编辑提交时用来对提交的数据进行序列化,如果不添加此属
                    性,默认提交的是各个可编辑的字段名值对,后台需要有多个同名
                    字段来对应,序列化后,可以只用一个包含各个字段的对象即可接
                    收*/
                    serializeRowData: function(postdata) {
        return  {&#39;entity.id&#39;:postdata.id,&#39;entity.num&#39;:postdata.num};
                            },
                    prmNames:                     
                    {rows:"pageInfo.pageSize",page:"pageInfo.page",
                    sort:"pageInfo.sidx",order:"pageInfo.sord",
                    search:  "pageInfo._search"},
                    pager:"#tablePager"
                });
            }            /*编辑行的函数*/
            function updateRowData()
            {
                //获取选中行的id
                var id=$(&#39;#tableList&#39;).jqGrid(&#39;getGridParam&#39;,&#39;selrow&#39;);                
                if(id==null)
                    {                        
                    return;
                    }
                lastId=id; //存放编辑的id
                //调用此方法,使当前行变为可编辑
                $("#tableList").jqGrid(&#39;editRow&#39;, id);  

                $(&#39;.btn-update&#39;).attr("disabled",true);//编辑按钮变为不可用
                //保存和取消按钮变为可用
                $(&#39;.btn-save&#39;).attr("disabled",false);
                $(&#39;.btn-cancel&#39;).attr("disabled",false);
            }            /*保存编辑后的数据函数*/
            function saveRowData()
            {
                $("#tableList").jqGrid(                        
                &#39;saveRow&#39;, 
                        lastId,//获取编辑行的id
                        { 
                            /*成功提交到后台的回调函数*/
                            successfunc: function(response) {
                                    //返回到前台的json字符串

                var data = 
                eval(&#39;(&#39; + response.responseText + &#39;)&#39;);                                    if(data.resultCode==0)
                                        {
                                    layer.msg("保存成功!",{icon:1});                                    //返回true,对前台数据进行更新
                                            return true;
                                        }                                    else
                                    {
                                    layer.msg("保存失败!",{icon:2});                                   //返回false,对前台数据不更新
                                            return false;
                                    }
                                },                                /*提交的请求地址*/
                            url:&#39;recoveryTokenUpdate.action&#39;,                            /*系统发生异常时的回调函数*/
                            errorfunc:function(){

                            layer.msg(&#39;系统异常!&#39;, {time: 2000});
                            },                            /*请求类型post*/
                            "mtype" : "POST"
                        });
                $(&#39;.btn-updateToken&#39;).attr(&#39;disabled&#39;,false);
                $(&#39;.btn-saveToken&#39;).attr(&#39;disabled&#39;,true);
                $(&#39;.btn-cancelToken&#39;).attr(&#39;disabled&#39;,true);
            }            /*取消编辑函数*/
            function cancelRowData()
            {
                //取消所编辑的行的操作
                $(&#39;#tableList&#39;).jqGrid(&#39;restoreRow&#39;, lastId);
                $(&#39;.btn-updateToken&#39;).attr(&#39;disabled&#39;,false);
                $(&#39;.btn-saveToken&#39;).attr(&#39;disabled&#39;,true);
                $(&#39;.btn-cancelToken&#39;).attr(&#39;disabled&#39;,true);
            }        </script>
Copy after login

The above is the detailed content of How to implement basic single-line editing function. 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)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
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.

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

PHP Basics Tutorial: From Beginner to Master PHP Basics Tutorial: From Beginner to Master Jun 18, 2023 am 09:43 AM

PHP is a widely used open source server-side scripting language that can handle all tasks in web development. PHP is widely used in web development, especially for its excellent performance in dynamic data processing, so it is loved and used by many developers. In this article, we will explain the basics of PHP step by step to help beginners from getting started to becoming proficient. 1. Basic syntax PHP is an interpreted language whose code is similar to HTML, CSS and JavaScript. Every PHP statement ends with a semicolon; Note

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

See all articles