ng-grid入手

王朝学院·作者佚名  2016-05-22
窄屏简体版  字體: 小  |  中  |  大  |  超大  

ng-grid入手简介

ng-grid是基于AngularJS和JQuery的富表格控件,由AngularUI Team领衔开发,到目前为止已有2354次Commit,1076个Fork。

AngualrUI:http://angular-ui.github.io/

ng-grid: http://angular-ui.github.io/ng-grid/

ng-grid(code on Github): https://github.com/angular-ui/ng-grid

入手(官网参照)

1.创建HTML页面和与之相对应的JS,CSS文件

ng-index.html

js/ng-index.js

css/ng-index.css

2.在ng-index.html中添加ng-grid所依赖的JQuery和AngularJS框架。

下载到本地:

JQuery:http://code.jquery.com/

Angularjs:https://angularjs.org/

在线(进入后选择适合版本):

JQuery:http://www.bootcdn.cn/jquery/

Angularjs:http://www.bootcdn.cn/angular.js/

3.引入ng-grid的JS和CSS文件

下载到本地: http://angular-ui.github.io/ng-grid/

在线:http://www.bootcdn.cn/ng-grid/

4.在ng-index.html中添加如下标签。

<div class="gridStyle" ng-grid="gridOptions" />

?gridStyle: 表格的Style控制,写入ng-index.css中

?gridOptions: ng-index.js中表格所在的Controller的$scope调用赋值(AngularJS数据绑定)

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>ng-index</title><!-- Basic References: JQuery/AngualrJS --><script type="text/javascript" src="http://cdn.bootcss.com/jquery/2.1.3/jquery.min.js"></script><script type="text/Javascript" src="http://cdn.bootcss.com/angular.js/1.3.8/angular.min.js"></script><!-- ng-grid --><link rel="stylesheet" href="http://cdn.bootcss.com/ng-grid/2.0.11/ng-grid.min.css"/><script src="http://cdn.bootcss.com/ng-grid/2.0.11/ng-grid.min.js"></script><!--local: ng-index.html --><script src="js/ng-index.js"></script><link rel="stylesheet" href="css/ng-index.css"/></head><body ng-app="indexApp"> <div id="container" ng-controller="gridCtrl"> <div class="gridStyle" ng-grid="gridOptions" /> </div></body></html>

ViewCode(ng-index.html)

5.编写ng-index.css文件

.gridStyle { height:300px;}

ViewCode(ng-index.css)?HTML页面中表格为div标签,数据在AngularJS中数据绑定到页面中,所以需要指定div标签的高度。

6.编写ng-index.js文件

var indexApp = angular.module('indexApp', ['ngGrid']);indexApp.controller('gridCtrl', function($scope) { $scope.members = [{name: "Moroni", age: 50}, {name: "Tiancum", age: 43}, {name: "Jacob", age: 27}, {name: "Nephi", age: 29}, {name: "Enos", age: 34}]; $scope.gridOptions = { data: 'members' };});

ViewCode(ng-index.js)?官网给的例子在用['ui.grid']是错误的,要改成['ngGrid'],不然Angular初始化不成功。

?将要显示的JSON数据赋值给页面控件的data属性,即可绑定到界面显示。

?表头名称默认显示JSON数据中当字段的字段名称。

7.设置表头显示名称(name→姓名age→年龄)。

在表格的配置属性中添加columnDefs的配置,详细参照以下代码。

var indexApp = angular.module('indexApp', ['ngGrid']);indexApp.controller('gridCtrl', function($scope) { $scope.members = [{name: "Moroni", age: 50}, {name: "Tiancum", age: 43}, {name: "Jacob", age: 27}, {name: "Nephi", age: 29}, {name: "Enos", age: 34}]; $scope.gridOptions = { data: 'members', columnDefs: [{field: 'name', displayName: '姓名'}, {field:'age', displayName:'年龄'}] };});

ViewCode(ng-index.js)

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航