我有一个加载了AJAX调用的表,并使用DataTables扩展为jQuery呈现。这是我的代码:
$("#tblSelectedUserRoles").dataTable({
bProcessing: true,
sAjaxSource: '@Url.Action("GetAllRolesForUser")?' + "userId=" + $("#txtSelectedUserId").val(),
bJQueryUI: true,
sProcessing: "<img src='~/Images/spinner.gif' />",
dom: 'T<"clear">rtip',
bAutoWidth: false,
"aoColumns": [
{ "sWidth": "1%", sClass: "smallFonts" },
{ "sWidth": "20%", sClass: "smallFonts" },
{ "sWidth": "20%", sClass: "smallFonts" },
{
"sWidth": "20%", sClass: "smallFonts", "sName": "UserRoleId", "mRender": function (data, type, row) {
return "<input type='checkbox' onchange=defaultrole('" + row[0] + "'); >";
}},
{ "sWidth": "20%", sClass: "smallFonts" },
{
"sName": "UserRoleId", "sWidth": "20%", sClass: "smallFonts", "mRender": function (data, type, row) {
return "<button class='gridButton' onclick=deleterole('" + row[0] + "');>Delete Role</button>";
}}
]
});
我正在尝试在表格中添加一个复选框。复选框显示,当我点击其中任何一个时,我使用正确的user_role_id调用该函数没有问题。我的问题是数据来自控制器中的数据:
[HttpGet]
public ActionResult GetAllRolesForUser(string userId)
{
List<UserRoleModel> userroles = DataRepository.GetSelectedUserRoles(userId);
return Json(new
{
aaData = userroles.Select(x => new String[] {
x.UserRoleId,
x.Role,
x.RoleDesc,
x.Default.ToString(),
x.UarApproved.ToString()})
}, JsonRequestBehavior.AllowGet);
}
默认值和已批准值是布尔值,我必须将它们转换为字符串,然后在aaData中传递。在任何情况下,至少应该有一个复选框都没有设置。你怎么做到这一点?
答案 0 :(得分:1)
您需要实际设置checked
状态。如果应该定义已检查状态的值为UarApproved
且该值是字符串化布尔值,请始终'true'
或'false'
,请执行以下操作:
{
sWidth: "20%",
sClass: "smallFonts",
sName: "UserRoleId",
mRender: function(data, type, row) {
return '<input type="checkbox" checked="'+row[4]+'" onchange=defaultrole('+row[0]+');/>';
}
},