easyui datagrid 自定义列checkbox

时间:2020-12-01 16:16:56

单击行选中checkbox,取消则取消checkbox。可根据条件,是否选中checkbox。

$(function(){
$(
'#dg').datagrid({
title:
'Load Data',
url:
'/Customer2/GetCustomerList',
pagination:
true,
rownumbers:
true,
fitColumns:
true,
idField:
'CustomerId',
columns:[[
{field:
'CustomerId',title:'<input type="checkbox" id="ck_all" />',width:100,formatter: function(value,row,index){
return row.FirstName == "ly3" ? '<input type="checkbox" disabled="disabled" name="ckId" />'
:
'<input type="checkbox" name="ckId" value="'+value+'" />';
}},
{field:
'FirstName',title:'FirstName',width:100},
{field:
'LastName',title:'LastName',width:100,align:'center'}
]],
onLoadSuccess:
function(){
$(
"#ck_all").click(function(){
if($(this).attr("checked"))
{
var chks = $("input[name='ckId']");
for(var i=0;i<chks.length;i++)
{
var chkobj = $(chks[i])
if(!chkobj.attr("disabled"))
{
chkobj.attr(
"checked",true);
}
else
{
chkobj.parent().parent().parent().css({
"background-color":"White"})
}
}
}
else
{
$(
"input[name='ckId']").attr("checked",false);
}
})
},
onSelect:
function(rowIndex, rowData){
if(rowData.FirstName != "ly3")
{
$(
"input[value='"+rowData.CustomerId+"']").attr("checked",true);
}
},
onUnselect:
function(rowIndex, rowData){
$(
"input[value='"+rowData.CustomerId+"']").attr("checked",false);
}

});
})

html:

<head>
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Scripts/easyui3.2/themes/default/easyui.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Scripts/easyui3.2/themes/icon.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/easyui3.2/jquery-1.8.0.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/easyui3.2/jquery.easyui.min.js")" type="text/javascript"></script>
</head>
<body>
<table id="dg"></table>

c#:

 
public JsonResult GetCustomerList(int page, int rows)
{
return Json(new { total = CustomerBus.GetListPagesCount(), rows = CustomerBus.GetListPage(page, rows) });
}
/// <summary>
/// 分页
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public static IList<Customer> GetListPage(int pageIndex, int pageSize)
{
StringBuilder sb
= new StringBuilder();
using (var session = Sessions.GetSession())
using (var tx = session.BeginTransaction())
{
var p = session.QueryOver<Customer>()
.OrderBy(c
=> c.CustomerId).Asc
.Skip((pageIndex
- 1) * pageSize)
.Take(pageSize)
.List();
tx.Commit();
foreach (var item in p)
{
item.Orders
= null;
}
return p;
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static int GetListPagesCount()
{
using (var session = Sessions.GetSession())
using (var tx = session.BeginTransaction())
{
var p = session.QueryOver<Customer>()
.RowCount();
          tx.Commit();
return p;
}
}