[英]how to show/ hide column in a extjs 3 grid panel
I have a grid panel i need to show / hide columns in a grid panel depending on the value of a checkbox. 我有一个网格面板我需要显示/隐藏网格面板中的列,具体取决于复选框的值。 If the checkbox is checked i need to display column in the grid and if it is unchecked i need to hide the column in the grid.
如果选中该复选框,我需要在网格中显示列,如果未选中,我需要隐藏网格中的列。
Here is my code 这是我的代码
var chkEnableDisplayResponsibilityForAction = '<%=Session["chkEnableDisplayResponsibilityForAction"]%>';
var flags = Boolean.parse(chkEnableDisplayResponsibilityForAction);
var flags1 = !Boolean.parse(chkEnableDisplayResponsibilityForAction)
var colModel = new Ext.grid.ColumnModel([
{ header: "PricePlanID", width: 100, sortable: true, dataIndex: 'PricePlanID', hidden: flags, hideable: flags1 },
]);
when i refresh the page i am not able to toggle the columns depending on the value of the checkbox. 当我刷新页面时,我无法切换列,具体取决于复选框的值。 But when i login and log out i am able to see the changes in the grid panel.
但是当我登录并注销时,我能够看到网格面板中的更改。 Can anyone help me in refreshing the column values in the grid panel?
任何人都可以帮我刷新网格面板中的列值吗?
if take a look at the ExtJS API, particulary the ColumModel there is a setHidden
method, it would hide/show a column in a GridPanel
. 如果看一下ExtJS API,特别是ColumModel有一个
setHidden
方法,它会隐藏/显示GridPanel
一列。
myGrid.getColumnModel().setHidden(0, true);
you should also hook the onchange
event of your check box so you can show or hide the column 您还应该勾选复选框的
onchange
事件,以便显示或隐藏列
In Ext JS 4.1, to hide a column, you use: 在Ext JS 4.1中,要隐藏列,请使用:
grid.columns[0].setVisible(false);
Looks like getColumnModel() with its setHidden() method is no longer part of the grid: http://docs.sencha.com/ext-js/4-1/#!/api/Ext.grid.Panel 看起来getColumnModel()及其setHidden()方法不再是网格的一部分: http ://docs.sencha.com/ext-js/4-1/#!/api/Ext.grid.Panel
You can show/hide columns using column header menu - you can choose which column you want to have shown. 您可以使用列标题菜单显示/隐藏列 - 您可以选择要显示的列。 Anyway, if you want to show/hide a column, try this
无论如何,如果你想显示/隐藏一列,试试这个
myGrid.getColumnModel().setHidden(0, true);
In ExtJS 4 gain access to your grid panel, and then access the columns attribute which is an array of Column objects. 在ExtJS 4中获取对网格面板的访问权限,然后访问columns属性,该属性是Column对象的数组。
From there you can use the array iterator methods ( http://www.diveintojavascript.com/core-javascript-reference/the-array-object ) to perform an action. 从那里,您可以使用数组迭代器方法( http://www.diveintojavascript.com/core-javascript-reference/the-array-object )来执行操作。
In the below example I hide and show a few of the columns based on their header names, but you can obviously perform action based on any Column attribute. 在下面的示例中,我隐藏并根据其标题名称显示一些列,但您显然可以根据任何Column属性执行操作。
var grid = Ext.getCmp( 'my_grid_panel' );
grid.columns.forEach( function( col ) {
if( ( col.text == "Foo" ) || ( col.text == "Bar" ) ) {
col.setVisible( true );
} else if( col.text == "Baz" ) {
col.setVisible( false );
}
});
The answers above I think are pretty good. 我认为上面的答案非常好。 But let me give you a advice.
但是,让我给你一个建议。
1) In ExtJS 4.x it is recommended to use Ext.ComponentQuery `s methods instead of Ext.getCmp() 1)在ExtJS 4.x中 ,建议使用Ext.ComponentQuery的方法而不是Ext.getCmp()
2) To hide/show columns of the grid you can use following code 2)要隐藏/显示网格列,您可以使用以下代码
Ext.ComponentQuery.query('grid gridcolumn[dataIndex^="service"]')[0].hide()
or to show 或者表明
Ext.ComponentQuery.query('grid gridcolumn[dataIndex^="service"]')[0].show()
It should resolve hiding/showing any column in a grid. 它应该解决隐藏/显示网格中的任何列。
Here grid is your grid , it maybe id or xtype etc. 这里的网格是你的网格,它可能是id或xtype等。
setVisibleColumn : function(name, flag) {
name = Ext.Array.from(name);
var column;
for (var i = 0; i < name.length; i++) {
column = this.getColumn(name[i]);
if (column) {
flag ? column.show() : column.hide();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.