15

我在我的 asp.net MVC Web 应用程序中实现 jqgrid。

在我的网格中,我有两列编辑和删除。仅当用户以 admin 身份登录时,删除才应可见。

我们如何在 jqgrid 中动态 hide.show 列。我有一个会话变量来检查登录用户是否是管理员。

我在 javascript 中访问该变量。但是,不知道如何在 jqgrid 中隐藏/显示列

请帮忙..

4

5 回答 5

20

使用此代码,

jQuery("#list").jqGrid('hideCol',["colModel1_name","colModel2_name"]);
jQuery("#list").jqGrid('showCol',["colModel1_name","colModel2_name"]);

愿这对你有所帮助。

于 2013-10-22T11:56:14.233 回答
15

这个有效:

$("#list").hideCol("ColumnName")
于 2013-10-22T12:59:57.690 回答
11

较新的 API

jQuery("#list").jqGrid('hideCol',["ColumnName","ColumnName2"]);

较旧的 API

$("#list").hideCol("ColumnName")
于 2014-06-04T23:26:25.273 回答
2

这不是使用 js 管理安全性的最佳实践。您不应该在服务器端显示此列!

于 2013-11-27T10:50:28.620 回答
1

I had to dive into some legacy stuff today. One of the requirements was to conditionally set the visibility of some columns. There was a drop down on the page that set a category parameter in the where clause for the grid. Long story short, watching the change event of the drop down wasn't possible making most of the methods in the answers here invalid.

I was able to use a ternary in the hidden param to set the visibility.

{
    name: 'mfg',
    index: 'mfg',
    width: 150,
    sortable: true,
    hidden: $('#evCategory').val() == 'Calibration' ? false : true
},

And it simply evals to if the dropdown has a value of calibration hidden should be false, if calibration is not the value hidden should be true.

Might also be able to shorten it to;

!$('#evCategory').val() == 'Calibration' || true

Although I haven't tested that.

于 2018-07-24T19:24:56.157 回答