Saved Search (Smart GWT)

Description

This example demonstrates how to serialize and use criteria filters for a ListGrid.

You must perform the following steps:

  • Create a DataSource for storing the saved search data. Also, create another item DataSources (supplyItem DataSource).
  • Add all DataSources to the application.
  • Create a schema and import data for the created DataSources.
  • Create the  main UI with ListGrid (supplyItem DataSource) and with a saved search filter.
  • Create the saved search UI.

A working example is available in the attachment.

Create a Saved Search DataSource

Create data source
<DataSource ID="savedSearch" serverType="sql" tableName="savedSearch" titleField="name">
    <fields>
        <field name="id" type="sequence" hidden="true" primaryKey="true" />
        <field name="type" type="text" length="255" hidden="true" required="true" />
        <field name="name" type="text" title="Name" length="255" required="true" />
        <field name="criteria" type="text" hidden="true" length="4000" required="true" />
    </fields>
</DataSource>

You need to add the DataSource to the application, and create a schema in the database (you can  use the "Smart GWT Admin Console" to do this). Please refer to the "Smart GWT Quick Start Guide" ("Data Binding" chapter under the "DataSources" section).

Create main UI

The ListGrid can use any DataSource. We will use the "supplyItem" DataSource.

Create ListGrid
private ListGrid createListGrid() {
    ListGrid result = new ListGrid();
    result.setHeight(450);
    result.setCanEdit(false);
    result.setDataSource(supplyItemDataSource);
    result.setSortField("itemID");

    return result;
}

The saved search filter uses the "savedSearch" DataSource filtering data by type. It uses the "supplyItem" DataSource ID as the type value (ComboBoxItem form field).

Create filter
private DynamicForm createFilterForm() {
    DynamicForm result = new DynamicForm();

    nameComboBoxItem = new ComboBoxItem();
    nameComboBoxItem.setDisplayField("name");
    nameComboBoxItem.setValueField("id");
    nameComboBoxItem.setTitle("Filter");
    nameComboBoxItem.setWidth(200);
    nameComboBoxItem.setEmptyDisplayValue("All Records");
    nameComboBoxItem.setShowAllOptions(true);
    nameComboBoxItem.setOptionDataSource(savedSearchDataSource);
    nameComboBoxItem.setFetchMissingValues(false);
    nameComboBoxItem.setCachePickListResults(false);

    // filter data by item datasource
    AdvancedCriteria criteria = new AdvancedCriteria("type", OperatorId.EQUALS, dataSource.getID());
    nameComboBoxItem.setOptionCriteria(criteria);

    nameComboBoxItem.addChangedHandler(new ChangedHandler() {
        @Override
        public void onChanged(ChangedEvent event) {
            ComboBoxItem item = (ComboBoxItem) event.getItem();
            ListGridRecord record = item.getSelectedRecord();

            if (record == null) {
                updateSavedSearch.disable();
                removeSavedSearch.disable();
            } else {
                updateSavedSearch.enable();
                removeSavedSearch.enable();
            }

            if (record != null) {
                Criteria newCriteria = new AdvancedCriteria(JSON.decode(record.getAttribute("criteria")));
                listGrid.fetchData(newCriteria);
            } else {
                listGrid.fetchData();
            }
        }
    });

    result.setFields(nameComboBoxItem);

    return result;
}

Create saved search UI

To create/update the saved search a modal window is used. It contains a form (DynamicForm) for editing data ("savedSearch" DataSource). To dynamically create filtering rules use a FilterBuilder with the "supplyItem" DataSource.

Create form
private DynamicForm createForm() {
    DynamicForm result = new DynamicForm();
    result.setWidth(700);
    result.setMargin(5);
    result.setUseAllDataSourceFields(true);
    result.setDataSource(dataSource);

    HeaderItem headerItem = new HeaderItem();
    headerItem.setDefaultValue("New search");

    TextItem nameItem = new TextItem("name");

    CanvasItem criteriaItem = new CanvasItem("criteriaFilter", "Criteria");
    filter = new FilterBuilder();
    filter.setDataSource(itemDataSource);
    criteriaItem.setCanvas(filter);

    ButtonItem submitItem = new ButtonItem("submit", "Search");
    submitItem.addClickHandler((ClickHandler) new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            JSONEncoder encoder = new JSONEncoder();
            encoder.setDateFormat(JSONDateFormat.DATE_CONSTRUCTOR);
            String criteriaValue = JSON.encode(filter.getCriteria().getJsObj(), encoder);
            form.setValue("criteria", criteriaValue);

            form.setValue("type", itemDataSource.getID());

            form.submit(new DSCallback() {
                @Override
                public void execute(DSResponse response, Object rawData, DSRequest request) {
                    EditSavedSearchWindow.this.record = response.getData()[0];
                    if (updateSavedSearchCallback != null) {
                        updateSavedSearchCallback.onUpdate();
                    }
                    hide();
                }
            });
        }
    });

    result.setFields(headerItem, nameItem, criteriaItem, submitItem);

    return result;
}
Sample updateSavedSearchCallback on main UI
private void onUpdateSavedSearch(Record record) {
    nameComboBoxItem.setValue(record.getAttributeAsInt("id"));
...
    Criteria criteria = new AdvancedCriteria(JSON.decode(record.getAttribute("criteria")));
    listGrid.setCriteria(criteria);
    listGrid.fetchData(criteria);
}

Attachment

  File Modified

ZIP Archive SavedSearch.zip

Mar 08, 2012 by Sergey Kiselev