Announcement

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

    Storing / Saving search criteria from filterbuilder

    Hi there,

    What would be the best way to store a search criteria? I want a user to be able to save it's search operation (search criteria) and later on load it again.

    I was thinking of just storing the AdvancedCriteria.getJsObj(), but I can't seem to find a way to load them again.

    Any ideas / thoughts?

    Regards,

    Kees.

    #2
    Use JSON encode/decode, storing it as a String.

    Comment


      #3
      Hi there,

      Thanks for the help so far... but I think I now found a smartgwt bug.

      If you run the attached sample project, you'll see it will try to save/load the search criteria.

      Everything seems to go find UNTIL the json object is set to the filterbuilder criteria, then the date that should be search for is messed up.

      To reproduce follow these steps:

      1. In the filterbuilder select "Nationhood" as the field to search for.
      2. In the date field, search for a date OTHER THAN TODAY.
      3. Press "save" button, this will show the decoded JSON on the console
      4. Press "load" button, this will show the encoded/decoded JSON again.
      5. The date searched for is different in the filterbuiler!

      Please let me know how I can solve/workaround this, as this should go into production soon.

      Regards,

      Kees.

      stand alone test case:
      Code:
      package com.kvb.client;
      
      import com.google.gwt.core.client.EntryPoint;
      import com.google.gwt.core.client.JavaScriptObject;
      import com.google.gwt.i18n.client.NumberFormat;
      import com.smartgwt.client.data.AdvancedCriteria;
      import com.smartgwt.client.data.DataSource;
      import com.smartgwt.client.types.ListGridFieldType;
      import com.smartgwt.client.util.JSON;
      import com.smartgwt.client.widgets.IButton;
      import com.smartgwt.client.widgets.events.ClickEvent;
      import com.smartgwt.client.widgets.events.ClickHandler;
      import com.smartgwt.client.widgets.form.FilterBuilder;
      import com.smartgwt.client.widgets.grid.CellFormatter;
      import com.smartgwt.client.widgets.grid.ListGrid;
      import com.smartgwt.client.widgets.grid.ListGridField;
      import com.smartgwt.client.widgets.grid.ListGridRecord;
      import com.smartgwt.client.widgets.layout.VStack;
      
      /**
       * Entry point classes define <code>onModuleLoad()</code>.
       */
      public class Smartgwtsupport implements EntryPoint {
      
      	private String jsObj = "";
      
      	/**
      	 * This is the entry point method.
      	 */
      	public void onModuleLoad() {
      		DataSource worldDS = WorldXmlDS.getInstance();
      
      		final FilterBuilder filterBuilder = new FilterBuilder();
      		filterBuilder.setDataSource(worldDS);
      
      		final ListGrid countryGrid = new ListGrid();
      		countryGrid.setWidth(550);
      		countryGrid.setHeight(224);
      		countryGrid.setDataSource(worldDS);
      		countryGrid.setAutoFetchData(true);
      
      		ListGridField nameField = new ListGridField("countryName", "Country");
      		ListGridField continentField = new ListGridField("continent",
      				"Continent");
      		ListGridField memberG8Field = new ListGridField("member_g8",
      				"Member G8");
      		memberG8Field.setCanEdit(false);
      
      		ListGridField populationField = new ListGridField("population",
      				"Population");
      		populationField.setType(ListGridFieldType.INTEGER);
      		populationField.setCellFormatter(new CellFormatter() {
      			public String format(Object value, ListGridRecord record,
      					int rowNum, int colNum) {
      				if (value == null)
      					return null;
      				try {
      					NumberFormat nf = NumberFormat.getFormat("0,000");
      					return nf.format(((Number) value).longValue());
      				} catch (Exception e) {
      					return value.toString();
      				}
      			}
      		});
      		ListGridField independenceField = new ListGridField("independence",
      				"Independence");
      		independenceField.setType(ListGridFieldType.DATE);
      		countryGrid.setFields(nameField, continentField, memberG8Field,
      				populationField, independenceField);
      
      		IButton filterButton = new IButton("Filter");
      		filterButton.addClickHandler(new ClickHandler() {
      			public void onClick(ClickEvent event) {
      				countryGrid.filterData(filterBuilder.getCriteria());
      			}
      		});
      
      		VStack vStack = new VStack(10);
      		vStack.addMember(filterBuilder);
      		vStack.addMember(filterButton);
      		vStack.addMember(countryGrid);
      
      		/* CUSTOM STUFF STARTS HERE */
      		IButton saveButton = new IButton("save");
      		saveButton.addClickHandler(new ClickHandler() {
      
      			public void onClick(ClickEvent event) {
      				jsObj = JSON.encode(filterBuilder.getCriteria().getJsObj());
      				System.out.println("SAVED JSON:");
      				System.out.println(jsObj);
      			}
      		});
      		vStack.addMember(saveButton);
      
      		IButton loadButton = new IButton("load");
      		loadButton.addClickHandler(new ClickHandler() {
      
      			public void onClick(ClickEvent event) {
      				JavaScriptObject jso = JSON.decode(jsObj);
      
      				System.out.println("LOADED JSON:");
      				System.out.println(JSON.encode(jso));
      				AdvancedCriteria ac = new AdvancedCriteria(jso);
      				filterBuilder.setCriteria(ac);
      				filterBuilder.redraw();
      			}
      		});
      		vStack.addMember(loadButton);
      
      		/* CUSTOM STUFF ENDS HERE */
      		vStack.draw();
      
      	}
      }

      Comment


        #4
        Please post all relevant versions (see the FAQ) and be sure to try the latest nightly build if you are testing with a nightly build.

        Comment


          #5
          GWT: 2.0.3
          Smart GWT: 2.1 Enterprise Edition PRO
          Issue occured in both hosted as deployed mode.
          Browser: IE6, Firefox 3.5 and Chrome (latest) all on Windows XP

          I'm not going to use the nightly built stuff, since this will go into production shortly. Proving that it works with a nightly build won't make my problem go away.

          Comment


            #6
            See the docs for JSONEncoder.dateFormat - use "dateConstructor" to allow correct round-tripping.

            Note that you are not marked as having a license in our system - please contact whoever you received your download links from to make sure this forums account is properly marked (or start using the account that is marked as licensed, if it's a separate one).

            Comment


              #7
              There was a bug in JSON.encode(JavaScriptObject object, JSONEncoder settings) which has been fixed. It will be present in the next nightly.

              After the fix, the following code from your testcase works as expected.

              Code:
              IButton saveButton = new IButton("save");
              saveButton.addClickHandler(new ClickHandler() {
              
                  public void onClick(ClickEvent event) {
                      JSONEncoder settings = new JSONEncoder();
                      jsonEncoder.setDateFormat(JSONDateFormat.DATE_CONSTRUCTOR);
                      jsObj = JSON.encode(filterBuilder.getCriteria().getJsObj(), settings);
                      System.out.println("SAVED JSON:");
                      System.out.println(jsObj);
                  }
              });
              Sanjiv

              Comment


                #8
                Fix seems to work. Thanks!

                Any thought on when 2.2 Enterprise will be released?

                Comment

                Working...
                X