Anky
Anky

Reputation: 270

How to show newly added record in top row of ng-grid?

I have a ng-grid when I add a record into that the row gets inserted at the bottom of the grid, I want to display the newly added row in the top of my grid so that a user would be able to know which data is added.

here is my app.js

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope, $http) {

    $scope.filterOptions = {
        filterText: "",
        useExternalFilter: true
    };

    $scope.totalServerItems = 0;
    $scope.pagingOptions = {
        pageSizes: [5, 10, 20],
        pageSize: 5,
        currentPage: 1
    };
    $scope.setPagingData = function(data, page, pageSize){
        var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
        $scope.myData = pagedData;
        $scope.totalServerItems = data.length;
        if (!$scope.$$phase) {
            $scope.$apply();
        }
    };
    $scope.getPagedDataAsync = function (pageSize, page, searchText) {
        setTimeout(function () {
            var data;
            if (searchText) {
                var ft = searchText.toLowerCase();
                $http.get('largeLoad.json').success(function (largeLoad) {
                    data = largeLoad.filter(function(item) {
                        return JSON.stringify(item).toLowerCase().indexOf(ft) != -1;
                    });
                    $scope.setPagingData(data,page,pageSize);
                });
            } else {
                $http.get('largeLoad.json').success(function (largeLoad) {
                    $scope.setPagingData(largeLoad,page,pageSize);
                });
            }
        }, 100);
    };

    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);

    $scope.$watch('pagingOptions', function (newVal, oldVal) {
        if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) {
            $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
        }
    }, true);

    $scope.$watch('filterOptions', function (newVal, oldVal) {
        if (newVal !== oldVal) {
            $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
        }
    }, true);

    $scope.edit = function (row) {
        row.entity.edit = !row.entity.edit;
    };

    $scope.gridOptions = {
        data: 'myData',
        enableRowSelection: true,
        showGroupPanel: true,
        enableCellSelection: false,
        jqueryUIDraggable: true,
        enablePaging: true,
        showFooter: true,
        totalServerItems:'totalServerItems',
        pagingOptions: $scope.pagingOptions,
        filterOptions: $scope.filterOptions,
        columnDefs: [{
            field: 'nm',
            displayName: 'Name',
            cellTemplate: '<div class="ngCellText"><div ng-show="!row.entity.edit">{{row.getProperty(col.field)}}</div>' +
            '<div ng-show="row.entity.edit" class="ngCellText"><input type="text" ng-model="row.entity.nm"/></div></div>'
        },
            {
            field: 'cty',
            displayName: 'city',
            cellTemplate: '<div class="ngCellText"><div ng-show="!row.entity.edit">{{row.getProperty(col.field)}}</div>' +
            '<div ng-show="row.entity.edit" class="ngCellText"><input type="text" ng-model="row.entity.cty"/></div></div>'
        },
            {
            field: 'hse',
            displayName: 'Address',
            cellTemplate: '<div class="ngCellText"><div ng-show="!row.entity.edit">{{row.getProperty(col.field)}}</div>' +
            '<div ng-show="row.entity.edit" class="ngCellText"><input type="text" ng-model="row.entity.hse"/></div></div>'
            },

            {
                field: 'yrs',
                displayName: 'PinCode',
                cellTemplate: '<div class="ngCellText"><div ng-show="!row.entity.edit">{{row.getProperty(col.field)}}</div>' +
                '<div ng-show="row.entity.edit" class="ngCellText"><input type="text" ng-model="row.entity.yrs"/></div></div>'
            },
         {
            displayName: 'Edit',
            cellTemplate: '<button id="editBtn" type="button" class="btn btn-primary" ng-click="edit(row)" >Modify</button> '

        },
            {
                displayName: 'Remove',
                cellTemplate: '<button id="removebtn" type="button" class="btn btn-primary" ng-click="removeRow($index)" >Remove</button> '
            }

    ]
    };

    $scope.removeRow = function() {
        var index = this.row.rowIndex;
        $scope.gridOptions.selectItem(index, false);
        $scope.myData.splice(index, 1);
    };

This is my addRow function; when this function gets executed a row gets inserted at the bottom of the grid I want that this new row to be displayed at the top most whenever the add button is clicked

$scope.addRow = function() {
        $scope.myData.push({nm: 'abc', cty: 0 , hse:'abcd' , yrs:2014});
    };

});

here is my code on plunker: http://plnkr.co/edit/QbsQ6uDgNxts9TUMERj2?p=info

Upvotes: 1

Views: 1039

Answers (1)

Kurt Du Bois
Kurt Du Bois

Reputation: 7665

When you push data into an array, javascript will always put it at the end. A possible solution would be to use unshift in stead of push:

The unshift() method adds new items to the beginning of an array, and returns the new length.

Upvotes: 2

Related Questions