Login Register

issue in hiding column in grid

Hi,

I have a requirement wherein I have to hide a column from my grid layout. This is the grid layout I am using,

var layout = [{"cells":[[
{cellStyles:"visibility:hidden",get:renderChkbox,headerStyles:"visibility:hidden",height:"5",name:"Select",width:"8"},
{field:"field1",height:"5",name:"Column1",width:"10"},
{field:"field2",height:"5",name:"Column2",width:"12"},
{field:"field3",height:"5",name:"Column3",width:"5"}
]]}];
dijit.byId('grid').setStructure(layout);

renderChkbox : function(inRowIndex){
return '';
}

When I use this layout, my column 'Select' gets hidden and I do not see it in the grid. On certain condition, I want to display this column, for which I am using the following code:

dijit.byId('grid').getCell(0).headerStyles = 'visibility:visible;';
dijit.byId('grid').getCell(0).cellStyles = 'visibility:visible;';

The issue is that after this I can see column header but the checkboxes in data part are not visible. Also, the data is not in synch with headers i.e. I see data for column 'Column1' under 'Select', for 'Column2' under 'Column1' and so on...Once I resize my columns then everything becomes normal.

Can someone please let me know what am I doing wrong here or if there is any other way to hide a column.

Thanks.

Column hiding isn't built

Column hiding isn't built into the grid, and setting the CSS visibility property definitely isn't the way you're going to want to implement it. I've gotten column hiding and and column drag-and-drop to work by calling the grid.setStructure method with a dynamically generated structure object. Something like this might work:

var fields = [
    {cellStyles:"visibility:hidden",get:renderChkbox,
      headerStyles:"visibility:hidden",height:"5",name:"Select",width:"8"},
    {field:"field1",height:"5",name:"Column1",width:"10"},
    {field:"field2",height:"5",name:"Column2",width:"12"},
    {field:"field3",height:"5",name:"Column3",width:"5"}
];

function makeStructure(visibilities) {
    var row = [];
    for (var i = 0; i < visibilities.length; i++) {
        if(visibilities[i]) {
            row.push(fields[i]);
        }
    }
    return [{cells:[row]];
}

And then, to hide the first column, call

dijit.byId('grid').setStructure(makeStructure([false, true, true, true]));

and to show it, call

dijit.byId('grid').setStructure(makeStructure([true, true, true, true]));

you can use display:none;

instead of visibility:hidden, you can use display:none; in your cellStyles attribute. IE and Firefox do behave differently for the column index. Firefox still includes the hidden columns in your index count, IE skips those hidden columns.

Hiding Column Suggestions Shown Above Do Not Appear to Work

Consider the following two-column grid;

var dynamicView = {
cells: [
[
{name: "Dynamic Structure Person", field: "fname", width:"50%"},
{name: "City", field: "city", cellStyles:"display:none", headerStyles:"display:none" }
]
]
};

var initialDynamicStructure = [dynamicView];

Here is the associated code in the jsp:

<span dojoType="dojox.grid.data.DojoData" jsId="dynamicModel"
store="globalDataStore5"
query="{fname: '*' }" clientSort="true">
</span>

<div dojoType="dojox.grid.Grid" id="dynamicGrid"
model="dynamicModel" structure="initialDynamicStructure"
rowsPerPage="20" class="anygrid" >
</div>

With the above setup, both columns of data are displayed, despite the display:none cellStyles and headerStyles. I have also tried visibility:hidden for both attributes, but again both columns are displayed. So I am unable to cause a designated column to be initially hidden.

Then I try to dynamically change the visibility of the city column. There are two HTML buttons, as follow:

<form>
<button type="button" onclick="displayCityColumn('dynamicGrid', true);">Show City Column</button>
<button type="button" onclick="displayCityColumn('dynamicGrid', false);">Hide City Column</button>
</form>

Here is the invoked JavaScript method displayCityColumn:

function displayCityColumn(gridId, cityVisibility) {
var grid = dijit.byId(gridId);
var row = []; //Note row is an array
row.push(dynamicView.cells[0][0]); //Push one field-level struct into row array - for fname

if (cityVisibility) {
alert("Setting cityVisibility on");
row.push( {name: "City", field: "city"} ); //Push one field-level struct into row array - for city
}
else {
alert("Setting cityVisibility off");
row.push( {name: "City", field: "city", cellStyles:"visibility:hidden", headerStyles:"visibility:hidden"} );
}

var dynamicStructure = {cells:[row] }; //The cells instance variable is an array of array of field-level structs

grid.setStructure(dynamicStructure);
grid.refresh();
}

Pressing either of the buttons "Show City Column" or "Hide City Column" causes the entire grid area to become grey colored, and empty.

I did not try display:none in this JavaScript method, because I did not know what the opposite value should be - perhaps display:inline or display:something else.

But in any event the pair of command buttons did not work either.

Does anyone see an error in the above code?

To summarize, I could not find a way that actually works in practice to initially hide a column, nor to dynamically change the visibility of a column.

Any help that actually works, as opposed to may work in theory, would be very much appreciated.

Thank you very much.

Rather try just omitting or including the column definition

If you are defining a new structure each time you change the visibility, there is no need to try to use visibility: hidden styles, rather just leave the column out of the grid entirely. Your grid does not need a column for every field in your data source.