95
Views
7
Comments
Solved
Multiselect checkbox list Save and delete

Hi all,


I have a variable called Display in an Example entity and a static entity from which i can choose the values. As I want to be able to select multiple values from the static entity, and save them as a record in the Example entity, I created a many to many relationship by adding a 3 entity, with both Id's as a reference. 

I understand to be able to select multiple option I can make a checkbox list in which I can select the records I would want to relate to the Display. However, I don't know how to save them properly to the 3th entity because when I deselect an item I would of course like too remove the record. 

I am not sure I am taking the right approach on this.


Kind Regards,

Liesbeth

Rank: #136
Solution

Hi Liesbeth,

indeed very close :-)

the content id should not be a filter, because you always want to show all possible display values.  The content id should be part of your join condition :

what you are basically saying here is that for left side of join you start with all records in Display, on right side of join you start with all records in ContentDisplay that have the correct ContentId.  And that's the 2 datasets you join as with or without.  At least, that's how I understand it, I'm not a DBA.


see attached oml,

Dorine


Edit : ik zie net dat we Belgische collega's zijn, leuk.  In Belgie is OS nog relatief klein, Belgen kom je niet al te vaak tegen op het forum.

DemoSelectManyToMany.oml

Rank: #180

Hi Liesbeth,
I think that this component help you todo what need.

https://www.outsystems.com/forge/component-overview/982/select2

However, if you not can do what you need whith this component, please, post some image with more details, or your .oml for we understand better your objective.


Another way, example with DropdownSelect and ListBox:

Regards.


Rank: #1799

Thanks Agno,


With the component I can't seem to really save or delete the records. Also, I need a structre list and I have an entity.


I have looked at your second method too. However, I should have mentioned I am building my application in reactive and it doenst seem to have a listbox. 


Thank you though!

Rank: #136

Hi Liesbeth,

there's not really enough info about how you want to display the options to select/deselect and when/how you want to save the changes, so I'm going to make some assumptions here.  Let's say you have entity Content, static entity Display and ContentDisplay entity to create the many to many relationship.

You have some detail screen for your Content entity.  It has a form, and when clicking save, you want to create or update that Content record in the database.  One of the features of a Content is that it can have 0, one or more Displays associated with it.  So you need to present all the display options available plus an indication what displays are selected for the current Content being edited.  

I'm guessing from what you say that you have already found a way of having a check box in each row of your list.  In case not, let me know, I'll elaborate, it involves doing a join between Display and ContentDisplay, making sure you get the right join options, and a calculated boolean column set to True if a record is found in the ContentDisplay entity.

With me so far ?

So in your list, you will have a checkbox bound to this calculated boolean column.

And finally, what do you do when the user Saves the Content record :

You iterate through the list, evaluating each boolean : if it is checked, you do a CreateOrUpdate on ContentDisplay, if it not checked you do a Delete.  I don't see a problem with the CreateOrUpdate, but you'd have to test what happens with the Delete, it might raise exception if you try to delete something that isn't there.  So you'd have to first see if it is there before you delete it.


Above is a simple approach, but you could make things more performant in following way :

The aggregate has 2 identical boolean calculated columns, named for example currentlySelected, newlySelected.  You only put the newlySelected on the screen, and in the save action, you only have to do a Create when newlySelected is True, and currentlySelected is False, and a delete when newlySelected is False and currentlySelected is True.  That could save some time if there are many Display types.


Please let us know if any of this resembles what you are trying to do,

Dorine

Rank: #1799

Thank you Agno, it is not exactly what I was looking for but I believe I can make use of that somewhere else in my application so it has been very helpfull.


Dorine, that is exactly what I wanted to do! Thank you for making sense out of this and for providing me with a solution. I am very close but there is still one thing I havent figured out. 


I cannot seem to find the right join conditions. Right now I have the following join:

Display with or without ContentDisplay. 

The problem with this is that I don't have the Content entity that tells me which ContentId I am looking at. When I join the Content entity or I filter for the ContentId, I loose all the Display option that are not selected. 

I've tried several thing but I just cannot figure it out...

Rank: #136
Solution

Hi Liesbeth,

indeed very close :-)

the content id should not be a filter, because you always want to show all possible display values.  The content id should be part of your join condition :

what you are basically saying here is that for left side of join you start with all records in Display, on right side of join you start with all records in ContentDisplay that have the correct ContentId.  And that's the 2 datasets you join as with or without.  At least, that's how I understand it, I'm not a DBA.


see attached oml,

Dorine


Edit : ik zie net dat we Belgische collega's zijn, leuk.  In Belgie is OS nog relatief klein, Belgen kom je niet al te vaak tegen op het forum.

DemoSelectManyToMany.oml

Rank: #1799

Bless you Dorine! 

Exact wat ik nodig had :) 

Op mezelf had ik er nog een tijdje mee gesukkelt