我正在使用从json文件加载数据的ng-grid,我想使用angular js编辑和删除按钮来编辑和删除我的数据?

时间:2015-06-29 11:10:20

标签: javascript json angularjs

我已经创建了一个编辑按钮,但当我点击按钮并编辑我的数据时,它不会反映到它隐藏先前数据的行中,我想实现一个功能,使我能够编辑以前的数据保持在行中,并在再次单击编辑按钮时显示并反映我所做的更改

这是我的代码main.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: false,
        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.name"/></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.city"/></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.Address"/></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.PinCode"/></div></div>'
            },
         {
            displayName: 'Edit',
            cellTemplate: '<button id="editBtn" type="button" class="btn btn-primary" ng-click="edit(row)" >Modify</button> '

        }]
    };

});

你可以在下面的plunker上看到它是链接

http://plnkr.co/edit/QbsQ6uDgNxts9TUMERj2?p=preview

2 个答案:

答案 0 :(得分:0)

你并不是真的在任何地方保存数据,我在你的js文件中看到的是你只是在点击你的按钮上使行可编辑和不可编辑

row.entity.edit = !row.entity.edit;

添加一些代码以将数据保存到实体中或尝试此

http://brianhann.com/create-a-modal-row-editor-for-ui-grid-in-minutes/

,这将解决您的问题

答案 1 :(得分:0)

在你的小提琴中将你的main.js改为follownig(我改变了ng-model)

网格定义中的

&#13;
&#13;
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: false,
        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.entity.nm}}</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.entity.cty}}</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.entity.hse}}</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.entity.yrs}}</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> '

        }]
    };

});
&#13;
&#13;
&#13;