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

    looking for tips to how to search Listgrid with foreign key field

    Hi, haven't done this in a while, so hoping for some help.

    I have a DataSource with a foreign key field:

    I have a ListGrid with a field using the optiondatasource setup:
    <field name="contractorId" type="integer" foreignKey="" displayField="name" required="false">
                    <fmt:message key="contractorId"/>
    public class FKGridField extends ListGridField {
            public FKGridField(DataSource owningDataSource, DataSourceField dataSourceField, String optionOperationId) {
                //this is a punctuation-separated string that specified the datasource and primary key field of the datasource used by this combobox. For exmaple ""
                String[] foreignKeyElements = FormUtils.getForeignKeyElements(dataSourceField.getForeignKey());
                DataSource optionDataSource = DataSource.get(foreignKeyElements[0]);
                if (optionDataSource == null) {
                    throw new NullPointerException("FK DataSource not found for " + owningDataSource.getID() + ": " + dataSourceField.getName() + ": " + dataSourceField.getForeignKey());
                if (optionOperationId != null) {
    When I set a cell formatter on that field, the "value" is the value of the "name" attribute as defined in the related optiondatasource record.

    Now, my use case is this - I want to search the grid for records that have a "name" attribute in the optiondatasource that corresponds to a search string entered by the user in a popup window..

    If I do grid.getRecords() and check the contractorId, they are of course just the foreign key ID values to a record in the contractor datasource. So how do I traverse over to the contractor datasource to compare the name value there while iterating the rows in the list grid?

    If I am confused, please let me know - again, hoping for help and tips.

    Hi mathias,

    you need to add an additional field for your displayField="name" and do all filtering on this one. As and if this will be joined serverside, this is very easy afterwards

    <field name="name" includeFrom="" />
    Also, as you FK field is not required=true, add joinType="outer" to it.

    If your server for some reason can't do joins I assume this will be more complicated. But as you already have the displayField-definition in your code, I'm pretty sure this will work.

    Best regards


      Hey there Blama. I'm doing my own DAO on the server, what I'm talking about here is how to traverse the grid strictly client-side.