Socket
Book a DemoInstallSign in
Socket

@nativescript-community/ui-collectionview

Package Overview
Dependencies
Maintainers
7
Versions
159
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nativescript-community/ui-collectionview

Your awesome NativeScript plugin.

Source
npmnpm
Version
4.0.5
Version published
Weekly downloads
235
45.06%
Maintainers
7
Weekly downloads
 
Created
Source

NativeScript CollectionView

npm downloads npm downloads npm

A NativeScript CollectionView widget. The CollectionView displays data in separate cells, each cell representing one data item. For iOS wraps up UICollectionView and for Android wraps up RecyclerView

iOS DemoAndroid Demo

Table of Contents

Installation

Run the following command from the root of your project:

ns plugin add @nativescript-community/ui-collectionview

This command automatically installs the necessary files, as well as stores @nativescript-community/ui-collectionview as a dependency in your project's package.json file.

Configuration

There is no additional configuration needed!

API

Events

  • itemLoading
    Triggered when generating an item in the CollectionView.

  • itemTap
    Triggered when the user taps on an item in the CollectionView.

  • loadMoreItems
    Triggered when the generated items reached the end of the items property.

Static Properties

  • itemLoadingEvent - String
    String value used when hooking to itemLoadingEvent event.

  • itemTapEvent - String
    String value used when hooking to itemTapEvent event.

  • loadMoreItemsEvent - String
    String value used when hooking to itemTapEvent event.

Instance Properties

  • ios - UICollectionView
    Gets the native iOS view that represents the user interface for this component. Valid only when running on iOS.

  • android - android.support.v7.widget.RecyclerView
    Gets the native android widget that represents the user interface for this component. Valid only when running on Android OS.

  • items - Array | ItemsSource
    Gets or sets the items collection of the CollectionView. The items property can be set to an array or an object defining length and getItem(index) method.

  • itemTemplate - String
    Gets or sets the item template of the CollectionView.

  • rowHeight - PercentLength
    Gets or sets the height for every row in the CollectionView.

  • colWidth - PercentLength
    Gets or sets the width for every column in the CollectionView.

Instance Methods

  • refresh()
    Forces the CollectionView to reload all its items.

  • scrollToIndex(index: number, animated: boolean = true)
    Scrolls the CollectionView to the item with the given index. This can be either animated or not. Defaults to animated.

Usage

You need to add xmlns:gv="@nativescript-community/ui-collectionview" to your page tag, and then simply use <gv:CollectionView/> in order to add the widget to your page. Use <gv:Gridview.itemTemplate/> to specify the template for each cell:

<!-- test-page.xml -->
<Page xmlns="http://schemas.nativescript.org/tns.xsd" xmlns:gv="@nativescript-community/ui-collectionview" loaded="pageLoaded">
  <GridLayout>
    <gv:CollectionView items="{{ items }}" colWidth="24%" rowHeight="15%" padding="5" itemTap="gridViewItemTap" itemLoading="gridViewItemLoading" loadMoreItems="gridViewLoadMoreItems">
      <gv:CollectionView.itemTemplate>
        <GridLayout backgroundColor="#33ffff" style="margin: 5">
          <Label text="{{ value }}" verticalAlignment="center"/>
        </GridLayout>
      </gv:CollectionView.itemTemplate>
    </gv:CollectionView>
  </GridLayout>
</Page>
// test-page.ts
import { EventData, Observable } from "data/observable";
import { ObservableArray } from "data/observable-array";
import { Page } from "ui/page";

import { CollectionViewItemEventData } from "@nativescript-community/ui-collectionview";

let viewModel: Observable;

export function pageLoaded(args: EventData) {
    const page = args.object as Page;
    const items = new ObservableArray();

    for (let loop = 0; loop < 200; loop++) {
        items.push({ value: "test " + loop.toString() });
    }
    viewModel = new Observable();
    viewModel.set("items", items);

    page.bindingContext = viewModel;
}

export function gridViewItemTap(args: CollectionViewItemEventData) {
    console.log("tap index " + args.index.toString());
}

export function gridViewItemLoading(args: CollectionViewItemEventData) {
    console.log("item loading " + args.index.toString());
}

export function gridViewLoadMoreItems(args: EventData) {
    console.log("load more items");
}

You can also have multiple templates the same way you add them in the builtin ListView control:

<gv:CollectionView id="gv" row="0" class="{{ cssClass }}" items="{{ items }}" 
                colWidth="{{ colWidth }}" rowHeight="{{ rowHeight }}" itemTemplateSelector="templateSelector"
                itemTap="gridViewItemTap" itemLoading="gridViewItemLoading" loadMoreItems="gridViewLoadMoreItems">
    <gv:CollectionView.itemTemplates>
        <template key="odd">
            <GridLayout backgroundColor="#33ffff" style="margin: 10 10 0 0">
                <Label text="{{ value }}" verticalAlignment="center"/>
            </GridLayout>
        </template>

        <template key="even">
            <GridLayout backgroundColor="#33ffff" rows="auto, auto" style="margin: 10 10 0 0">
                <Label row="0" text="{{ value }}" verticalAlignment="center"/>
                <Label row="1" text="{{ value }}" verticalAlignment="center"/>
            </GridLayout>
        </template>
    </gv:CollectionView.itemTemplates>
</gv:CollectionView>
export function templateSelector(item: any, index: number, items: any) {
    return index % 2 === 0 ? "even" : "odd";
}

Usage in Angular

Import CollectionViewModule in your NgModule:

import { CollectionViewModule } from '@nativescript-community/ui-collectionview/angular';

@NgModule({
    //......
    imports: [
        //......
        CollectionViewModule,
        //......
    ],
    //......
})

Example Usage

// app.module.ts
import { CollectionViewModule } from "@nativescript-community/ui-collectionview/angular";

@NgModule({
    bootstrap: [
        AppComponent
    ],
    imports: [
        NativeScriptModule,
        AppRoutingModule,
        CollectionViewModule,
    ],
    declarations: [
        AppComponent,
        ItemsComponent,
        ItemDetailComponent
    ],
    providers: [
        ItemService
    ],
    schemas: [
        NO_ERRORS_SCHEMA
    ]
})
export class AppModule { }
<!-- my.component.html -->
<GridLayout class="page">
  <CollectionView [items]="items" colWidth="30%" rowHeight="100">
    <ng-template let-item="item" let-odd="odd">
      <StackLayout margin="10" [nsRouterLink]="['/item', item.id]" borderColor="blue" borderWidth="2" borderRadius="5" verticalAlignment="stretch" class="list-group-item" [class.odd]="odd">
        <Label verticalAlignment="center" [text]="item.name" class="list-group-item-text" textWrap="true"></Label>
      </StackLayout>
    </ng-template>
  </CollectionView>
</GridLayout>

If you want to use multiple item templates, you can do that very similarly to how you do it for the builtin ListView control. The only difference is that due to current limitations instead of using the nsTemplateKey directive you need to use the cvTemplateKey directive that comes from the CollectionView. (In a future version, once the framework allows it this will be changed and you will be able to use the same directive for the CollectionView as well)

<CollectionView row="1" [items]="items" colWidth="33%" rowHeight="100" [itemTemplateSelector]="templateSelector">
    <ng-template cvTemplateKey="Defender" let-item="item" let-odd="odd">
        <StackLayout [nsRouterLink]="['/item', item.id]" borderColor="blue" borderWidth="2" borderRadius="5" verticalAlignment="stretch" class="list-group-item" [class.odd]="odd">
        <Label verticalAlignment="center" [text]="item.name" class="list-group-item-text" textWrap="true"></Label>
        </StackLayout>
    </ng-template>

    <ng-template cvTemplateKey="Goalkeeper" let-item="item" let-odd="odd">
        <StackLayout [nsRouterLink]="['/item', item.id]" borderColor="black" borderWidth="2" borderRadius="5" verticalAlignment="stretch" class="list-group-item" [class.odd]="odd">
        <Label verticalAlignment="center" [text]="item.name" class="list-group-item-text" textWrap="true"></Label>
        </StackLayout>
    </ng-template>

    <ng-template cvTemplateKey="Midfielder" let-item="item" let-odd="odd">
        <StackLayout [nsRouterLink]="['/item', item.id]" borderColor="yellow" borderWidth="2" borderRadius="5" verticalAlignment="stretch" class="list-group-item" [class.odd]="odd">
        <Label verticalAlignment="center" [text]="item.name" class="list-group-item-text" textWrap="true"></Label>
        </StackLayout>
    </ng-template>

    <ng-template cvTemplateKey="Forward" let-item="item" let-odd="odd">
        <StackLayout [nsRouterLink]="['/item', item.id]" borderColor="red" borderWidth="2" borderRadius="5" verticalAlignment="stretch" class="list-group-item" [class.odd]="odd">
        <Label verticalAlignment="center" [text]="item.name" class="list-group-item-text" textWrap="true"></Label>
        </StackLayout>
    </ng-template>
</CollectionView>

Demos

This repository includes Angular, Vue.js, and Svelte demos. In order to run these execute the following in your shell:

$ git clone https://github.com/@nativescript-community/ui-collectionview
$ cd ui-collectionview
$ npm run setup
$ npm run build
$ cd demo-ng # or demo-vue or demo-svelte
$ ns run ios|android

Keywords

NativeScript

FAQs

Package last updated on 28 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts