Getting RadListView itemIndex when a button is tapped

nativescriptcore
plugins
android

#1

Hello guys, I’ve been stack for while now. How can I get the itemIndex of an item in a RadListView by tapping on a button in the list. I can use itemTap to get the itemIndex but that’s not the desired behavior. I want to use a button instead.

<lv:RadListView id="listview" class="list-group" items="{{ news }}"  itemTap="{{ allPostsItemTap }}"> 
                                                        <lv:RadListView.listViewLayout>
                                                            <lv:ListViewLinearLayout scrollDirection="Vertical"/>
                                                        </lv:RadListView.listViewLayout>
                                                        <lv:RadListView.itemTemplate itemIndex="{{ post_id }}">
                                                            <StackLayout class="list-group-item" style="padding:0; margin:0; margin-bottom:1; background-color:#fff">
                                                                <Label text="{{ post_title }}" textWrap="true" class="list-group-item-heading" />
                                                                <Image id = "{{ post_id }}" style="margin-top:-22;" src="{{ post_media }}" stretch='aspectFit' class='' />
                                                                <Label text="{{ media_caption }}" textWrap="true" class="list-group-item-text font-weight-bold" />
                                                                <Label text="{{ post_content }}" textWrap="true" class="list-group-item-text" />
                                                                <!--ACTIONS SECTION-->
                                                                <StackLayout orientation='horizontal' style="margin-bottom:0px; margin-left:0; padding:0; width:100%; margin-top:-20">     
                                                                    <Label text="{{ post_date }}" textWrap="true" class="list-group-item-text" style="font-size:11" horizontalAlignment='left' width='50%'/>
                                                                    <Label horizontalAlignment='right' text="{{ posted_by }}" textWrap="true" class="label font-weight-bold" class="list-group-item-text label" width='50%' style.textAlignment='right'/>
                                                                </StackLayout>

                                                                <StackLayout orientation='horizontal' style="margin-bottom:0px; margin-left:0; padding:0; width:100%; margin-top:-10">                                                                                                                                                                                                    
                                                                    <Label text="{{ likes_count }} " class="social_interactions" style='font-size:15'/>
                                                                    <Label text='likes ' class='social_labels'/>                                                                    
                                                                    <Label class="social_interactions" text="{{ comments_count }} " />                                                                                                                                     
                                                                    <Label text='comments ' class='social_labels'/>
                                                                    <Button text='share' class="social_btn" tap="{{sharePost}}" name='{{post_media}}'></Button>																	
                                                                    <Button text='Delete' class="social_btn" tap="{{deletePost}}" id = "{{ post_id }}" visibility = '{{can_delete}}'></Button>                                                                    
                                                                </StackLayout>
                                                                <!--ACTIONS SECTION ENDS-->
                                                                <StackLayout class="hr-light m-b-1"></StackLayout>
                                                            </StackLayout>
                                                        </lv:RadListView.itemTemplate>
                                                    </lv:RadListView>

I want to get the index of an item and delete it when the Delete button is tapped. Any Ideas?


#2

You should have access to data item already so you may try,

allPostsItemTap.indexOf(item);

#3

Thanks, I’m giving it a try.


#4

I’m getting an error allPostsItemTap.indexOf is not a function.
Actually allPostsItemTap is a function that runs when the item itself is tapped and takes me to another page with a complete article. I want to get the index of this item by tapping a button instead.


#5

I assumed it’s an array, just try indexOf on the source array.


#6

Thanks a lot manojdcoder for the insights, the problem is getting reference to the index of the item holding the button, I had to kind of convert my ObservableArray to an array, loop through the data to get the index of the target post. For now that’s how I’m handling it but it would be great if we could get the item index without looping through the data (the same way itemTap event works, it exposes the index of the tapped item out-of-the-box).