AnjularJS ngTable外掛程式使用備忘記錄(一個ngTableDemo)

來源:互聯網
上載者:User

標籤:

項目中用到AngularJS的表格ng-table,功能相當強大,基本的排序、分頁等都有。這裡做個備忘,以便以後使用。

該Demo利用vs2012中的webapi(暫時沒連資料庫),下面是詳細代碼

 

ngTable.html

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <title></title>    <link href="Content/bootstrap/bootstrap.css" rel="stylesheet" />    <script src="Scripts/anjularJS/angular.min.js"></script>    <script src="Scripts/jquery-1.9.0.js"></script>    <script src="Scripts/ng-table/ng-table.js"></script>    <link href="Scripts/ng-table/ng-table.css" rel="stylesheet" />    <script src="Scripts/angular-ui/ui-bootstrap-tpls.js"></script>    <script src="Scripts/angular-ui/ui-bootstrap.js"></script>    <script src="myTable.js"></script></head><body >    <div  ng-app="myTable" ng-controller="table1Ctrl">        <h3>AnjularJS ngTable外掛程式使用</h3>        <table ng-table="tableParams" class="table table-condensed table-bordered table-striped" style="text-align:center" >            <tr ng-repeat="user in $data">                <td title="‘ID‘"  sortable="‘ID‘">{{user.ID}}</td>                <td title="‘Name‘"  sortable="‘Name‘">{{user.Name}}</td>                <td title="‘Age‘" sortable="‘Age‘">{{user.Age}}</td>                <td title="‘Email‘"  sortable="‘Email‘">{{user.Email}}</td>            </tr>        </table>    </div></body></html>

 

 

myTable.js

var app = angular.module(‘myTable‘, [‘ngTable‘, ‘ui.bootstrap‘]);app.factory(‘UsersFactory‘, function ($http, $rootScope) {    return {        getUsers: function () {            var users = $http.get("http://localhost:7366/api/User/GetUser");            return users;//正常        }    };});app.controller("table1Ctrl", function ($scope, NgTableParams, UsersFactory) {    $scope.GetUsers = function () {        UsersFactory.getUsers().success(function (data) {            //alert(data);            $scope.tableParams = new NgTableParams({ count: 10 }, { counts: [], dataset: data });        }).error(function (data) {            $scope.error = "An Error has occured while Loading LeagueTable! " + data.ExceptionMessage;        });    }    $scope.GetUsers();});

 

 

Models中的User.cs

    public class User    {        public int ID { get; set; }        public string Name { get; set; }        public int Age { get; set; }        public string Email { get; set; }     }

 

 

App_Start中的WebApiConfig.cs路由配置

public static class WebApiConfig    {        public static void Register(HttpConfiguration config)        {            config.Routes.MapHttpRoute(                name: "DefaultApi",                routeTemplate: "api/{controller}/{action}/{id}",                defaults: new { id = RouteParameter.Optional }            );        }    }

 

Controllers中的UserController.cs

using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Http;using System.Web.Http;using XZDemo.Models;namespace XZDemo.Controllers{    public class UserController : ApiController    {        //        // GET: /User/        [HttpGet]        [ActionName("GetUser")]        public List<User> GetUser()        {            List<User> users = new List<User> {                new User(){ ID=1,Name="aa", Age=1,Email="[email protected]"},                new User(){ ID=2,Name="bb", Age=1,Email="[email protected]" },                new User(){ ID=3,Name="cc", Age=1,Email="[email protected]"},                new User(){ ID=4,Name="dd", Age=1,Email="[email protected]"},                new User(){ ID=5,Name="ee", Age=1,Email="[email protected]"},                new User(){ ID=6,Name="ff", Age=1,Email="[email protected]"},                new User(){ ID=7,Name="gg", Age=1,Email="[email protected]"},                new User(){ ID=8,Name="hh", Age=1,Email="[email protected]"},                new User(){ ID=9,Name="ii", Age=1,Email="[email protected]"},                new User(){ ID=10,Name="jj", Age=1,Email="[email protected]"},                new User(){ ID=11,Name="kk", Age=1,Email="[email protected]"},                new User(){ ID=12,Name="ll", Age=1,Email="[email protected]"},                new User(){ ID=13,Name="mm", Age=1,Email="[email protected]"},                new User(){ ID=14,Name="nn", Age=1,Email="[email protected]"},                new User(){ ID=15,Name="oo", Age=1,Email="[email protected]"},                new User(){ ID=16,Name="pp", Age=1,Email="[email protected]"},                new User(){ ID=17,Name="qq", Age=1,Email="[email protected]"}            };            return users;        }    }}

截屏

該Demo使用到的js和css,點擊下載

AnjularJS-ngTable-js&css.zip

AnjularJS ngTable外掛程式使用備忘記錄(一個ngTableDemo)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.