Checkbox Group how to show current user selections

Hi folks,

I have a form that connects to the memberItem database. My form has 3 checkboxs store tag from my membersTags database. This is how I am storing the tags information, it works.

This is the code I am using just in case it helps someone else.

$w('#MemberItem').onBeforeSave(() => {
 let checked = [];
 if ($w('#checkbox1').checked)
                        checked.push('Club Member');
 if ($w('#checkbox2').checked)
                        checked.push('Club Volunteer');
 if ($w('#checkbox3').checked)
                        checked.push('Recycling Organizer');

  $w('#MemberItem').setFieldValue('memberTags', checked);
                });

The checkboxes that are clicked are saved to the memberTags field in my memberItem database as tags.

My problem is this form does not show me the previously selected checkboxes for the user when it loads.

I’m thinking maybe something along these lines might work but I’m getting errors.

export function membersNameDropdown_click(event) {

    $w("#MemberItem").getItems(1, 2, 3);
         .then((result) => {
 let item = result.items;
 let value1 = item[1].name;
 let value2 = item[2].name;
 let value3 = item[3].name;
}

 if (value1 === "1425 Club Member") {
                $w('#checkbox1').checked = true;
            } else {
                $w('#checkbox1').checked = false;
            }
 if (value2 === "1425 Club Volunteer") {
                $w('#checkbox2').checked = true;
            } else {
                $w('#checkbox2').checked = false;
            }
 if (value3 === "1425 Recycling Organizer") {
                $w('#checkbox3').checked = true;
            } else {
                $w('#checkbox3').checked = false;
            }
         });
}

Many thanks to anyone that can help me out?

Sylvia

$w("#MemberItem").getItems(0,3)// 0 is the index to start from; 3 is the number of items
 .then((result) => {
 let item = result.items;
 let value1 = item[0].name;// 0 is the index of the first item
 let value1 = item[1].name;
 let value1 = item[2].name;
 //etc....

Thanks J.D.
I tried it out in the onReady for my page and in the membersNameDropdown_click, but neither does anything. Any idea why? Here is my code now.

import wixData from 'wix-data';

$w.onReady(function () {

            $w('#memberItem').onReady(function () {
                $w('#memberItem').getItems(0, 3) // 0 is the index to start from; 3 is the number of items
                    .then((result) => {
 let item = result.items;
 let value0 = item[0].name; // 0 is the index of the first item
 let value1 = item[1].name;
 let value2 = item[2].name;

 if (value0 === "Club Member") {
                            $w('#checkbox0').checked = true;
                        } else {
                            $w('#checkbox0').checked = false;
                        }
 if (value1 === "Club Volunteer") {
                            $w('#checkbox1').checked = true;
                        } else {
                            $w('#checkbox1').checked = false;
                        }
 if (value2 === "Recycling Organizer") {
                            $w('#checkbox2').checked = true;
                        } else {
                            $w('#checkbox2').checked = false;
                        }
                    });

                $w('#memberItem').onBeforeSave(() => {
 let checked = [];
 if ($w('#checkbox0').checked)
                        checked.push('Club Organizing Committee');
 if ($w('#checkbox1').checked)
                        checked.push('Club Member');
 if ($w('#checkbox2').checked)
                        checked.push('Club Volunteer');
                                            $w('#memberItem').setFieldValue('memberTags', checked);

                });
                });
            });

@1425club66601 your if conditions don’t make sense.
But first try to explain how your collection is organized.
Does each record stand for a member, and does each record have a field key named memberTags that contains an array of strings?
And what is #1425MemberItem ?
And What’s the event handler for saving? And where is the save function?

More details can help.

ok here goes, 1425 is the name of the group. I thought I should delete it, you can just ignore it when you see it. I will leave it in on the remainder of my emails.

Step1 . This image shows how my collections are organized.

Step 2. Yes each record is connected to the privateMembersData record.

Step 3.
1425MemberDetails dataset = name is the “Main Field” it is connected to name in privateMembersData. The email and phone fields are reference fields from the privateMembersData.
1425MemberItem dataset = Is used by the dropdown membersNameDropdown field. When you click it filters to find the current name.
membersTags dataset = membersTags is the “Main Field” (not really used in this form but I would like to).

Step 4. Yes each checkbox the user clicks is put into membersTags field as tag using

$w('#1425MemberItem').onBeforeSave(() => {
 let checked = [];
if ($w('#checkbox10').checked)
                        checked.push('1425 Club Graphic Designer');
                    $w('#1425MemberItem').setFieldValue('memberTags', checked);


Step 5. This is the info for the dropdown membersNameDropdown it selects the users name.

Step 6. This is the info for the Submit button.


This is the page code with the 1425 added back in.


import wixData from 'wix-data';

$w.onReady(function () {

            $w('#1425MemberItem').onReady(function () {
                $w('#1425MemberItem').getItems(0, 3) // 0 is the index to start from; 3 is the number of items
                    .then((result) => {
 let item = result.items;
 let value0 = item[0].name; // 0 is the index of the first item
 let value1 = item[1].name;
 let value2 = item[2].name;

 if (value0 === "Club Member") {
                            $w('#checkbox0').checked = true;
                        } else {
                            $w('#checkbox0').checked = false;
                        }
 if (value1 === "Club Volunteer") {
                            $w('#checkbox1').checked = true;
                        } else {
                            $w('#checkbox1').checked = false;
                        }
 if (value2 === "Recycling Organizer") {
                            $w('#checkbox2').checked = true;
                        } else {
                            $w('#checkbox2').checked = false;
                        }
                    });

                $w('#1425MemberItem').onBeforeSave(() => {
 let checked = [];
 if ($w('#checkbox0').checked)
                        checked.push('1425 Club Organizing Committee');
 if ($w('#checkbox1').checked)
                        checked.push('1425 Club Member');
 if ($w('#checkbox2').checked)
                        checked.push('1425 Club Volunteer');

                        $w('#1425MemberItem').setFieldValue('memberTags', checked);

                });
                });
            });


Thank you so much for your help!!!
Sylvia

Just to understand: when you use $w(‘#memberItem’).getItems(0, 3) you get the first 3 members or the first 3 tags?

I get the details for that member Member tags is one of the details.

@1425club66601 I see. So you only want to get 1 member? Is it a dynamic page for a specific member?

Sorry I answered wrong last time. When I test the page the 3 member tags are being input into the memberTags field correctly. But when I select a different member and then click back to the user I added them to, the checkboxes aren’t clicked.

@smwebview , Sorry. despite your explanation, I still don’t think I understand the flow and goals.
Maybe someone else…

I really hope so, thanks for trying JD. So step right up folks, who can solve this issue for me. My code works to save my checked checkboxes as tags to the 1425MembersItem dataset, membersTags field.

Now I am trying to read back the tags from that same field when the user loads the page to show the tags they have already selected as checked.

This part of the code is not working:

$w('#1425MemberItem').getItems(0, 3) // 0 is the index to start from; 3 is the number of items
            .then((result) => {
 let item = result.items;
 let value0 = item[0].name; // 0 is the index of the first item
 let value1 = item[1].name;
 let value2 = item[2].name;

 if (value0 === "") {
    $w('#checkbox0').checked = false;
} else {
    $w('#checkbox0').checked = true;
}
if (value1 === "") {
    $w('#checkbox1').checked = false;
} else {
    $w('#checkbox1').checked = true;
}
if (value2 === "") {
    $w('#checkbox2').checked = false;
} else {
    $w('#checkbox2').checked = true;
}

Any help is appreciated!
Sylvia