Redefine lookupfield of dataGrid with EditorOpenEvent

Hi,

I have a lookupfield « A » that should display values from entity « B » filtered on value from column « C ».

screenDataGrid

In my example, if user is on line 3 lookupfield should only display values from entity « B » where « B.filterColumn » = « C » ; In this case it should only display line where B.filterColumn = 20.
If user is on line 2, it should only display line where B.filterColumn = 10.

My code looks following :

@Subscribe("contratsDataGrid")
protected void onCustomersDataGridEditorOpen(DataGrid.EditorOpenEvent event) {

    Contrat contrat =contratsDataGrid.getEditedItem();
    compteFraisAccessoiresDl.setParameter("q_c",contrat.getC());
    compteFraisAccessoiresDl.load();
    ArrayList<B> compteFraisAccessoireList =  new ArrayList<>(compteFraisAccessoiresDl.getContainer().getItems());
    LookupField<B> lookupField = uiComponents.create(LookupField.NAME);

    // A = compte_COFI
    contratsDataGrid.getColumnNN("A").setEditFieldGenerator(cpterEditorField -> {
        lookupField.setValueSource((ValueSource<CompteFraisAccessoire>) cpterEditorField
                .getValueSourceProvider().getValueSource("A"));
        lookupField.setOptionsList(compteFraisAccessoireList);

        return lookupField;
    });

}

The xml for the collection :

<data>
    <instance id="contratInstance" class="ch.abacus.abatef.entity.Contrat" view="view_cpte"/>
  //B = CompteFraisAccessoire
    <collection id="compteFraisAccessoiresDc" class="ch.abacus.abatef.entity.B" view="_base">
        <loader id="compteFraisAccessoiresDl">
            <query><![CDATA[select e from abatef_CompteFraisAccessoire e where e.C = :q_c]]></query>
        </loader>
    </collection>

    <collection id="contratsDc" class="ch.abacus.abatef.entity.Contrat" view="view_cpte">
        <loader id="contratsDl">
            <query><![CDATA[select d from abatef_Contrat c]]></query>
        </loader>
    </collection>

</data>


In my datagrid, when user double-click on the line 3, the value of C is 20 but the lookupfield is empty. Then, user double-click on the line 2, the value of C is 10 but the lookupfield displays lines where B.filterColumn = 20.

Best Regards

Nicolas

Hi,

The problem is that you set EditFieldGenerator inside EditorOpenEvent handler. This handler is intended to customize editor components, e.g. add listeners, change some attributes etc. This means that when this handler is called, all editor components are created and can’t be changed, as the result the component you set as an edit field for the column A is used when the editor is opened next time.

Also, I can see that you use the compteFraisAccessoiresDc's loader only to load data, i.e. you don’t use compteFraisAccessoiresDc at all. In this case I would recommend either create a service that loads entities or use compteFraisAccessoiresDc as options:

lookupField.setOptions(new ContainerOptions<>(compteFraisAccessoiresDc))

Regards,
Gleb

1 Like

Hi Gleb,

Thank you ! That solved the problem.

Best regards,
Nicolas