GithubHelp home page GithubHelp logo

ackuser / nativescript-grid-view Goto Github PK

View Code? Open in Web Editor NEW

This project forked from peterstaev/nativescript-grid-view

1.0 2.0 0.0 6.57 MB

A NatvieScript GridView widget.

License: Apache License 2.0

TypeScript 88.64% CSS 0.77% HTML 5.17% JavaScript 5.42%

nativescript-grid-view's Introduction

NativeScript GridView widget

Build Status npm downloads npm downloads npm

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

Screenshot

Screenshot of Android

Instalation

Run the following command from the root of your project:

tns plugin add nativescript-grid-view

This command automatically installs the necessary files, as well as stores nativescript-grid-view as a dependency in your project's package.json file.

Usage

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

API

Events

  • itemLoading
    Triggered when generating an item in the GridView.

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

  • 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 GridView. 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 GridView.

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

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

Instance Methods

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

Example

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

import { GridItemEventData } from "nativescript-grid-view";

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: GridItemEventData) {
    console.log("tap index " + args.index.toString());
}

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

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

Angular:

Import GridViewModule in your NgModule:

import { GridViewModule } from 'nativescript-grid-view/angular';

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

Example Usage

// app.module.ts
import { GridViewModule } from "nativescript-grid-view/angular";

@NgModule({
    bootstrap: [
        AppComponent
    ],
    imports: [
        NativeScriptModule,
        AppRoutingModule,
        GridViewModule,
    ],
    declarations: [
        AppComponent,
        ItemsComponent,
        ItemDetailComponent
    ],
    providers: [
        ItemService
    ],
    schemas: [
        NO_ERRORS_SCHEMA
    ]
})
export class AppModule { }
<!-- my.component.html -->
<GridLayout class="page">
  <GridView [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>
  </GridView>
</GridLayout>

Working with Webpack+Uglify

In case you are uing webpack and also are minifying/uglifying your code, there are some specific names that should be excluded from the uglification for the widget to work properly. The GridView widget exports those and you need to add them to the mangle exclude option of the uglifyjs plugin in the webpack.common.js file:

var gridViewMangleExcludes = require("nativescript-grid-view/uglify-mangle-excludes").default;
//......
module.exports = function (platform, destinationApp) {
    //......
    if (process.env.npm_config_uglify) {
        plugins.push(new webpack.LoaderOptionsPlugin({
            minimize: true
        }));

        //Work around an Android issue by setting compress = false
        var compress = platform !== "android";
        plugins.push(new webpack.optimize.UglifyJsPlugin({
            mangle: {
                except: nsWebpack.uglifyMangleExcludes.concat(gridViewMangleExcludes),
            },
            compress: compress,
        }));
    }
   //......
}

Donate

bitcoin:14fjysmpwLvSsAskvLASw6ek5XfhTzskHC

Donate

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.