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函數

  1. function formatOper(val,row,index){  
  2.     return '<a href="#" onclick="editUser('+index+')">修改</a>';  
  3. }  


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

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

  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');

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

 

具體效果如圖

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章