This section details how to bind ng Grid to a local data model. It should be fairly straight-forward, and will allow us to bind the model quickly To set up the basic ng Grid,
- public class HomeController: Controller
- {
- public ActionResult NgGrid()
- {
- return View();
- }
- public JsonResult NgGridDetails()
- {
- List < Student > stdList = new List < Student > ();
- stdList.Add(new Student
- {
- Id = 1, FName = "Sachin", LName = "Tendulkar", Class = 1
- });
- stdList.Add(new Student
- {
- Id = 2, FName = "Saurav", LName = "Ganguly", Class = 2
- });
- stdList.Add(new Student
- {
- Id = 3, FName = "VVS", LName = "Laxman", Class = 3
- });
- stdList.Add(new Student
- {
- Id = 4, FName = "MS", LName = "Dhoni", Class = 4
- });
- return Json(stdList, JsonRequestBehavior.AllowGet);
- }
- }
- public class Student
- {
- public int Id
- {
- get;
- set;
- }
- public string FName
- {
- get;
- set;
- }
- public string LName
- {
- get;
- set;
- }
- public int Class
- {
- get;
- set;
- }
- }
NgGrid View- @{
- ViewBag.Title = "NgGrid";
- Layout = null;
- }
-
- <h2>NgGrid</h2>
- <script src="~/Scripts/jquery-1.10.2.js"></script>
- <script src="~/Scripts/angular.js"></script>
- <script src="~/Scripts/ng-grid.min.js"></script>
- <script src="~/Scripts/Javascripts/Nggrid Ang.js"></script>
- <link href="~/Content/ng-grid.css" rel="stylesheet" />
- <style type="text/css">
- .gridStyle {
- border: 1px solid #d4d4d4;
- width: 400px;
- height: 300px
- }
- </style>
- <div ng-app="Mygrid">
- <div ng-controller="gridCtrl">
- <strong>Filter:</strong><input type="text" ng-model="filterOptions.filterText" />
- <div class="gridStyle" ng-grid="ngGridView"></div>
-
- </div>
- </div>
NggridAng.js - var MyApp = angular.module('Mygrid', ['ngGrid']);
- MyApp.controller('gridCtrl', ['$scope', '$http', 'services', function (scope, http, ser)
- {
- scope.filterOptions = {
- filterText: ''
- };
- ser.getStudentDetails()
- .success(function (response)
- {
- scope.persons = response;
- });
- scope.ngGridView = {
- data: 'persons',
- filterOptions: scope.filterOptions
- }
- }])
- MyApp.service('services', function ($http)
- {
- this.getStudentDetails = function ()
- {
- var result = $http.get('Home/NgGridDetails');
- return result;
- };
- });