Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
gpatton
Posts: 193
Registered: ‎12-29-2010
My Device: Bold 9900

Is it possible to know the "rank" of an item in the listView?

I'm trying to sort some items on a list. I have a custom ListItemComponent.

In it, when I sort, I'd like to assign the position of the item a number.

 

So let's say we have three countries. Argentina, India, Zambia.

 

When I sort by Name, I want it to show a rank number next to the name. (I'm getting the name from a JSON list).

So when sorting by Name, it would say:

Argentina 1

India 2

Zambia 3

 

When I sort by population now, I want it to say

India 1

Argentina 2

Zambia 3

 

Now if I sort Name descending it would say

Zambia 1

India 2

Argentina 3

 

Any idea on how to do this in QML?

 

Thanks!

GP

Developer
slashkyle
Posts: 820
Registered: ‎10-16-2012
My Device: Red Z10

Re: Is it possible to know the "rank" of an item in the listView?

where you insert the country name in your list, since it appears you want the # value to stay the same

 

ListItemComponent {
            type: "header"
             
            Label {
                text: "Country Name" + " 1"
            }
        }

 

Developer
gpatton
Posts: 193
Registered: ‎12-29-2010
My Device: Bold 9900

Re: Is it possible to know the "rank" of an item in the listView?

[ Edited ]

Ah i see what you're saying, but thats not quite what I need.

 

I need to figure our programatically, the country's position in the list.

 

so I would do text: CountryName + index

 

I need to figure out the "index". How can I figure that out?

 

Additionally, every time the list is sorted, by population, name, area etc. the position of the country would change, hence its "index" (I could be using the wrong word here).

Developer
dishooom
Posts: 103
Registered: ‎12-31-2012
My Device: Blackberry 9810

Re: Is it possible to know the "rank" of an item in the listView?

 

 

To set the rank in list view, you need to modufy the title as follows :

 

 ListView {
            id: lv
            dataModel: dataModel
            
            listItemComponents: [
                ListItemComponent {
                    type: "item"
                    StandardListItem {  title: ListItemData.name +  (ListItem.indexInSection + 1)
                    
                    }
                }
            ]
        }      

 .... and to order the list based on Criterion... population for instance

   Button {
            id: ascendPopButton
            
            text : "Sort by Pop"
             
             
            onClicked: {               
                dataModel.sortingKeys = [
                    "population"
                ]
                dataModel.sortedAscending = true 
            
            
            }

 i had set my itemGrouping to None while adding the DataModel via attachedObjects...

     GroupDataModel {
            id: dataModel
            sortingKeys: [
                "name"
            ]
            grouping: ItemGrouping.None
            sortedAscending: true
        }

 

Hope this helps :Angel:

 

-Dishooom