asp.net mvc - MVC 5 & Jquery DataTables Column display based on user Role -
using jquery datatables in mvc 5 project. using datatables.mvc5 helper making whole task easier , followed this tutorial set mvc server side processing.
i trying display button columns i.e. edit | delete | first 2 columns based on user role. these buttons call normal mvc controller.
i can create buttons, can not work out how handle role checking. previous client side easy razor syntax, lot more tricky using datatables.
my current table.
$(function () { assetlistvm = { dt: null, init: function () { dt = $('#assetdatatable').datatable({ "serverside": true, "processing": true, "displaylength": 25, "ajax": { "type": "post", // required change 'get' produced server error query string length long. "url": "@url.action("get","demo")", "data": // allows return data object controller function (d) { d.excelexport = $("#excelexport").val(); } }, "scrolly": viewheight, // calculated ensure table fitting max area of screen 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 need check user role , choose if display column or not. // 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 datatables assetlistvm.init(); });
in column titled buttons column have tried razor syntax not working unlike previous tables dummys projects html based. send viewbag object current user role view , check against that, not know how perform if/else decissions within table setup.
if has examples of grips or can point me in right direction appreciated.
i have searched , checked closest question found this old one.
update: jamie
here new working code, based on current users role hide first column.
var rolecheck = @(user.isinrole("sausage") ? "true" : "false"); // new check user role outside of jquery datatable function work. $(function () { assetlistvm = { dt: null, init: function () { dt = $('#assetdatatable').datatable({ "serverside": true, "processing": true, "ajax": { "type": "post", // required change 'get' produced server error query string length long. "url": "@url.action("get","demo")", "data": // allows return data object 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 datatables assetlistvm.init(); });
you might able add columndef column , set visibility there.
"columndefs": [ { "targets": [ 0 ], //first column "visible": @(user.isinrole("sausage") : ? "true" : "false") } ]
setting variable instead of using inline razor code.
var rolecheck = @(user.isinrole("sausage") : ? "true" : "false"); "columndefs": [ { "targets": [ 0 ], //first column "visible": rolecheck } ]
https://datatables.net/examples/basic_init/hidden_columns.html
Comments
Post a Comment