Announcement

Collapse
No announcement yet.
X
  • 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:
    Code:
    <field name="contractorId" type="integer" foreignKey="contractor.id" displayField="name" required="false">
                <title>
                    <fmt:message key="contractorId"/>
                </title>
            </field>
    Code:
    public class FKGridField extends ListGridField {
    
            public FKGridField(DataSource owningDataSource, DataSourceField dataSourceField, String optionOperationId) {
    
                setName(dataSourceField.getName());
    
                //this is a punctuation-separated string that specified the datasource and primary key field of the datasource used by this combobox. For exmaple "location.id"
                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());
                }
                setOptionDataSource(optionDataSource);
                setTitle(optionDataSource.getTitle());
    
                setValueField(foreignKeyElements[1]);
    
                if (optionOperationId != null) {
                    setOptionOperationId(optionOperationId);
                }
            }
        }
    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.

    #2
    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

    Code:
    <field name="name" includeFrom="contractor.name" />
    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
    Blama

    Comment


      #3
      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.

      Comment

      Working...
      X