search
HomeWeb Front-endHTML TutorialEasyUI DataGrid combines with ThinkPHP to implement addition, deletion, modification and search operations for beginners_html/css_WEB-ITnose

EasyUI is a collection of user interface plug-ins based on jQuery; DataGrid is a data table;

ThinkPHP is a fast and simple lightweight PHP development framework based on MVC and object-oriented.

The integrated development environment used is WAMPSever, (wampserver is a development kit that integrates Apache, PHP and MySQL, and supports switching between different PHP versions, MySQL versions and Apache versions)

The effect is as follows:

The main code is as follows

1. Define a table

<table id="dg" class="easyui-datagrid" title="DataGrid Complex Toolbar" style="width:700px;height:250px"            data-options="rownumbers:true,singleSelect:true,url:'{:U(read)}',method:'get',toolbar:'#tb'">        <thead>            <tr>                <th data-options="field:'ID',width:80,align:'center'">ID</th>                <th data-options="field:'Product',width:100">Product</th>                <th data-options="field:'Content',width:500,align:'center'">Content</th>            </tr>        </thead></table>

class="easyui-datagrid" is a custom format in easyui. data-options are used to initialize attributes. The attributes here include rownumbers to display the number of rows. singleSelect indicates the selected status of the row;

url:'{U(read)}' First, ThinkPHP's U method (reference: http://www.thinkphp.cn/info/132.html) is used To complete the assembly of the URL address, the call in the template adopts the method of {:U('address', 'parameter'...)}. Secondly, the data format used by EasyUI is json, which is in the controller. The read method outputs data in json format. toolbar:'#tb'This is the toolbar of the table, which is to add, delete and modify.

The toolbar that defines the table is as follows:

<div id="tb" style="padding:2px 5px;">        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onClick="addPro()"></a>        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editPro()"></a>        <a href="javascrtpt:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removePro()"></a> </div>

Note: the id here should correspond to the toolbar: '#tb';

2. When you click Add or Modify, a dialog box will pop up. The code is as follows:

<!--the page of dialog-->    <div id="dl" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px" closed="true" footer="ft" buttons="#dlg-buttons">       <div class="ftitle">Information</div>       <form id="am" method="post" novalidate >             Product:<input type="text" name="Product" class="easyui-validatebox" required="true"/></br>             Content:<Textarea name="Content" rows="5" cols="45"></Textarea></br>       </form>    </div>  

class='easyui-dialog' defines a dialog box because it needs to communicate with the background Interaction, a form is installed in this dialog box, and some input elements inside need to be verified. required="true" means that the elements must be filled in

class="easyui-validatebox" defines the prompt after verification failure. , buttons="#dlg-buttons" indicates the two confirmation and cancel buttons below this dialog box. novalidate means no verification.

Buttons in the dialog box:

<div id="dlg-buttons">        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="savePro()">Save</a>        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel"           onclick="javascript:$('#dl').dialog('close')">Cancel</a></div>

3. Page js function

  <script type="text/javascript">         var url;        function addPro(){              $('#dl').dialog('open').dialog('setTitle','New Information');              $('#am').form('clear');              url = '__URL__/insert';       }        function editPro(){            var row = $("#dg").datagrid("getSelected");            if(row){                $("#dl").dialog("open").dialog("setTitle","Change Information");               $("#am").form("load",row);               url = '__URL__/update?ID='+row.ID;//为update方法准备访问url,注意是全局变量            }        }                function savePro(){            $('#am').form('submit',{                url: url,                onSubmit: function(){                    return $(this).form('validate');                },                success: function(result){                    var result = eval('('+result+')');                    if (result.success){                        $('#dl').dialog('close');        // close the dialog                        $('#dg').datagrid('reload');    // reload the user data                    } else {                        $.messager.show({                            title: 'Error',                            msg: result.msg                        });                    }                }            });        }              function removePro()       {          var row = $('#dg').datagrid('getSelected');            if (row){                $.messager.confirm('Confirm','Are you sure you want to remove this row?',function(r){                    if (r){                        $.post('__URL__/delete',{ID:row.ID},function(result){                            if (result.success){                                $('#dg').datagrid('reload');    // reload the user data                            } else {                                $.messager.show({    // show error message                                    title: 'Error',                                    msg: result.msg                                });                            }                        },'json');                    }                });            }     }    </script>

JS is not yet I know a lot about it, so I referred to the code on the Internet. $.messager.show is the message prompt box provided by EasyUI (reference: http://www.jeasyui.net/demo/371.html), which can display a message window in the lower right corner of the screen. $.messager.confirm is an interactive message that pops up a message confirmation box.

4. Code in the controller (IndexAction.class.php)

<?php// 本类由系统自动生成,仅供测试用途class IndexAction extends Action {    public function index(){      $this->display();    }      publicfunction read(){
$Test = M('test');        /*$Total = $Test->count();        $Json = '{"total":'.$Total.',"rows":'.json_encode($Test->select()).'}';*/        $Json = json_encode($Test->select());        echo $Json;
 }     public function insert(){        $data = $this->_post();        $Test = M('Test');        $result = $Test->add($data);        if($result)    {            echo json_encode(array('success'=>true));        }else {            echo json_encode(array('msg'=>'Some error occured'));            }      }    public function update($ID=0){       $Test  =   M('test');       $ID = $_GET['ID'];       if($Test->create()) {           $Test->ID = $ID;        $result  =   $Test->save();        if($result)    {            echo json_encode(array('success'=>true));        }else {            echo json_encode(array('msg'=>'Some error occured'));            }        }else{              $this->error($Test->getError());         }     }      public function delete($ID=0){         $result = false;         $Test = M('test');         $result = $Test->where('ID='.$ID)->delete();         if($result==false){             echo json_encode(array('msg'=>'删除出错!'));         }else{             echo json_encode(array('success'=>true));         }     }     }?>

Statement
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
Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

How to implement multi-project carousel in Bootstrap 4?How to implement multi-project carousel in Bootstrap 4?Apr 30, 2025 pm 03:24 PM

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How does deepseek official website achieve the effect of penetrating mouse scroll event?How does deepseek official website achieve the effect of penetrating mouse scroll event?Apr 30, 2025 pm 03:21 PM

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

How to modify the playback control style of HTML videoHow to modify the playback control style of HTML videoApr 30, 2025 pm 03:18 PM

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.

What problems will be caused by using native select on your phone?What problems will be caused by using native select on your phone?Apr 30, 2025 pm 03:15 PM

Potential problems with using native select on mobile phones When developing mobile applications, we often encounter the need for selecting boxes. Normally, developers...

What are the disadvantages of using native select on your phone?What are the disadvantages of using native select on your phone?Apr 30, 2025 pm 03:12 PM

What are the disadvantages of using native select on your phone? When developing applications on mobile devices, it is very important to choose the right UI components. Many developers...

How to optimize collision handling of third-person roaming in a room using Three.js and Octree?How to optimize collision handling of third-person roaming in a room using Three.js and Octree?Apr 30, 2025 pm 03:09 PM

Use Three.js and Octree to optimize collision handling of third-person roaming in the room. Use Octree in Three.js to implement third-person roaming in the room and add collisions...

See all articles

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.