Check if checkbox is checked

I have a ListRecord that has the source record the GetMovieGenre.List. In the ListRecord there is a checkbox for each movie genre. So in the ListRecord there is an expression with the value
"GetMovieGenre.List.Current.MovieGenre.Genre" and a checkbox with the variable property as a boolean variable IsSelected. The user can select 1 or more checkboxes and when click "Show" I want to show in a table the movies associated with the movie genres checked.

So I have an action "ShowMovies" for when the user clicks on "Show" and it seems that is necessary to use a foreach to check for each checkbox if is checked or not. However Im not understanding how to check if the checkbox is checked or not because that info is not stored in db and each checkbox is associated with the same variable IsSelected. Do you know what is necessary to verify if each checkbox is checked or not?

John K wrote:

I have a ListRecord that has the source record the GetMovieGenre.List. In the ListRecord there is a checkbox for each movie genre. So in the ListRecord there is an expression with the value
"GetMovieGenre.List.Current.MovieGenre.Genre" and a checkbox with the variable property as a boolean variable IsSelected. The user can select 1 or more checkboxes and when click "Show" I want to show in a table the movies associated with the movie genres checked.

So I have an action "ShowMovies" for when the user clicks on "Show" and it seems that is necessary to use a foreach to check for each checkbox if is checked or not. However Im not understanding how to check if the checkbox is checked or not because that info is not stored in db and each checkbox is associated with the same variable IsSelected. Do you know what is necessary to verify if each checkbox is checked or not?

if you have a temporary table to save selected values or if you have an attribute in your table named "IsSelected" and populate this with the checkbox value, you just need to filter your query by all records with IsSelected=true.


Thanks, but like that each time the user clicks in the button "Show" is necessary to first update the IsSelected columns of all movie genres, based on the selected checkboxes, and only then show the records?

John K wrote:

Thanks, but like that each time the user clicks in the button "Show" is necessary to first update the IsSelected columns of all movie genres, based on the selected checkboxes, and only then show the records?

In the show button action, you have to make a query or some logic to verify when the attribute "IsSelected"=true, and in these cases, you show in a table the movies associated with the movie genres "checked" and put an ajax refresh to refresh your screen.


Yes, but my doubt is how to verify which checkboxes are checked (which movie genres are checked), because all checkboxes have the same variable associated "IsSelected" and are not related with the movie genre. 

do you want something like this:

with a table filtered by genre?

Yes, the user select the genres in checkboxes and after clicking in the button "View" it should appear a table with movies associated with the selected genres.

John K wrote:

Yes, but my doubt is how to verify which checkboxes are checked (which movie genres are checked), because all checkboxes have the same variable associated "IsSelected" and are not related with the movie genre. 

Hi John,


You mentioned that all the checkboxes are referring to the variable IsSelected. This is not correct/the right way.
Create separate variables for every checkbox. This way you can assign these variables to the correct filter(s) in your aggregate for your data retrieval.

Regards,

Jasper

Hi John,

The typical approach for what you are trying to do would be to add a Calculated Attribute to your GetMovieGenre Aggregate. Name it IsSelected and set its formula to True or False, depending on what is the default value you prefer the checkboxes to have. Change the Variable of your Checkboxes to be ListRecord1.List.Current.IsSelected. (this assumes your List Records widget is named ListRecord1)

When you click on a button, you can iterate through your List Records' values to check which ones have the ListRecord1.List.Current.IsSelected attribute set to True.

Hope this helps!

Thanks, but a calculated attribute is not an attribute calculated based on the value of other attributes? If I create the attribute "IsSelected" is not based on any attribute of the movie entity. 

But so, if for example I set the formula of that calculated attribute IsSelected to False, this is at the database level right? So even if the checkbox variable is set to be the ListRecord1.List.Current.IsSelected the checkboxes selection is at the client side so Im not udnerstanding how that calculated attribute IsSelected  works in this scenario to associate the checkboxes that are checked with the corresponding movie genres to filter the results. Can you explain better? Thanks!

John K wrote:

Thanks, but a calculated attribute is not an attribute calculated based on the value of other attributes? If I create the attribute "IsSelected" is not based on any attribute of the movie entity. 

But so, if for example I set the formula of that calculated attribute IsSelected to False, this is at the database level right? So even if the checkbox variable is set to be the ListRecord1.List.Current.IsSelected the checkboxes selection is at the client side so Im not udnerstanding how that calculated attribute IsSelected  works in this scenario to associate the checkboxes that are checked with the corresponding movie genres to filter the results. Can you explain better? Thanks!

For me, the best solution is to do this:

you need to have:

  1.  A local variable SelectedGenreIds 
  2. GenreList Variable of type GenreStructure, where GenreStructure have 3 attributes ( IsSelected (boolean) , GenreId (Genre Identifier) , GenreLabel (text))
  3. A Preparation
  4. An UpdateSelectedIds action
  5. Show action



  6. Add a list Clear after ListAppendAll

  7.  

  8. Thanks and Best Regards, Nuno Pereira

I Added a point to the 13th step

Solution

John K wrote:

Thanks, but a calculated attribute is not an attribute calculated based on the value of other attributes? If I create the attribute "IsSelected" is not based on any attribute of the movie entity. 

But so, if for example I set the formula of that calculated attribute IsSelected to False, this is at the database level right? So even if the checkbox variable is set to be the ListRecord1.List.Current.IsSelected the checkboxes selection is at the client side so Im not udnerstanding how that calculated attribute IsSelected  works in this scenario to associate the checkboxes that are checked with the corresponding movie genres to filter the results. Can you explain better? Thanks!

A calculated attribute can be based on the values of other attributes. Or it can be simply an expression that evaluates to a value regardless of other values. By adding this attribute you are adding to the output Record type an extra boolean attribute that you will then be able to use.

The Source Record List of a Table/List Records (let's say yours is named ListRecords1) is used by the platform to determine the data type of its internal List runtime attribute (that is used for the actual rendering of the screen), and also to initialise it (so the Table/List Records displays the information you want) - this means ListRecords1.List is a copy of what you placed as Source Record List . Since it uses the values that were returned from the Aggregate, it will have False as the initial value for your checkboxes.

Whenever you use a Submit/Ajax Submit, the platform will use the checkboxes' state to update the value of your ListRecord1.List.Current.IsSelected automatically, which means you will be able to test it to determine whether it was selected or not.

You can see this pattern in Action very easily:

  • Create a new Application, and a new Module inside your application.
  • Open Manage Dependencies...
  • Select the (System) producer on the left, check the Role and User_Role Entities from the Elements tree to the right and press OK.
  • You should now be seeing the empty Mainflow UI Flow canvas (if not, go to the Interface tab and double-click on the Mainflow UI Flow).
  • Switch to the Data tab, and inside Entities\Database, expand the (System) module and drag & drop the User entity twice on your Mainflow UI Flow.
  • Open the UserDetail web screen that was automatically generated
  • Now drag the User_Role entity next to the form
  • On the generated table you will see checkboxes that allow you to choose multiple rows to delete in one go
  • You can check the Aggregate that is used (you will see the platform adds a Boolean Structure to the Sources, for a similar effect to what I described), the Variable bound to the check boxes (User_UserRoleTable.List.Current.Boolean.Value) and how it is used in the DeleteUserRoles to determine if the check box was checked or not.
    Note: The logic for DeleteUserRoles duplicates the list because it wants to safely delete records while iterating. In your case I don't think there is a need for this, you can iterate User_UserRoleTable.List directly

Hope this helps understand how to use checkboxes to select an arbitrary number of elements and do something with that information.

You can find the OML resulting from these steps attached to this post.

Solution