Announcement

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

    Using ListGridField.setShowIfCondition()

    Hi,

    I'm trying to dynamically show/hide a column of a ListGrid using the showIf mechanism.

    There seems to be no effect for returning true or false in the following snippet.
    The column showing this field appears on startup, and remains even after calls to grid.refreshFields().

    Code:
            listGridField.setShowIfCondition(new ListGridFieldIfFunction() {
                @Override
                public boolean execute(ListGrid grid, ListGridField field, int fieldNum) {
                    // return true;
                    return false;
                }
            });
    Technincal details:
    SmartGWT 2.1
    IE 8.0.7600, FF 3.6.12

    Following is the full example code - I was expecting that pressing the "Toggle" button would make the column show/hide, but it doesn't.

    Any insight on what I'm missing here ?

    Thanks!
    Lior

    Code:
    import com.google.gwt.core.client.EntryPoint;
    import com.google.gwt.user.client.ui.RootPanel;
    import com.smartgwt.client.widgets.IButton;
    import com.smartgwt.client.widgets.Label;
    import com.smartgwt.client.widgets.events.ClickEvent;
    import com.smartgwt.client.widgets.events.ClickHandler;
    import com.smartgwt.client.widgets.grid.ListGrid;
    import com.smartgwt.client.widgets.grid.ListGridField;
    import com.smartgwt.client.widgets.grid.ListGridFieldIfFunction;
    import com.smartgwt.client.widgets.grid.ListGridRecord;
    import com.smartgwt.client.widgets.layout.VLayout;
    
    /**
     *
     */
    public class Test implements EntryPoint {
        private ListGrid grid;
        private IButton button;
        private Label label;
        private boolean isShowingDescription;
    
    
        private ListGrid getGrid() {
            final ListGrid grid = new ListGrid();
            grid.setWidth(500);
            grid.setHeight(224);
            grid.setShowAllRecords(true);
            grid.setCanEdit(false);
    
            ListGridField nameField = getNameField();
            ListGridField descField = getDescriptionField();
    
            grid.setFields(nameField, descField);
            grid.setData(getDummyData());
            return grid;
        }
    
        private ListGridField getDescriptionField() {
            isShowingDescription = true;
            ListGridField descField = new ListGridField("desc", "Description");
            descField.setShowIfCondition(new ListGridFieldIfFunction() {
                @Override
                public boolean execute(ListGrid grid, ListGridField field, int fieldNum) {
                    label.setContents(isShowingDescription ? "Showing description" : "Hiding description");
                    return isShowingDescription;
                }
            });
            return descField;
        }
    
        private IButton getButton() {
            IButton btn = new IButton("Toggle");
            btn.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    isShowingDescription = !isShowingDescription;
                    grid.refreshFields();
                    grid.redraw();
                }
            });
            return btn;
        }
    
        //
        //
        //
    
        private ListGridField getNameField() {
            return new ListGridField("name", "Name");
        }
    
        private Label getLabel() {
            Label label = new Label();
            label.setContents(isShowingDescription ? "Showing description" : "Hiding description");
            return label;
        }
    
        private ListGridRecord[] getDummyData() {
            ListGridRecord[] data = new ListGridRecord[]{
                    new Item("One", "Traditionally the 1st natural number"),
                    new Item("Two", "Better than one.")
            };
            return data;
        }
    
        private class Item extends ListGridRecord {
            public Item(String name, String desc) {
                setAttribute("name", name);
                setAttribute("desc", desc);
            }
        }
    
        public void onModuleLoad() {
            VLayout layout = new VLayout();
    
            grid = getGrid();
            button = getButton();
            label = getLabel();
            layout.addMember(grid);
            layout.addMember(label);
            layout.addMember(button);
    
            RootPanel.get().add(layout);
        }
    
    }

    #2
    This code works using the latest build. You can get it from here : http://www.smartclient.com/builds/ or wait for the next official release.

    Please post Smart GWT questions in the Smart GWT forum. We'll move this thread over.

    Comment


      #3
      Thanks,
      I moved to SmartGWT 2.3 and it works.

      Comment


        #4
        Suppose I wish to show/hide a particular column depending on the value of another column in the same row. How do I do it ?

        In the following code, assume that the field1, field2, .. are added to a TreeGrid.

        Code:
        field1.setShowIfCondition(new ListGridFieldIfFunction() {
        
          @Override
          public boolean execute(ListGrid grid, ListGridField field, int fieldNum) {
              if (field1.getAttributeAsString("attributeA").equals(field2.getAttributeAsString("attributeB"))) {
                        	return true;
                        } else {
        				return false;
        			}
                       }
        		});

        Comment


          #5
          Also a bit mystified on how to properly use ListGridFieldIfFunction. Is there any documentation on it? I'm trying to use it to auto-hide a column if none of the records have any values in them for that field, but have had no luck so far (reaches the if statement but that's it):

          Code:
          final ListGridField touchPointField = new ListGridField("touchPoint");
          touchPointField.setShowIfCondition(new ListGridFieldIfFunction() {
          	@Override
          	public boolean execute(ListGrid _grid, ListGridField field, int fieldNum) {
          		RecordList records = grid.getRecordList();
          		for(int i=0; i<records.getLength(); i++) {
          			Record record = records.get(i);
          			if(record != null && record.getAttribute("touchPoint") != null) {
          				return true;
          			}
          		}
          		
          		return false;
          	}
          });

          Comment


            #6
            Your setShowIfCondition code work for me with minimum change. Just call listGrid.refreshFields(); after listGrid.setData();
            As setShowIfCondition doc says
            "Evaluated on initial draw, then reevaluated on explicit calls to listGrid.refreshFields() or listGrid.setFields()."

            Thanks
            Minhaj

            Comment

            Working...
            X