How to access outer element inside Repeater?

545 Views Asked by At

Javascript

var pageData = new Observable({
    appointmentsList: appointmentsList,
    user_role: ''
});

exports.onLoaded = function (args) {
    page = args.object;
    page.bindingContext = pageData;
    pageData.set('user_role', appSettings.getString('role'));

XML

<Repeater items="{{appointmentsList.booking_dates.bookings}}">
    <Repeater.itemsLayout>
        <StackLayout class="appointment-rows" />
    </Repeater.itemsLayout>
    <Repeater.itemTemplate>
        <StackLayout>
            <WrapLayout visibility="{{ user_role === 'agency' ? 'visible' : 'collapse' }}">
                <Label text="{{user_name}}" class="row-client-name row-client-name-new" />
            </WrapLayout>
        </StackLayout>
    </Repeater.itemTemplate>
 </Repeater>

In above snippet that problem is i am not able to access user_role inside Repeater. Can anybody help me how to access outer element inside Repeater ?

Thanks.

2

There are 2 best solutions below

0
Marek Maszay On BEST ANSWER

Based on this https://docs.nativescript.org/core-concepts/data-binding#example-4-creating-listview-child-items-based-on-the-itemtemplate, You can try something like this:

<WrapLayout visibility="{{$parents['Repeater'].user_role,$parents['Repeater'].user_role === 'agency' ? 'visibile' : 'collapse' }}">

or this way

<WrapLayout visibility="{{$parents['Page'].user_role,$parents['Page'].user_role === 'agency' ? 'visibile' : 'collapse' }}">
9
Nathanael On

You need to use the $parent or $parents[] property.

I believe:

<WrapLayout visibility="{{ $parent.user_role, $parent.user_role === 'agency' ? 'visible' : 'collapse' }}">

Should work. However since this is in a repeater, I don't believe it does anything weird to the parent binding; but if that doesn't work -- you can simply do this: <WrapLayout visibility="{{ $parents['Page'].user_role, $parents['Page'].user_role === 'agency' ? 'visible' : 'collapse' }}">

NativeScript Documentation on this feature: http://docs.nativescript.org/core-concepts/data-binding#binding-to-a-parent-binding-context