Thanks for the help David, you set me on the right path…
For anyone following this particular path, this is what I now have, and how I did it
I didn’t use forEach, Instead I turned my related (has to be related or you lose your previous values when you change value lists) dataprovider, converted to an array, used a splice, and spliced my value into and out of the array depending on whether I was adding or removing it (you have to remember to add the \n ). My array was then converted back to text where I joined the array back together on the \n. It has taken all day, but it works beautifully.
The point of it is that it adds the same checkbox value to a foundset of records - and the valuelists are different depending on the user. You can choose user A, and see all their (enabled) checkbox values, or choose user B and see theirs etc. Once you’ve chosen a user, you can add or remove a particular value to or from the whole foundset. This means that my records can have perpetually saved groups of items - a la metadata. The related record is on the username and the record id, so each user has to have a join record for every row in the table.
My code, to add the checkbox is:
function onDataChange_loop_add_tag_to_foundset(oldValue, newValue, event) {
var vTheTag = globals.g_tag_add_to
for (var i = 1; i <= foundset.getSize(); i++){
foundset.setSelectedIndex(i)
if (foundset.getSize() > galleria_administration_to_superuser.tag_warning_size){
var thePressedButton = plugins.dialogs.showWarningDialog('Warning','You have selected more than ' + galleria_administration_to_superuser.tag_warning_size + ' items to tag, are you sure?','OK','Cancel')
if (thePressedButton = 'Cancel'){
return
}
}
if (foundset.works_to_tags_join.getSize()>0){
if (foundset.works_to_tags_join.getSize()>1){
application.ouput('error - more than one related works to tags record!!!')
}
}
else{
foundset.works_to_tags_join.newRecord(true)
}
var itemsArray = new Array();
if (works_to_tags_join.all_tags){
itemsArray = works_to_tags_join.all_tags.split('\n');
}
var index = itemsArray.indexOf(globals.g_tag_add_to);
if (index < 0) {
itemsArray.splice(2, 0, vTheTag);
}
var myString = itemsArray.join('\n');
foundset.works_to_tags_join.all_tags = myString
}
databaseManager.saveData()
}
And to remove the specified check:
function onDataChange_loop_remove_tag_from_foundset(oldValue, newValue, event) {
var vTheTag = globals.g_tag_remove_from
for (var i = 1; i <= foundset.getSize(); i++){
foundset.setSelectedIndex(i)
if (foundset.works_to_tags_join.getSize()>0){
}
else{
foundset.works_to_tags_join.newRecord(true)
}
var itemsArray = new Array();
itemsArray = works_to_tags_join.all_tags.split('\n');
var index = itemsArray.indexOf(globals.g_tag_remove_from);
if (index > -1) {
itemsArray.splice(index, 1);
}
var myString = itemsArray.join('\n');
foundset.works_to_tags_join.all_tags = myString
}
I initially tried to use just text and string.replace(this, that), but if you have say ‘2014’ as a valuelist value, and ‘2014 accounts’ as another, and you remove 2014 as a string value from all, you end up breaking all the ‘2014 accounts’ values. It had to be an array for this reason.