Home > Web Front-end > JS Tutorial > body text

Detailed explanation of Easyui Datagrid custom button column

小云云
Release: 2018-01-01 10:36:30
Original
2696 people have browsed it

When working on a project, due to requirements, we need to add an operation column at the end of the table. EasyUI does not seem to provide this function. Let's customize the button column and implement the code. Please refer to this article. I hope it can help you. .

Version: jQuery easyUI 1.3.2

My implementation method here is in HTML form, the js method has not been used yet

The first is the HTML part


<table id="dg" title="学生信息" class="easyui-datagrid" 
      url="${ctx}listStudent.do" 
      toolbar="#toolbar" pagination="true" 
      rownumbers="false" fitColumns="true" singleSelect="true"> 
    <thead> 
      <tr> 
        <th data-options="field:&#39;stuNo&#39;,sortable:true,width:20">学号</th> 
        <th data-options="field:&#39;name&#39;,width:20">姓名</th> 
        <th data-options="field:&#39;gender&#39;,width:20,formatter:formatGender">性别</th> 
        <th data-options="field:&#39;nationality&#39;,width:20">名族</th> 
        <th data-options="field:&#39;address&#39;,width:50,formatter:formatAddr">家庭地址</th> 
        <th data-options="field:&#39;mobile&#39;,width:20">手机号</th> 
        <th data-options="field:&#39;birthday&#39;,width:20">出生日期</th> 
        <th data-options="field:&#39;registDate&#39;,sortable:true,width:20">入学时间</th> 
        <th data-options="field:&#39;_operate&#39;,width:80,align:&#39;center&#39;,formatter:formatOper">操作</th> 
      </tr> 
    </thead> 
  </table> 
<th data-options="field:&#39;_operate&#39;,width:80,align:&#39;center&#39;,formatter:formatOper">操作</th>
Copy after login

Pay attention to the red part, which is our operation column. You can choose the name of the field casually. Here I am _operate. The key is the formatOper function


function formatOper(val,row,index){ 
  return &#39;<a href="#" rel="external nofollow" onclick="editUser(&#39;+index+&#39;)">修改</a>&#39;; 
}
Copy after login

There are three parameters in the formatOper() function. val refers to the value of the current cell, row, the current row object, and index the index of the current row. Here we need this index

I pass this index Entering a function called editUser, why do we need to pass this index? Let’s take a look at this editUser function


function editUser(index){ 
  $(&#39;#dg&#39;).datagrid(&#39;selectRow&#39;,index);// 关键在这里 
  var row = $(&#39;#dg&#39;).datagrid(&#39;getSelected&#39;); 
  if (row){ 
    $(&#39;#dlg&#39;).dialog(&#39;open&#39;).dialog(&#39;setTitle&#39;,&#39;修改学生信息&#39;); 
    $(&#39;#fm&#39;).form(&#39;load&#39;,row); 
    url = &#39;${ctx}updateStudent.do?id=&#39;+row.id; 
  } 
}
Copy after login

Looking through the easyUI documentation, we can find that datagrid has a method called selectRow


selectRow index Select a row, the row index start with 0.
Copy after login

Its function is to manually select the rows of the table. The parameter is the index value, starting from 0

In this way, we can get the mouse in real time Click on the data corresponding to the row


$(&#39;#dg&#39;).datagrid(&#39;selectRow&#39;,index);
var row = $(&#39;#dg&#39;).datagrid(&#39;getSelected&#39;);
Copy after login

These two sentences are to get the selected row

The specific effect is as shown in the figure

Related recommendations:

Example detailed explanation of adding operation buttons for each row of data in EasyUI's DataGrid

Detailed explanation of Datagrid in EasyUi control

Detailed explanation of EasyUI's DataGrid binding Json data source method

The above is the detailed content of Detailed explanation of Easyui Datagrid custom button column. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!