How to catch click on category header with RadListView grouping function? (Nativescript)

泄露秘密 提交于 2019-12-02 22:18:43

问题


I had previously asked about a few items re: RadListView's grouping function here. I didn't get an answer, so wanted to try to focus on hopefully the simplest part: how do I catch the click event on a category header?

Normally, this would be pretty easy, like <Label text="group.category" (tap)="youClickedTheCategory()"></Label>.

But using the grouping function with RadListView, the category does not have an html entry, so how do I know if the user clicks on the category header instead of somewhere else in the group?

If example code is helpful:

html:

<GridLayout>
    <RadListView [items]="places" [groupingFunction]="myGroupingFunc">
        <ng-template let-place="item" >
            <StackLayout>
                <Label [text]="place.city"></Label>
            </StackLayout>
        </ng-template>
    </RadListView>
</GridLayout>

ts:

   public places = [
        {country: 'US', city: 'New York'}, 
        {country: 'US', city: 'Los Angeles'},     
        {country: 'Canada', city: 'Toronto'},
        {country: 'England', city: 'London'}
        ]

    constructor() {
    }

    myGroupingFunc(value) {
      return value.country;
    }

Result would be:

Canada
--Toronto

England
--London

US
--New York
--Los Angeles

Goal: know if the user clicks on the country category header (here, Canada , England, or US)--instead of the user clicking on the whole group.

Using Nativescript Angular (for iOS).


回答1:


I have found the answer, based on this github discussion here. The key is the tkGroupTemplate in RadListView. For NativeScript (and iOS--probably works for Android too), if you want to have a list that has category headers and entries below, and you want to be able to click on the category headers, the present method is to use tkGroupTemplate.

Here is an example:

$ tns plugin add nativescript-ui-listview

component html:

<GridLayout>
    <RadListView [items]="places" [groupingFunction]="myGroupingFunc">
        <ng-template tkListItemTemplate let-place="item">
            <StackLayout>
                <Label [text]="place.city" (tap)="listEntryTap(place.city)"></Label>
            </StackLayout>
        </ng-template>
        <ng-template tkGroupTemplate let-country="category">
            <StackLayout ios:height="25">
                <Label [text]="country" (tap)="headerTap(country)"></Label>
            </StackLayout>
        </ng-template>
    </RadListView>
</GridLayout>

ts: (component ts file)

public places = [
         {country: 'US', city: 'New York'}, 
         {country: 'US', city: 'Los Angeles' },     
         {country: 'Canada', city: 'Toronto'},
         {country: 'England', city: 'London'},
         {country: 'US', city: 'Chicago'},
         {country: 'Canada', city: 'Calgary'}      
]
...
constructor(){}
...
myGroupingFunc(value) {
   return value.country;
}

headerTap(country){
    console.log('you tapped this country header: ' + country)
}

listEntryTap(city){
    console.log('you tapped this city entry: ' + city)
}

module.ts: (module for the component--if using lazy loading of components. If not using lazy loading, this probably would go in the main app.module.ts file)

import { NativeScriptUIListViewModule } from "nativescript-ui-listview/angular";

@NgModule({
  imports: [
  ...
   NativeScriptUIListViewModule 
  ]
...

And that should produce the following, with headers (countries) and entries (cities) separately clickable:

Canada
Toronto
Calgary

England
London

US
New York
Los Angeles
Chicago

It looks like this comes with some default formatting (the headers have a different background color automatically)--but you can override that with your own styles.

Without the ios:height="25"(or whatever height) some of the category headers go over the entries.

Otherwise, though, this seems to work for iOS and NativeScript 5.0+ (and, I assume, earlier versions too).



来源:https://stackoverflow.com/questions/53885975/how-to-catch-click-on-category-header-with-radlistview-grouping-function-nativ

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!