easyui datagrid自定義按鈕列,即最后面的操作列(轉)


做項目的時候因為需求,要在表格的最后添加一列操作列,easyUI貌似沒有提供這種功能,不過沒關系,我們可以自定義來實現

版本:jQuery easyUI 1.3.2

這里我的實現方式是采用HTML形式,js方式暫時還沒用到

首先是HTML部分

  1. <table id="dg" title="學生信息" class="easyui-datagrid"  
  2.             url="${ctx}listStudent.do"  
  3.             toolbar="#toolbar" pagination="true"  
  4.             rownumbers="false" fitColumns="true" singleSelect="true">  
  5.         <thead>  
  6.             <tr>  
  7.                 <th data-options="field:'stuNo',sortable:true,width:20">學號</th>  
  8.                 <th data-options="field:'name',width:20">姓名</th>  
  9.                 <th data-options="field:'gender',width:20,formatter:formatGender">性別</th>  
  10.                 <th data-options="field:'nationality',width:20">名族</th>  
  11.                 <th data-options="field:'address',width:50,formatter:formatAddr">家庭地址</th>  
  12.                 <th data-options="field:'mobile',width:20">手機號</th>  
  13.                 <th data-options="field:'birthday',width:20">出生日期</th>  
  14.                 <th data-options="field:'registDate',sortable:true,width:20">入學時間</th>  
  15.                 <th data-options="field:'_operate',width:80,align:'center',formatter:formatOper">操作</th>  
  16.             </tr>  
  17.         </thead>  
  18.     </table>  

<th data-options="field:'_operate',width:80,align:'center',formatter:formatOper">操作</th>
注意紅色部分,就是我們的操作列,field的名字隨便取,我這里是_operate,關鍵是formatOper函數

[javascript] view plain copy
print ?
  1. function formatOper(val,row,index){  
  2.     return '<a href="#" onclick="editUser('+index+')">修改</a>';  
  3. }  


formatOper()函數中有三個參數,val指當前單元格的值,row,當前行對象,index當前行的索引.這里我們就需要這個index

我把這個index傳入了一個叫editUser的函數中,為什么要傳這個index呢,我們在來看下這個editUser函數

[javascript] view plain copy
print ?
  1. function editUser(index){  
  2.     $('#dg').datagrid('selectRow',index);// 關鍵在這里  
  3.     var row = $('#dg').datagrid('getSelected');  
  4.     if (row){  
  5.         $('#dlg').dialog('open').dialog('setTitle','修改學生信息');  
  6.         $('#fm').form('load',row);  
  7.         url = '${ctx}updateStudent.do?id='+row.id;  
  8.     }  
  9. }  

翻閱easyUI文檔可以發現datagrid有一個方法叫selectRow

selectRow index Select a row, the row index start with 0.

它的作用就是手動選中表格的行,參數就是index值,從0開始

這樣,我們就能實時獲取到鼠標點擊行所對應的數據了

 

$('#dg').datagrid('selectRow',index);

var row = $('#dg').datagrid('getSelected');

這兩句話就是獲取選中的行

 

具體效果如圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM