easyui-datagrid自定義操作列
阿新 • • 發佈:2019-01-04
在實際的專案中,有時會有操作列這個需求,主要是修改和刪除的功能。
類似於:
實現該效果的方法並不複雜。需要使用到datagrid資料表格中的formatter方法。自定義列。
下面寫個小例子。
html
<table id="dg" class="easyui-datagrid" url="你的url" striped="true" rownumbers="false" pagination="true" singleSelect="true">
<thead>
<tr>
<th field="role_id" width="15%">編號</th>
<th data-options="field:'title',width:'15%'">名稱</th>
<th data-options="field:'modules_list',width:'30%'">列表</th>
<th data-options="field:'_operate',width:'25%',formatter:formatOption">操作</th ><!--該列使用formatter,呼叫方法formatOption -->
</tr>
</thead>
</table>
js部分:在js程式碼中寫一個函式formatOption 。函式名隨便取
function formatOption (value,row,index){//用到的一個引數是index代表第幾行。
return <a onclick="updata('+index+')">修改</a> <a onclick="deletdata('+index+')" >刪除</a>
}
//修改操作
function updata(index){
$('#dg').datagrid('selectRow', index);//按照選擇的index來找到所選行
var row = $('#dg').datagrid('getSelected');//獲得選中行的資料
//得到行資料之後,就可做我們的操作了,比如點選彈出框
if(row){//在這裡最好判斷下
$('#dlg').dialog('open').dialog('setTitle','修改資訊');//開啟彈出框
//如果想去的行資料中的值。可以這樣做,row.xxx
}
}
//刪除操作
$('#dg').datagrid('selectRow', index);
var row = $('#dg').datagrid('getSelected');
//和修改操作相同
}
這樣我們的自定義操作列就完成了。
總結下:
1.在需要的自定義的列中加入formatter方法。加上一個自己寫的函式。
2.定義該函式。在函式中寫自己需要加的操作,比如‘刪除’,修改;
3.呼叫修改,刪除函式,傳入index值,根據改index,讓表格找到你點選的行
4.利用datagrid的selectRow方法,找到點選的行
5.使用datagrid的getSelect方法,得到所選行的行資料。
6.判斷得到的行資料是否為空,進行你想要的操作