Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Determine if column is hidden by user programmatically

    Hello,

    Using (SC_SNAPSHOT-2012-02-27_v8.2p/PowerEdition Deployment 2012-02-27)

    I could not find the API (and wonder if one exists) for a simple use case:
    Check whether the user has hidden a column using the context menu.

    I'd like to do something like myListGrid.getField("myField").isHidden() which returns true if hidden (by the user using context menu), false otherwise.

    I would like to use this to make an export widget that only exports the visible fields (ie. the user can trim the export to excel by hiding columns in the grid).

    I know this is also possible with excel, but the context menu is far nicer, because it gives a better overview and doesn't require horizontal scrolling for grids with a lot of columns.

    #2
    get/setFieldState() might do what you want if you copy the field state between two grids, but it would copy everything (sizes, etc).

    The other (more difficult) approach would be to wait for fieldStateChanged and look for differences in what fields were hidden before the event happened.

    Comment


      #3
      I successfully used getFieldState(), and I made a little util method like so, for others who want to do the same.
      Feel free to make this method part of ListGrid ;-)
      Code:
      /**
      * Observes the fieldstate of the grid, and returns a list of fieldnames that are currently visible
      * ie. not hidden by the user.
      */
      public static String[] getVisibleFields(ListGrid grid) {
      	List<String> ret = new ArrayList<String>();
      
      	String state = grid.getFieldState();
      	JSONValue val = JSONParser.parseLenient(state);
      	JSONArray arr = val.isArray();
      	for (int i = 0; i < arr.size(); i++) {
      		JSONValue f = arr.get(i);
      
      		JSONObject obj = f.isObject();
      		JSONString name_ = obj.get("name").isString();
      		String name = name_.toString();
      
      		boolean visible = true;
      		if(obj.get("visible") != null) {
      			JSONBoolean visible_ = obj.get("visible").isBoolean();
      			visible = visible_==null || visible_.booleanValue();
      		}
      			
      		if(visible){
      			ret.add(name);
      		}
      	}
      
      	return ret.toArray(new String[0]);
      }
      I then used this result as input to the export feature of SmartGWT, to only export the visible fields.
      Last edited by Sytematic; 15 Jun 2012, 01:04.

      Comment

      Working...
      X