smartgwtlistgrid

How do i select a record in a grouped smartgwt listgrid?


I have site with a listgrid and a openlayers map with points. When i cklick on one of these, the application shall scroll and mark this record. This works with a standard listgrid, but with a grouped listgrid it does not work.

lg = new ListGrid();
lg.setWidth(330);
lg.setDataSource(ds1);
lg.setAutoFetchData(true);
lg.setSortField("KU_NAME");
lg.setGroupStartOpen(GroupStartOpen.ALL);
lg.setGroupByField("KU_NAME");
lg.setShowFilterEditor(true);

kuName = new ListGridField("KU_NAME", "Künstler Name",150);

// Standorte
ListGridField stdOrt = new ListGridField("STDORT_NR","Standort Nr.");
ListGridField oid = new ListGridField("OID","OID.");
lg.setFields(stdOrt,kuName,oid);

and the select:

String stdortOID = stdOrtOIDjso.toString();
ListGridRecord[] records = lg.getRecords();
    int i;
    for (i = 0; i < records.length; i++) {
        if (records[i].getAttribute("OID").equalsIgnoreCase(stdortOID)){
            break;
    }
}
lg.deselectAllRecords();
lg.selectRecord(i);
lg.scrollToRow(lg.getRecordIndex(record));

the reason is that in the record is only the value of the group name and the other attributs are unavailable.


Solution

  • When grouping is enabled, all data are "transformed" into tree and listgrid itself contains data for groups so you have to look for your record in this tree. Replace last 3 lines with (modified) Vittorio Paternostro suggestion:

    Tree tree = lg.getGroupTree();
    if (tree != null) {
        TreeNode node = tree.find("OID", stdortOID);
        if (node != null) {
            lg.selectSingleRecord(node);
            lg.scrollToRow(getRecordIndex(node));
            lg.markForRedraw();
        }
    }
    

    Note: Instead of deselectAllRecords + selectRecord use simplified selectSingleRecord.