Different behavior on setting initial value when creating items

I have a table defined and want to set an initial value on newly created items depending on the filter (through datasource) that is applied to the table. When I do so, it works perfectly when creating a new item on right-clicking the table and select the create option in the menu that is shown.
When using the create button however, the initial value is not set.

This is my table definition:


              <table id="chaptersTable"
                       editable="true"
                       width="100%">
                    <actions>
                        <action id="create"/>
                        <action id="edit"/>
                        <action id="remove"/>
                    </actions>
                    <columns>
                        <column id="chapterBook"
                                editable="true"
                                width="200"/>
                        <column id="chapterOrder"
                                editable="true"
                                width="75"/>
                        <column id="chapterTitle"
                                editable="true"/>
                        <column id="chapterIcon"
                                editable="true"
                                width="100"/>
                    </columns>
                    <rows datasource="chaptersDs"/>
                  <buttonsPanel id="buttonsPanel"
                                alwaysVisible="true">
                      <button caption="msg://create"
                              icon="font-icon:PLUS"
                              action="chaptersTable.create"/>
                      <button id="saveBtn"
                              caption="msg://save"
                              invoke="onSaveBtnClick"/>
                  </buttonsPanel>
                </table>

This is the init from the screen controller:


    @Override
    public void init(Map<String, Object> params) {
        // Get chapters for specific book
        chaptersDs.setQuery("select e from base$Chapter e where e.chapterBook = '" + Book + "' order by e.chapterOrder asc");
        chaptersDs.refresh();

        // Set current book as default for newly created chapters
        chaptersDs.addItemChangeListener(event -> {
                Map<String, Object> values = new HashMap<>();
                values.put("chapterBook", Books.fromId(Book));
                chapCreateAction.setInitialValues(values);
            });
    }

I’m puzzled why the different events lead to different results. The create button calls upon the table create action, like the right-click on the table does as well.

What’s wrong here?

Why did you place the initialization of the action to the ItemChangeListener? As a result, the action is initialized only when you click on a table row, i.e. select an item. So I beleive if you first select a row, and then click Create button, it will work the same as when you select the action from context menu.

Hi Konstantin, that’s correct. I’d rather have a different approach to setting the initial value but I’m only able to provide the correct value from the browser screen in which I have the correct reference.

I need the create button to have this behavior as well without selecting a row first. Any suggestions how to do this?

Hi Berend,

You can init the action right in the init() method, no need to wrap it in the listener:


chapCreateAction.setInitialValues(ParamsMap.of("chapterBook", Books.fromId(Book)));

However, I don’t know where do you get the Book variable from, and it can make a difference of course.

Obviously I was thinking too difficult here. Thanks - worked as it should.