C# 如何动态隐藏 jqgrid 列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19517092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hide a jqgrid column dynamically
提问by Sai Avinash
I am implementing jqgrid in my asp.net MVC web application.
我正在我的 asp.net MVC web 应用程序中实现 jqgrid。
In my grid i have two columns edit and delete. The delete should be visible only if the user is logged as admin .
在我的网格中,我有两列编辑和删除。仅当用户以 admin 身份登录时,删除操作才可见。
How can we dynamically hide.show columns in jqgrid. I am having a session variable to check whether the logged in user is Admin or not.
我们如何在 jqgrid 中动态隐藏.show 列。我有一个会话变量来检查登录用户是否是管理员。
I am accessing that variable in javascript. but, not sure how can i hide/show column in jqgrid
我正在 javascript 中访问该变量。但是,不确定如何在 jqgrid 中隐藏/显示列
Please help..
请帮忙..
采纳答案by Sai Avinash
This one worked:
这个有效:
$("#list").hideCol("ColumnName")
回答by Vinoth Krishnan
Use this code,
使用此代码,
jQuery("#list").jqGrid('hideCol',["colModel1_name","colModel2_name"]);
jQuery("#list").jqGrid('showCol',["colModel1_name","colModel2_name"]);
May this help you.
愿这对你有帮助。
回答by Suwer
This is not the best practice to use js to manage your security. You should not show this column on your server side!
这不是使用 js 管理安全性的最佳实践。您不应在服务器端显示此列!
回答by Mustafa
Newer API
较新的 API
jQuery("#list").jqGrid('hideCol',["ColumnName","ColumnName2"]);
Older API
较旧的 API
$("#list").hideCol("ColumnName")
回答by Mark Carpenter Jr
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.
今天我不得不深入研究一些遗留的东西。其中一项要求是有条件地设置某些列的可见性。页面上有一个下拉菜单,可以在网格的 where 子句中设置类别参数。长话短说,观看下拉菜单的更改事件不可能使此处答案中的大多数方法无效。
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.
虽然我没有测试过。