Home How to conditionally draw DataTables columns in Razor pages
Reply: 0

How to conditionally draw DataTables columns in Razor pages

user3702
1#
user3702 Published in April 22, 2018, 6:06 pm

Currently we are doing this to draw the columns for a server side DataTable for 2 different users, we were able to hide the column headers using razor code to only draw them when the user is an admin or group admin. However when it comes to drawing the data we are having an issue with the following:

function getTableColumns() {
            var allowedToDelete = @(User.IsInRole("SysAdmin") || GroupManager.IsUserGroupAdmin(Model.GroupId, User.FindFirst(ClaimTypes.NameIdentifier).Value) ? "true" : "false");

            if (allowedToDelete) {
                return [{ "defaultContent": "" },
                { "data": "deadlineDate", "name": "DeadlineDate" },
                { "data": "priority", "name": "Priority" },
                { "data": "jobNumber", "name": "JobNumber" },
                { "data": "project", "name": "Project" },
                { "data": "description", "name": "Description" },
                { "data": "reference", "name": "Reference" },
                { "data": "subReference", "name": "SubReference" },
                { "data": "employee", "name": "Employee" },
                { "data": "allocatedTo", "name": "AllocatedTo" },
                {
                    "render": function (data, type, full, meta) {
                        return "<i title ='Archive Item' class='fa fa-archive table-icon-view' onclick='archiveJob(\""
                            + full.groupId + "\",\"" + full.id + "\")'></i>";
                    }
                },
                {
                    "render": function (data, type, full, meta) {
                        return "<i title ='Edit' class='fa fa-pencil table-icon-edit' onclick='editJob(\""
                            + full.groupId + "\",\"" + full.id + "\")'></i>";
                    }
                },
                {
                    "render": function (data, type, full, meta) {
                        return "<i title ='Delete' class='fa fa-trash table-icon-delete' onclick='showDeleteModal(\""
                            + full.groupId + "\",\"" + full.id + "\")'></i>";
                    }
                },
                { "data": "issueDate" },
                { "data": "startDate" },
                { "data": "createdBy" },
                { "data": "createdDate" },
                { "data": "notes" }];
            } else {
                return [{ "defaultContent": "" },
                { "data": "deadlineDate", "name": "DeadlineDate" },
                { "data": "priority", "name": "Priority" },
                { "data": "jobNumber", "name": "JobNumber" },
                { "data": "project", "name": "Project" },
                { "data": "description", "name": "Description" },
                { "data": "reference", "name": "Reference" },
                { "data": "subReference", "name": "SubReference" },
                { "data": "employee", "name": "Employee" },
                { "data": "allocatedTo", "name": "AllocatedTo" },
                {
                    "render": function (data, type, full, meta) {
                        return "<i title ='Edit' class='fa fa-pencil table-icon-edit' onclick='editJob(\""
                            + full.groupId + "\",\"" + full.id + "\")'></i>";
                    }
                },
                { "data": "issueDate" },
                { "data": "startDate" },
                { "data": "createdBy" },
                { "data": "createdDate" },
                { "data": "notes" }];
            }
        }

Is there a way to optimise this so that we don't have to repeat the code just to hide 2 icon links?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.5496 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO