欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解支持Angular 2的表格控件

 更新時間:2017年01月19日 09:28:21   作者:啟明  
本文主要對支持Angular2的表格控件進(jìn)行詳細(xì)介紹。具有一定的參考價值,下面跟著小編一起來看下吧

前端框架一直這最近幾年特別火的一個話題,尤其是Angular 2擁有眾多的粉絲。在2016年9月份Angular 2正式發(fā)布之后,大量的粉絲的開始投入到了Angular 2的懷抱。當(dāng)然這其中也包括我。如果你想了解Angular 2,推薦官方網(wǎng)站:英文版中文版。通過快速起步,可以快速體驗Angular 2。

公司的一個項目想基于Angular 2的2.4 版本進(jìn)行開發(fā),目前還在進(jìn)行前期的調(diào)研階段。我擔(dān)當(dāng)?shù)娜蝿?wù)就是研究基于Angular 2的UI控件,在官方網(wǎng)站的資源中列出了很多支持Angular 2的資源。發(fā)現(xiàn)WijmoFlexgrid控件已經(jīng)支持Angular 2的2.4版本,初步滿足我們的需求。

一、環(huán)境搭建

Angular 2不僅是功能上和Angular 1有很多的差別,環(huán)境搭建也是區(qū)別很大。很多初學(xué)者反饋Angular 2的代碼很難運行起來。Angular2是基于ES6來開發(fā)的,所以會有很多第三方依賴。由于很多瀏覽器還不支持ES6,所以Angular2引入了很多polyfill或者shim, 導(dǎo)致我們引入了第三方依賴。下面以FlexGrid為例來說明如何搭建運行環(huán)境。

1、  安裝NodeJS

可以從Node官網(wǎng)下載 https://nodejs.org/en/download/。

2、  新建目錄來存放項目

mkdir ng2-flexGrid

cd ng2-flexGrid

3、  配置文件

package.json

用來標(biāo)記項目需要使用的npm依賴包。

{
 "name": "wj-ng2-flexgrid",
 "version": "1.0.0",
 "scripts": {
 "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
 "lite": "lite-server",
 "tsc": "tsc",
 "tsc:w": "tsc -w"
 },
 "licenses": [
 {
 "type": "MIT",
 "url": "https://github.com/angular/angular.io/blob/master/LICENSE"
 }
 ],
 "dependencies": {
 "@angular/common": "~2.1.1",
 "@angular/compiler": "~2.1.1",
 "@angular/core": "~2.1.1",
 "@angular/forms": "~2.1.1",
 "@angular/http": "~2.1.1",
 "@angular/platform-browser": "~2.1.1",
 "@angular/platform-browser-dynamic": "~2.1.1",
 "@angular/router": "~3.1.1",
 "@angular/upgrade": "~2.1.1",
 "angular-in-memory-web-api": "~0.1.13",
 "core-js": "^2.4.1",
 "reflect-metadata": "^0.1.8",
 "rxjs": "5.0.0-beta.12",
 "systemjs": "0.19.39",
 "zone.js": "^0.6.25"
 },
 "devDependencies": {
 "@types/core-js": "^0.9.34",
 "@types/node": "^6.0.45",
 "concurrently": "^3.0.0",
 "lite-server": "^2.2.2",
 "typescript": "^2.0.3"
 }
}

tsconfig.json

TypeScript的配置文件,定義TypeScript 編譯器如何從項目源文件生成 JavaScript 代碼。

{
 "compilerOptions": {
 "target": "es5",
 "module": "commonjs",
 "moduleResolution": "node",
 "sourceMap": true,
 "emitDecoratorMetadata": true,
 "experimentalDecorators": true,
 "removeComments": false,
 "noImplicitAny": false
 }
}

systemjs.config.js

為SystemJS(模塊加載器)提供到哪里查找應(yīng)用模塊的信息,并注冊了所有必備的依賴包。

/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
 System.config({
 paths: {
 // paths serve as alias
 'npm:': 'node_modules/'
 },
 // map tells the System loader where to look for things
 map: {
 // our app is within the app folder
 app: 'app',
 // angular bundles
 '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
 '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
 '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
 '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
 '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
 '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
 '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
 '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
 '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
 // other libraries
 'rxjs':   'npm:rxjs',
 'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
 },
 // packages tells the System loader how to load when no filename and/or no extension
 packages: {
 app: {
 main: './main.js',
 defaultExtension: 'js'
 },
 rxjs: {
 defaultExtension: 'js'
 }
 }
 });
})(this);

4、  運行npm install

 

NPM會根據(jù)package.json中定義的包進(jìn)行安裝。會產(chǎn)生一個node_modules目錄,將這些包放在這里。

至此環(huán)境搭建的任務(wù)就已經(jīng)完成了。下面我們以FlexGrid為例說明支持Angular 2。

二、支持Angular 2的表格控件如何使用

1、HTML

<html>
<head>
 <meta charset="UTF-8">
 <title>使用 Angular 2 來創(chuàng)建FlexGrid控件</title>
 <!--angular 2 模塊-->
 <!--用于填充舊版瀏覽器-->
 <script src="node_modules/core-js/client/shim.min.js"></script>
 <script src="node_modules/zone.js/dist/zone.js"></script>
 <script src="node_modules/reflect-metadata/Reflect.js"></script>
 <script src="node_modules/systemjs/dist/system.src.js"></script>
 <!--systemjs 配置-->
 <script src="systemjs.config.js"></script>
 <!--wijmo 模塊-->
 <script src="scripts/vendor/wijmo.min.js"></script>
 <script src="scripts/vendor/wijmo.grid.min.js"></script>
 <link rel="stylesheet" href="styles/wijmo.min.css">
 <script src="scripts/vendor/wijmo.angular2.min.js"></script>
 <!--mine-->
 <script>
 System.import('./app/main').catch(function(err){ console.error(err); });
 </script>
</head>
<body>
 <!--申明根組件-->
 <app-cmp>
 Loading
 </app-cmp>
</body>
</html>

在HTML宿主頁面中,除了Angular 2中必須的組件,還需要引入Wijmo腳本。

2、編寫數(shù)據(jù)服務(wù)

'use strict'
import { Injectable } from '@angular/core';
@Injectable()
export class DataService {
 getData(count: number): wijmo.collections.ObservableArray {
 var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(','),
  data = new wijmo.collections.ObservableArray();
 for (var i = 0; i < count; i++) {
  data.push({
  id: i,
  country: countries[i % countries.length],
  date: new Date(2014, i % 12, i % 28),
  amount: Math.random() * 10000,
  active: i % 4 == 0
  });
 }
 return data;
 }
}

3、編寫根組件

現(xiàn)在我們編寫應(yīng)用的第一個組件:根組件 app.component ,也是這個程序唯一的組件。在這個組件中,需要引入兩個元標(biāo)記:Component, Inject。還需要注入定義的數(shù)據(jù)服務(wù)data.Service。

app.component.ts:

import { Component, Inject } from '@angular/core';
import { DataService } from '../services/data.service';
@Component ({
 selector:'app-cmp',
 templateUrl:'app/components/app.component.html',
})
export class AppComponent{
 protected dataSvc:DataService;
 data: wijmo.collections.CollectionView;
 constructor(@Inject(DataService) dataSvc:DataService){
 this.dataSvc = dataSvc;
 this.data = new wijmo.collections.CollectionView(this.dataSvc.getData(50));
 }
}

app.component.html:

<div class="header">
 <h2>
 展示如何在angular 2上使用 Wijmo的FlexGrid。
 </h2>
</div>
<div>
<wj-flex-grid [itemsSource]="data"> </wj-flex-grid>
</div>

在這里僅僅需要引入wj-flex-grid標(biāo)記,就可以創(chuàng)建FlexGrid控件。wj-flex-grid 組件是作為一個子組件存在,在app.module 模塊中注入。itemsSource 綁定一個數(shù)據(jù)源,這個itemsSource是flexgrid已經(jīng)封裝完成的屬性。

在Angular 2下使用FlexGrid的最大好處就是:Angular 2組件提供了使用標(biāo)記語言來聲明控件的能力。聲明標(biāo)記很好地遵循了MVVM設(shè)計模式,我們可以完全通過View(標(biāo)記語言)來配置我們的組件。FlexGrid支持使用Angular 2標(biāo)記語言來聲明完整的API。你完全可以使用標(biāo)記語言設(shè)置屬性,附加事件,配置子組件。

4、編寫根模塊

在根模塊中將組件注入,需要將引用的所有的組件和模塊都要注入進(jìn)來。

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { WjGridModule } from 'wijmo/wijmo.angular2.grid';
import { AppComponent } from './components/app.component';
import { DataService } from './services/data.service';
@NgModule({
 imports: [ WjGridModule, BrowserModule],
 declarations: [AppComponent],
 providers:[DataService],
 bootstrap: [AppComponent],
})
export class AppModule { }

5、引導(dǎo)程序

main.ts:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import {enableProdMode} from '@angular/core';
import { AppModule } from './app.module';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);

三、運行

在命令行執(zhí)行 npm start,這時,程序會自動打開默認(rèn)瀏覽器并渲染頁面。

start 命令是執(zhí)行定義在 package.json 文件中的scripts命令。 會將ts代碼編譯為原生js,并且會啟動一個靜態(tài)服務(wù)器。 這個服務(wù)器會檢測文件的變化,當(dāng)發(fā)現(xiàn)文件改動,那么會自動編譯ts代碼。

下面是運行的結(jié)果:

FlexGrid內(nèi)置的基本功能比如:排序、過濾、分組、編輯等,也以通過可選的擴(kuò)展來提供其他功能。FlexGrid和其它產(chǎn)品比較,性能還是不錯的。它的文件尺寸比較小壓縮后約25K。

下載源代碼:http://xiazai.jb51.net/201701/yuanma/ng2-flexGrid_jb51.rar

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

  • Webpack 實現(xiàn) AngularJS 的延遲加載

    Webpack 實現(xiàn) AngularJS 的延遲加載

    這篇文章主要介紹了Webpack 實現(xiàn) AngularJS 的延遲加載的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • Angular.js中ng-include用法及多標(biāo)簽頁面的實現(xiàn)方式詳解

    Angular.js中ng-include用法及多標(biāo)簽頁面的實現(xiàn)方式詳解

    這篇文章主要給大家介紹了在Angular.js中ng-include用法及多標(biāo)簽頁面的實現(xiàn)方式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),相信對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-05-05
  • Angular Universal服務(wù)器端渲染避免 window is not defined錯誤消息

    Angular Universal服務(wù)器端渲染避免 window is not&

    這篇文章主要介紹了Angular Universal服務(wù)器端渲染避免 window is not defined錯誤消息,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • 讓angularjs支持瀏覽器自動填表

    讓angularjs支持瀏覽器自動填表

    簡單來說Angular.js是google開發(fā)者設(shè)計和開發(fā)的一套前端開發(fā)框架,幫助你簡化前端開發(fā)的負(fù)擔(dān)。當(dāng)然,這里有很多其它的前端開發(fā)框架,但是如何選擇合適的前端框架對于我們這些開發(fā)人員來說就不是那么容易了!
    2014-11-11
  • AngularJS 中括號的作用詳解

    AngularJS 中括號的作用詳解

    這篇文章主要介紹了AngularJS 中括號的作用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • 詳解JavaScript的AngularJS框架中的作用域與數(shù)據(jù)綁定

    詳解JavaScript的AngularJS框架中的作用域與數(shù)據(jù)綁定

    這篇文章主要介紹了JavaScript的AngularJS框架中的作用域與數(shù)據(jù)綁定,包括作用域的繼承以及數(shù)據(jù)的單向和雙向綁定等重要知識點,需要的朋友可以參考下
    2016-03-03
  • AngularJS基于ngInfiniteScroll實現(xiàn)下拉滾動加載的方法

    AngularJS基于ngInfiniteScroll實現(xiàn)下拉滾動加載的方法

    這篇文章主要介紹了AngularJS基于ngInfiniteScroll實現(xiàn)下拉滾動加載的方法,結(jié)合實例形式分析AngularJS下拉滾動插件ngInfiniteScroll的下載、功能、屬性及相關(guān)使用方法,需要的朋友可以參考下
    2016-12-12
  • AngularJS中下拉框的基本用法示例

    AngularJS中下拉框的基本用法示例

    這篇文章主要介紹了AngularJS中下拉框的基本用法,結(jié)合具體實例形式分析了AngularJS下拉框的元素綁定、選中及顯示等功能實現(xiàn)方法,需要的朋友可以參考下
    2017-10-10
  • Angular中使用$watch監(jiān)聽object屬性值的變化(詳解)

    Angular中使用$watch監(jiān)聽object屬性值的變化(詳解)

    下面小編就為大家?guī)硪黄狝ngular中使用$watch監(jiān)聽object屬性值的變化(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Angular.js中定時器循環(huán)的3種方法總結(jié)

    Angular.js中定時器循環(huán)的3種方法總結(jié)

    這篇文章主要給大家總結(jié)了angular.js中定時器循環(huán)的3種方法,分別是利用$interlval實現(xiàn)、$timeout的遞歸調(diào)用來實現(xiàn)以及$timeout借助arguments.callee來實現(xiàn),每種方法都給出了詳細(xì)的示例艾瑪供大家學(xué)習(xí)參考,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-04-04

最新評論