I have an interface where a user is able to click on several choices in a checkbox. Each selection is stored as a separate row in a data table that is linked to their user data. Adding new choices is easy as you just write a new row for that box. Removing them is a bit more tricky. Is there a standard best practice for this row addition/subtraction process?
Discussion posts and replies are publicly visible
Here is an simple example using local variables:
a!localVariables( local!checkBoxesFromDB: { a!map(id: 1, value: "ITEM_1"), a!map(id: 2, value: "ITEM_2") }, local!checkBoxValue: local!checkBoxesFromDB.value, local!checkBoxesToAdd, local!checkBoxesToRemove, { a!checkboxField( label: "Check box field", choiceLabels: { "Item 1", "Item 2", "Item 3" }, choiceValues: { "ITEM_1", "ITEM_2", "ITEM_3" }, value: local!checkBoxValue, saveInto: { local!checkBoxValue, a!save( local!checkBoxesToAdd, a!forEach( items: local!checkBoxValue, expression: a!map(id: fv!index, value: fv!item) ) ), a!save( local!checkBoxesToRemove, difference( cast( typeof({ a!map() }), local!checkBoxesFromDB ), cast( typeof({ a!map() }), local!checkBoxesToAdd ) ) ) } ) } )
gotcha, so I come up with separate lists of things to add and things to remove. Thanks
The "best practice" (IMHO) is to not remove but instead deactivate rows that a user un-selects in this manner (via an "active flag" row or a "removed flag" row, both the same thing except for being opposite in value). The question then becomes, what happens if a user re-selects previously un-selected rows? The code is a little trickier but it helps prevent DB rows from getting spammed if you implement a system that looks up any deactivated rows for the current user and marks them as re-activated if they're re-selected (instead of selected for the first time). IMHO it's worth the time and hassle to implement.
If you found my advice helpful, please consider marking the response as verified. Thanks! :)