Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Grid in AngularJS using UI Grid

UI-Grid 3.0 (formerly ng-grid), is a 100% angular grid written with no dependencies other than AngularJS. It is designed around a core grid module and features are layered on as angular modules and directives. This keeps the core small and focused while executing very complex features only when you need them.
In the core module, you get:
  • Virtualized rows and columns - only the rows and columns visible in the viewport (+ some extra margin) are actually rendered
  • Bind cells to complex properties and functions
  • Column sorting with three states: Asc, Desc, None
  • Column filtering
  • Ability to change header and cell contents with custom templates
  • i18nService allows label translations
In this example we create the most basic grid possible.
Steps:
  • Include uiGrid in your script and css

    1. <link rel="styleSheet" href="release/ui-grid.min.css"/>

    2. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js"></script>

    3. <script src="/release/ui-grid.min.js"></script>


  • Include ui.grid module as a dependency in your app

    1. var app = angular.module('app', ['ui.grid']);


  • Add a css style to your app css so the grid knows it's dimensions

    1. .myGrid {

    2. width: 500px;

    3. height: 250px;

    4. }


  • Add an array of data to a property on your $scope

    1. $scope.myData = [

    2. {

    3. "firstName": "Cox",

    4. "lastName": "Carney"...


  • Use the ui-grid directive and specify a json object with a data property referencing your $scope.myData property.

    1. <div ng-controller="MainCtrl">

    2. <div ui-grid="{ data: myData }" class="myGrid"></div>

    3. </div>


Example

Source

index.html


  1. <!doctype html>
  2. <html ng-app="app">
  3. <head>
  4. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.js"></script>
  5. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular-touch.js"></script>
  6. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular-animate.js"></script>
  7. <script src="http://ui-grid.info/docs/grunt-scripts/csv.js"></script>
  8. <script src="http://ui-grid.info/docs/grunt-scripts/pdfmake.js"></script>
  9. <script src="http://ui-grid.info/docs/grunt-scripts/vfs_fonts.js"></script>
  10. <script src="/release/ui-grid.js"></script>
  11. <script src="/release/ui-grid.css"></script>
  12. <script src="app.js"></script>
  13. </head>
  14. <body>
  15. <div ng-controller="MainCtrl">
  16. <div id="grid1" ui-grid="{ data: myData }" class="grid"></div>
  17. </div>
  18. </body>
  19. </html>

main.css

  1. .grid {
  2. width: 500px;
  3. height: 250px;
  4. }

app.js

  1. var app = angular.module('app', ['ngTouch', 'ui.grid']);
  2.  
  3. app.controller('MainCtrl', ['$scope', function ($scope) {
  4.  
  5. $scope.myData = [
  6. {
  7. "firstName": "Cox",
  8. "lastName": "Carney",
  9. "company": "Enormo",
  10. "employed": true
  11. },
  12. {
  13. "firstName": "Lorraine",
  14. "lastName": "Wise",
  15. "company": "Comveyer",
  16. "employed": false
  17. },
  18. {
  19. "firstName": "Nancy",
  20. "lastName": "Waters",
  21. "company": "Fuelton",
  22. "employed": false
  23. }
  24. ];
  25. }]);

Source: http://ui-grid.info/docs/#/tutorial/101_intro


This post first appeared on Life-Health-Finance-Tech-Others, please read the originial post: here

Share the post

Grid in AngularJS using UI Grid

×

Subscribe to Life-health-finance-tech-others

Get updates delivered right to your inbox!

Thank you for your subscription

×