Jmix 2 - Entity initialization

Hi
I can’t figure out how to initialize an entity with values before opening the details view.
I tried to open the view manually with the following code, as written in the documentation:

@Autowired
private ViewNavigators viewNavigators;

private void navigateToCreateEntity() {
    viewNavigators.detailView(Department.class)
            .newEntity()
            .navigate();
}

But I can’t find a way to initialize my fields from the List View.

​I read Gleb’s post: https://forum.jmix.io/t/flowui-sending-parameter-to-next-screen/2267

But I would like to use a normal Detail View not a Popup Dialog.

I prefer not to use QueryParameters because are visible in the url and prevent them from being changed.

I appreciate your help
Thank you

Hi,

You can use the withAfterNavigationHandler method. For example:

import com.company.sample.entity.Customer;

import com.company.sample.view.main.MainView;

import com.vaadin.flow.component.ClickEvent;
import com.vaadin.flow.router.Route;
import io.jmix.flowui.ViewNavigators;
import io.jmix.flowui.component.grid.DataGrid;
import io.jmix.flowui.kit.component.button.JmixButton;
import io.jmix.flowui.view.*;
import org.springframework.beans.factory.annotation.Autowired;

@Route(value = "customers", layout = MainView.class)
@ViewController("Customer.list")
@ViewDescriptor("customer-list-view.xml")
@LookupComponent("customersDataGrid")
@DialogMode(width = "64em")
public class CustomerListView extends StandardListView<Customer> {

    @Autowired
    private ViewNavigators viewNavigators;
    @ViewComponent
    private DataGrid<Customer> customersDataGrid;

    @Subscribe(id = "createBobBtn", subject = "clickListener")
    public void onCreateBobBtnClick(final ClickEvent<JmixButton> event) {
        viewNavigators.detailView(customersDataGrid)
                .newEntity()
                .withViewClass(CustomerDetailView.class)
                .withAfterNavigationHandler(afterViewNavigationEvent -> {
                    CustomerDetailView customerDetailView = afterViewNavigationEvent.getView();
                    customerDetailView.setCustomerFirstName("Bob");
                })
                .navigate();
    }
}

Here is the project that demonstrates this:
init-entity-sample.zip (121.3 KB)

1 Like

Super !!!
Thank you very much