Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the syntax for a radio group? #95

Open
Whip opened this issue Aug 5, 2018 · 7 comments
Open

What is the syntax for a radio group? #95

Whip opened this issue Aug 5, 2018 · 7 comments

Comments

@Whip
Copy link

Whip commented Aug 5, 2018

I've checked all the demos and readme but I couldn't find any example of a radio group. Basically I want a few radio checkboxes and enabling one should disable others. Right now, I can enable all the radios and disable none.

@KirilOkun
Copy link

+1. i think I understood from the example but the main document should really include a clear writeup of how to setup radio group instead of just punting to the uncommented example code.

@Whip
Copy link
Author

Whip commented Oct 24, 2018

I couldn't find anything. Could you post an example?

@KirilOkun
Copy link

It's in the angular demo app. From what i can tell there are no separate settings in the xml. The group management is done in the code behind. Take a look at the event handler functions for the checkbox in Tab 2.
https://github.com/nstudio/nativescript-checkbox/blob/master/demo-ng/app/item/items.component.html

@Whip
Copy link
Author

Whip commented Oct 25, 2018

Thanks @bearoutthere . I'll see what I can do for regular javascript from that. There is no code in the normal demo folder for this. I'd still like the author to add this in the documentation.

@KirilOkun
Copy link

KirilOkun commented Oct 28, 2018

I looked at the example and it seemed over complicated. Maybe it's due to the Angular syntax, which i'm not familiar with. The easiest approach i ended up with was to use the list view model array directly.
In the template use listview's index for the checkbox like so:

<ListView id="personsList" items="{{ personSelections }}" itemTemplateSelector="$index">
<ListView.itemTemplate>
<cb:CheckBox id="{{ $index }}" checked="{{ selected }}" text="{{ name }}" boxType="circle" loaded="onCheckboxLoaded" tap="onPersonTap" ></cb:CheckBox>
</ListView.itemTemplate>
</ListView>

then in code behind handler function use the model list to go through the list and unset all list items' selected properties. After the tap event the selected checkbox component will set it's selected property to true. Make sure that the items in the source ObservableArray are also of type Observable, otherwise the property change will not be reflected on the other checkboxes.

export function onPersonTap(args) {
    // when a radio button is selected unselect all radio buttons in the list.
    // checkbox will set the selected item's checked property to true after the tap event.
    pageData.personSelections.forEach((item, index, array) => {
        item.selected = false;
    });
}

That will keep only the clicked item selected and all the others unselected.

@cfjedimaster
Copy link

I'm still struggling with this - has anyone does this in Vue?

@enzome
Copy link

enzome commented May 21, 2021

It works as radio like this - for Vue.

<Template>
(...)
                <StackLayout>
                    <StackLayout v-for="(group,groupkey) in item.groupedchoices" :key="groupkey">
                        <check-box v-for="(choice, choicekey) in group" :key="choicekey" :text="choice.name" :checked="choice.checked" @checkedChange="ticked($event.value, groupkey, choicekey, choice)" />        
                    </StackLayout>
                </StackLayout>
(...)
</Template>
<script>
(...)
        ticked(val, groupkey, choicekey, choice) {
            // this.item.groupedchoices[groupkey].each.checked = false;
            
            this.item.groupedchoices[groupkey].forEach(grc => {grc.checked = false});
            setTimeout(()=> {this.item.groupedchoices[groupkey][choicekey].checked = val}, 30);
        },

</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants