Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

All Blogs

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    Tags: ng-grid

    comments

    Introduction

    Today I tried out Angularjs and posted about it. After that it was time to add a grid to make my data look better. And I picked ng-grid.

    Installation

    These are the packages I am using.

    • angularjs 1.0.4
    • jQuery 1.9.0
    • Moment.js 1.7.2
    • ng-grid 1.6.0

    But the ng-grid package does not contain the scripts. So I had to get those manually. I took the ones on github. The ng-grid.1.6.3.js and ng-grid.css.

    Server

    See my previous post on angularjs for this.

    Client

    My app.js changes a little.

    1. angular.module('plantsapp', ['ngGrid']).
    2.   config(['$routeProvider', function ($routeProvider) {
    3.       $routeProvider.
    4.           when('/plants', { templateUrl: 'partials/plants.html', controller: PlantsController }).
    5.           when('/plants/:Id', { templateUrl: 'partials/plant.html', controller: PlantController }).
    6.           otherwise({ redirectTo: '/plants' });
    7.   }]);

    I just added the ngGrid string.

    My controller.js will change a little too. And then specifically my PlantsController.

    1. function PlantsController($scope, $http) {
    2.     $scope.plants = [];
    3.     $scope.myOptions = {
    4.         data: 'plants'
    5.     };
    6.     $http.get('http://localhost:59025/plants').success(function (thisdata) {
    7.         $scope.plants = thisdata;
    8.     });
    9. }

    See how I instantiate my plants outside the get. And then I add the myOptions line.

    In the Index.html I will add these two lines.

    1. <link href="css/ng-grid.css" rel="stylesheet" />
    2.     <script src="/Scripts/jquery-1.9.0.min.js"></script>
    3.     <script src="/Scripts/ng-grid.js"></script>

    And the plants.html file changes to this.

    1. <div ng-grid="myOptions"></div>

    The myOptions is the same as what property I added in my PlantsController.

    And tada.

    But you might see that the date is not well formatted. Let's fix that.

    Formatting the date

    First we need to add moment.js import to our index.html.

    Then we need to add a filter to our app.js.

    Like this.

    1. angular.module('plantsapp', ['ngGrid']).
    2.   config(['$routeProvider', function ($routeProvider) {
    3.       $routeProvider.
    4.           when('/plants', { templateUrl: 'partials/plants.html', controller: PlantsController }).
    5.           when('/plants/:Id', { templateUrl: 'partials/plant.html', controller: PlantController }).
    6.           otherwise({ redirectTo: '/plants' });
    7.   }]).filter('moment', function () {
    8.       return function (dateString, format) {
    9.           return moment(dateString).format(format);
    10.       };
    11.   });;

    And now we need to change our controller so that we can add this filter to our column definition for DateAdded.

    Like this.

    1. function PlantsController($scope, $http) {
    2.     $scope.plants = [];
    3.     $scope.myOptions = {
    4.         data: 'plants',
    5.         columnDefs: [
    6.                         {field:'Id', displayName:'Id'},
    7.                         {field:'Name', displayName:'Name'},
    8.                         { field: 'Genus', displayName: 'Genus' },
    9.                         { field: 'DateAdded', displayName: 'Date added', cellFilter:"moment:'DD/MM/YYYY h:m'" }
    10.         ]
    11.     };
    12.     $http.get('http://localhost:59025/plants').success(function (thisdata) {
    13.         $scope.plants = thisdata;
    14.     });
    15. }

    Of course we could set our fofrmat straight in the filter and and then just use the moment filter.

    For this we just need to change the filter to this.

    1. return moment(dateString).format('DD/MM/YYYY h:m');

    and the columndefinition then becomes this.

    1. { field: 'DateAdded', displayName: 'Date added', cellFilter:"moment" }

    And tada!!!!

    Conclusion

    Once you get it ng-grid is pretty easy to set up. I just didn't get it for the first hour or so. Because I had my $scope.myOptions in my Get function, because that looked so much like what they did in the getting started things. But I was wrong.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    InstapaperVote on HN