How to add components to a container programatically?

Hi there.

Sorry, i might seem totally clueless but it might turn true.
I have an entity form with a one to many to some “messages”. Just like comments added to an issue in a tracker.
Of course, jmix wants to move me to the proper editor when i want to add a comment.
I would like to be able to list the comments in a proper list and add them in a more convenient way (directly in the form by clicking on an “add” button, which would add an other entry down the screen, remove the add button and show a “save” one, OR just by opening a modal frame with a text zone, a ok and cancel button.
Do you have any example on how to do this or something approaching? I believe it would be too long to describe directly on the forum, but i would take that also. I am pretty desesperate at the moment.

Thanks a lot.

Hi Frederic,

I’ve prepared a project demonstrating how you can solve this. The project uses two entities: Topic and Comment, where Topic contains a one-to-many collection of Comments.

Perhaps the most effortless implementation similar to what you need is using editable DataGrid. You can look at the example in Sampler and in the editable-data-grid branch of my project.

The more interesting and customized solution is in the master branch. The Topic edit screen doesn’t have any tables for Comments, instead its XML descriptor just defines a hbox for new comment and the “Add” button. The rest is done programmatically in the controller.

As a result, we have the following Topic edit screen:

  • Displaying the list of comments:

    image

  • Adding new comment:

    image

Regards,
Konstantin

1 Like

Hi Konstantin !
Thanks for the very very appreciated effort. This will build a solid base for all those that follow. That is so much better than just posting a link to a documentation.
I started looking into it and it opened my eyes, i understand things much better from this example than using docs.
Will keep on checking it all and code my own based on this.

Thousand thanks.

Hi Konstantin.
I’ve successfully applied and modified your example, thanks again.
I am almost there, i just need one last thing to work correctly: It would be perfect for the flow if i could add comments without being in edit. I succeeded in having all the logic work to add and buttons to be active, but new entities are not persisted. There must be something blocking this due to no really editing the root entity, but i can not figure out what that is. Can you please help?
Thanks a lot.

Changed entities in an edit screen are normally saved by the DataContext object. It’s created by the framework if the screen contains the <data read-only="false"> element or just <data>.

An editor screen calls DataContext.save() when user clicks OK. Perhaps you should do the same in your screen when you want to save changes.

Hi Konstantin.
I must have fumbled previously. I did not see any save() but tried again with commit().
As i already did -it seemed logical- and did not see it work, i did not try again.

Thanks a lot for your suggestion, it got me on the right tracks.
Have a nice day.