Announcement

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

    Filtering local listgrid with optiondatasource multiple values

    Hello,

    I'm having trouble getting the FetchMode.LOCAL ListGrid filtering to work with multiple valued fields mapped via option data source. Problem is reproduced with following code:

    Code:
        public void onModuleLoad() {
            SC.showConsole();
    
            viewport = new VLayout();
            viewport.setWidth100();
            viewport.setHeight100();
    
            // option data source
    
            final DataSource ods = new DataSource();
            DataSourceIntegerField odsIdField = new DataSourceIntegerField("oid");
            odsIdField.setPrimaryKey(true);
            odsIdField.setHidden(true);
            DataSourceTextField odsTextField = new DataSourceTextField("otext");
            ods.setFields(odsIdField, odsTextField);
            ods.setClientOnly(true);
            ods.setTestData(
                    getOption(1, "option 1"),
                    getOption(2, "option 2")
            );
    
            // data source for actual test data
    
            final DataSource ds = new DataSource();
    
            DataSourceIntegerField dsIdField = new DataSourceIntegerField("id");
            dsIdField.setPrimaryKey(true);
            dsIdField.setHidden(true);
            DataSourceIntegerField dsOptionField = new DataSourceIntegerField("option");
            dsOptionField.setMultiple(true);
            ds.setFields(dsIdField, dsOptionField);
    
            ds.setClientOnly(true);
            ds.setTestData(
                    getRecord(1, new int[]{1}),
                    getRecord(2, new int[]{1, 2})
            );
    
            // local grid with filtering
    
            final ListGrid g = new ListGrid();
            g.setSize("400", "400");
            g.setDataSource(ds);
            g.setShowFilterEditor(true);
            g.setFilterOnKeypress(true);
            g.setCanEdit(true);
            g.setDataFetchMode(FetchMode.LOCAL);
            g.setAutoFetchData(false);
    
            // our listgridfield with option data source, maps option to otext via oid
            ListGridField of = new ListGridField("option");
            of.setOptionDataSource(ods);
            of.setValueField("oid");
            of.setDisplayField("otext");
    
            g.setFields(of);
    
            ds.fetchData(null, new DSCallback() {
                @Override
                public void execute(DSResponse response, Object rawData, DSRequest request) {
                    final ResultSet resultSet = new ResultSet(ds);
                    resultSet.setAllRows(response.getData());
                    g.setData(resultSet);
                }
            });
    
            viewport.addMember(g);
            viewport.draw();
        }
    
        public Record getOption(int id, String text) {
            Record r = new Record();
            r.setAttribute("oid", id);
            r.setAttribute("otext", text);
            return r;
        }
    
        public Record getRecord(int id, int[] options) {
            Record r = new Record();
            r.setAttribute("id", id);
            r.setAttribute("option", options);
            return r;
        }
    The option data source is a simple int->string mapping option data source bound to field in list grid with filtering enabled. What I am aiming at is a simple OR type filtering so that when option 1 is selected from the filter the grid would show all the rows with '1' in the "option" field array. I'm having trouble understanding what the filter actually now filters as the result makes no sense at all. Is this the expected behaviour or might this be a bug ?

    Using Smartgwt 3.1d LGPL NIGHTLY-2012-09-27, Firefox.

    br,
    Marko

    #2
    It looks like you are expecting that a clientOnly DataSource will know what to do with an array of int as a field value, but it doesn't implement any sort of handling for this. This doesn't stop you from implementing such handling with your own DataSource, CLIENT_CUSTOM or server-based.

    Comment


      #3
      Ok thanks, is that an only way to achieve something I'm after ? The test case where int[] is used with clientOnly DS is just a testing template I'm trying to get this to work. In the actual application the records are derived from a restdatasource with a similar mapping of DataSourceIntegerField in multiple configuration. The ListGrid is similarly in FetchMode.LOCAL mode. The application behaves similarly to the testing code.

      I tried setting a foreign key relation from ds.option -> ods.oid (dsOptionField.setForeignKey("ods.oid")) but that has no effect.

      It would be a nice feature for client only filtering to support something like this as I would think this is a quite common approach with multiple fields. For the heck of it I tried patching the DataSource.js fieldMatchesFilter function and appended something like

      Code:
      ...
      if(isc.isAn.Array(fieldValue)  ) {
          for(var i=0; i<fieldValue.length; i++) {
              if(filterValue.contains(fieldValue[i]))
                  return true;
          }
      }
      ...
      .. and it seems to work. You could make an OR compare (like above) when textMatchStyle is "startsWith" or "substring" and an AND-style compare when textMatchStyle is "exact" for sensible defaults.

      Can you give any pointers from where to start with this ? I tried looking through the api documentation and ResultSet class javadoc says:

      Code:
      By default, client-side filtering interprets the Criteria passed to setCriteria(com.smartgwt.client.data.Criteria) as a set of field values that records must match (similarly to the built-in SQL/Hibernate connectors built into the SmartGWT Server). Custom client-side filtering logic can be implemented by overriding ResultSet#applyFilter.
      I have trouble finding the applyFilter method in ResultSet class to override.
      Last edited by markok; 18 Oct 2012, 23:21.

      Comment

      Working...
      X