javagwtsmartgwtlistgrid

Getting visible columns in SmartGWT ListGrid


I am trying to get the number of visible columns in ListGrid and came across the same problem in this question :-

However this does not work. Here is my code:-

      ListGridField firstName=new ListGridField("firstName","First Name");
      ListGridField lastName=new ListGridField("lastName","Last Name");
      ListGridField companyName=new ListGridField("companyName","Company Name");
      ListGridField companyNameHidden=new ListGridField("companyNameHidden","Company Name");
      firstName.setWidth(200);
      lastName.setWidth(200);
      companyName.setWidth(200);
      companyNameHidden.setWidth(200);
      companyNameHidden.setHidden(true);
      listGrid.setFields(firstName,lastName,companyName,companyNameHidden);

      for(ListGridField fieldName:listGrid.getFields()){
             System.out.println(fieldName.getName()+"======="+listGrid.fieldIsVisible(fieldName.getName())); 
      }

The output is :-

firstName=======true
lastName=======true
companyName=======true
companyNameHidden=======true

Shouldn't it be false for last one. The field is clearly hidden. Please suggest how to find number of visible fields, of a ListGrid.


Solution

  • You need to draw the listgrid, before you ask it to tell you, if its fields are visible. Kind of makes sense, since if an element is not drawn, visible state is undetermined - could/should return false IMO, but it returns true at the framework's implementation. If you add: listGrid.draw() before the for loop, you will see that the companyNameHidden is not even printed out, because apparently, getFields returns only the visible fields, after the grid has been drawn. You can just use listGrid.fieldIsVisible(companyNameHidden.getName()), if you are interested for a specific field.