在MVC 5项目中使用Jquery Datatables。还使用Datatables.MVC5帮助程序,这使得整个任务变得更加容易,并且this tutorial帮助设置了MVC服务器端处理。
我正在尝试显示按钮列,即编辑|删除|作为基于用户角色的前两列。这些按钮会调用我的普通MVC控制器。
我可以创建按钮,我无法弄清楚如何处理角色检查。使用Razor语法,我以前的客户端很容易,现在使用DataTables会更加棘手。
我目前的表格。
$(function () {
assetListVM = {
dt: null,
init: function () {
dt = $('#assetdatatable').DataTable({
"serverSide": true,
"processing": true,
"DisplayLength": 25,
"ajax": {
"type": "POST", // Required to change from 'GET' as this produced server error query string length too long.
"url": "@Url.Action("Get","Demo")",
"data": // Allows us to return extra data object to controller
function (d) {
d.excelExport = $("#excelExport").val();
}
},
"scrollY": viewheight, // Calculated to help ensure table is fitting max area of screen with out dropping off screen.
"colReorder": true,
"select": true,
"stateSave": true,
"dom": 'fBrtip',
"lengthMenu": [[25, 50, 100, 200, -1], [25, 50, 100, 200, "All"]],
"buttons": [
"pageLength",
"copy",
"excel"
],
"columns": [
// HERE I need to check user Role and choose if to display column or not.
// I have tried Razor @if (User.IsInRole("Sausage")) { }
{
"title": "button column"
"data": "AssetID",
"className": 'details_button',
"render": function (AssetID)
{
return '<a class="btn-xs btn-primary glyphicon glyphicon-list-alt" href=/Demo/Details/' + AssetID +'></a>';
}
},
{ "title": "AssetID", "data": "AssetID" },
{ "title": "SIM", "data": "SIM" },
{ "title": "IMEI", "data": "IMEI" },
{ "title": "Software", "data": "LoggedOnSoftware" },
{ "title": "Soft No", "data": "LoggedOnSoftwareVerNo" },
{ "title": "Last Reset", "data": "LastResetType" },
{
"title": "Last Log", "data": "LastLogOnTime",
"render": function (LastLogOnTime) {
return (moment(LastLogOnTime).isValid()) ? moment(LastLogOnTime).format("DD MMM YY") : "-";
}
}
],
"order": [[2, 'asc']]
});
}
}
// initialize the datatables
assetListVM.init();
});
在标题为“按钮”列的列中,我尝试了Razor语法,但与以前的表格不同,dummys项目都是基于html的。我可以发送一个具有当前用户角色的viewbag对象进行查看,然后检查一下,我只是不知道如何在表设置中执行if / else decissions。
如果有人有任何类似的例子,我可以掌握或指出我正确的方向,我们将非常感激。
我搜索并检查了我发现的最近的问题this old one。
更新: 感谢Jamie
这是我的新工作代码,它基于当前用户角色将隐藏第一列。
var RoleCheck = @(User.IsInRole("sausage") ? "true" : "false"); // new check for user role outside of Jquery DataTable function which will work.
$(function () {
assetListVM = {
dt: null,
init: function () {
dt = $('#assetdatatable').DataTable({
"serverSide": true,
"processing": true,
"ajax": {
"type": "POST", // Required to change from 'GET' as this produced server error query string length too long.
"url": "@Url.Action("Get","Demo")",
"data": // Allows us to return extra data object to controller
function (d) {
d.excelExport = $("#excelExport").val();
}
},
"columnDefs": [
{
"target": [0],
"visible": RoleCheck // new variable true or false based on user role.
}
]
"columns": [
{
"title": "button column"
"data": "AssetID",
"className": 'details_button',
"render": function (AssetID)
{
return '<a class="btn-xs btn-primary glyphicon glyphicon-list-alt" href=/Demo/Details/' + AssetID +'></a>';
}
},
{ "title": "AssetID", "data": "AssetID" },
{ "title": "SIM", "data": "SIM" },
{ "title": "IMEI", "data": "IMEI" },
{ "title": "Software", "data": "LoggedOnSoftware" },
{ "title": "Soft No", "data": "LoggedOnSoftwareVerNo" },
{ "title": "Last Reset", "data": "LastResetType" },
{
"title": "Last Log", "data": "LastLogOnTime",
"render": function (LastLogOnTime) {
return (moment(LastLogOnTime).isValid()) ? moment(LastLogOnTime).format("DD MMM YY") : "-";
}
}
],
"order": [[2, 'asc']]
});
}
}
// initialize the datatables
assetListVM.init();
});
答案 0 :(得分:2)
您可以为该列添加columnDef并在那里设置可见性。
"columnDefs": [
{
"targets": [ 0 ], //first column
"visible": @(User.IsInRole("Sausage") : ? "true" : "false")
}
]
设置变量而不是使用内联Razor代码。
var RoleCheck = @(User.IsInRole("Sausage") : ? "true" : "false");
"columnDefs": [
{
"targets": [ 0 ], //first column
"visible": RoleCheck
}
]
https://datatables.net/examples/basic_init/hidden_columns.html