nativescript-community / ui-collectionview

Allows you to easily add a collection view (grid list view) to your projects. Supports vertical and horizontal modes, templating, and more.
Apache License 2.0
59 stars 18 forks source link

Can not select an element by id inside the collectionView #17

Closed Whip closed 3 years ago

Whip commented 3 years ago

Which platform(s) does your issue occur on?

Please, provide the following version numbers that your issue occurs with:

Please, tell us how to recreate the issue in as much detail as possible.

<gv:CollectionView items="{{ items }}" colWidth="50%" rowHeight="167" itemTap="onItemSelected">
    <gv:CollectionView.itemTemplate>
         <Label text="{{ name }}" id="{{ 'check'+index }}" verticalAlignment="center"/>
    </gv:CollectionView.itemTemplate>
</gv:CollectionView>
exports.onItemSelected = function(args){
   const selectedItem = page.getViewById('check'+args.index);
   console.log(selectedItem); // returns undefined
}

I'm trying to replace Radlistview with this plugin but I can't to this so I can't use it as of yet. Am I doing something wrong or is it a bug?

farfromrefug commented 3 years ago

I don't even know why it works in rlv. it should not. there is a big chance that view might not be "findable". instead you need to use getViewForItemAtIndex

Whip commented 3 years ago

I can't find any documentation on this. Can you post a simple example?

farfromrefug commented 3 years ago

you can look at the typings. yes the doc might not be full but the typings are always up to date

Whip commented 3 years ago

Okay I looked through your code and I see the function is used in there but I can't seem to get it to work. I tried

exports.onItemSelected = function(args){
   const selectedItem = getViewForItemAtIndex(args.index);
   const selectedItem = this.getViewForItemAtIndex(args.index);
}

Both don't work. I did spot that there's a view returned in the function, so I tried

console.log(args.view);

it returns the GridLayout. My code is a bit complicated than I posted above as an example. Its a GridLayout containing several items, one of those has the id on it which I need. I feel like I'm missing something basic but I'm not an expert on this. Could you post a basic example? I would highly appreciate it.

farfromrefug commented 3 years ago

@Whip you re almost there. Now that you have the GridLayout which is the cell holder, simply use the getViewById you want on this GridLayout then you ll get your view

Whip commented 3 years ago

Perfect. Thanks for your guidance.