NativeScript Angular

RadListView Data Operations

This article will guide you through the process of enabling one or all of the data operations available as built-in functionality into the RadListView. The component provides all of the basic data operations with an easy to use API that enables powerful configuration of the grouping, filtering and sorting criteria to achieve any desired configuration of the displayed items in the RadListView.

By 'data operations' in the context of the RadListView we mean doing operations on the 'raw' source collection that is set to the items property. Those operations can be grouping, sorting and filtering. Each one can be used separately or combined with the others, this way you can filter the items and group them without changing the 'raw' business object collection you have provided to the items.

Grouping

In order to enable grouping you simply need to set the groupingFunction property to a function that simply returns the object which will be used as criteria for grouping functionality. Here is a simple example of such function:

this.myGroupingFunc = (item: DataItem) => {
    return item.category;
};

Grouping template

Setting up custom grouping template.

<ng-template tkGroupTemplate let-category="category">
    <GridLayout ios:height="50">
        <Label class="nameLabel" [text]="category" class="groupLabel"></Label>
    </GridLayout>
</ng-template>

Sorting

Sorting is enabled by simply setting the sortingFunction property to a function that takes one parameter and returns true if the item passed as parameter should be included and false otherwise. Here is a simple example of such function:

this.mySortingFunc = (item: DataItem, otherItem: DataItem) => {
    let res = item.id < otherItem.id ? SortingOrder.NSOrderedAscending : item.id > otherItem.id ? SortingOrder.NSOrderedDescending : SortingOrder.NSOrderedSame;
    return res;
};

Filtering

To enable filtering in the RadListView simply set its filteringFunction property to a function that takes two parameters and returns 0 (the params are equal), 1 (first param is greater) or -1 (second param is greater). Here is a simple example of such function:

this.myFilteringFunc = (item: DataItem) => {
    return item && item.name.includes("Special Item");
};

Example 1: Implementing grouping, filtering and sorting

In this example we are going to enable all of the data operations on the same RadListView with the following criteria:

  • Filter the business objects which name property contains the string "Special Item"
  • Sort the business objects by the value of their id property
  • Group the business objects by the value of their category property
<GridLayout tkExampleTitle tkToggleNavButton rows="*, *7">
    <StackLayout orientation="horizontal">
        <Button width="33%" [text]="isFilteringEnabled ? 'Disable filtering' : 'Enable filtering'" (tap)="toggleFilter()" ios:margin="5"></Button>
        <Button width="33%" [text]="isSortingEnabled ? 'Disable sorting' : 'Enable sorting'" (tap)="toggleSorting()" ios:margin="5"></Button>
        <Button width="33%" [text]="isGroupingEnabled ? 'Disable grouping' : 'Enable grouping'" (tap)="toggleGrouping()" ios:margin="5"></Button>
    </StackLayout>
    <RadListView selectionBehavior="Press" (itemSelected)="onItemSelected($event)" [items]="dataItems" row="1" #myListView 
                    [sortingFunction]="mySortingFunc" 
                    [filteringFunction]="myFilteringFunc"
                    [groupingFunction]="myGroupingFunc" >
        <ng-template tkListItemTemplate let-item="item">
            <StackLayout orientation="vertical">
                <Label class="nameLabel" [text]="item.name"></Label>
                <Label class="descriptionLabel" [text]="item.description"></Label>
            </StackLayout>
        </ng-template>
    </RadListView>
</GridLayout>

This will produce the following UI with the filtered, sorted and grouped objects:

Figure 1: Grouping, filtering and sorting enabled in RadListView:

RadListView Data-Operations on iOS RadListView Data-Operations on Android

References

Want to see this scenario in action? Check our SDK examples repo on GitHub. You will find this and many other practical examples with NativeScript UI.

Related articles you might find useful: