esvit / ng-table

Simple table with sorting and filtering on AngularJS
http://esvit.github.io/ng-table
BSD 3-Clause "New" or "Revised" License
2.77k stars 851 forks source link

remove row after server response #725

Open scne opened 9 years ago

scne commented 9 years ago

Hi! I've some troubles with ng-table. I would like to remove a row and delete if from table. This is a part of my code. In this way I fill my table

$scope.tableParams = new ngTableParams({
        page: 1,
        count: 10
    }, {
        getData: function ($defer, params) {
            var queryParams = params.sorting();
            var page = params.page();
            var size = params.count();

            function initFilters(filters, queryParams){
                $scope.search = {};

                if("companyProfile" in queryParams && !("companyProfile" in filters)){
                    filters.companyProfile = ''
                    filters.companyProfile=$location.$$search["companyProfile"];
                    $scope.search.companyProfile = {};
                    $scope.search.companyProfile = filters["companyProfile"]; 

                }

                return filters;
            }

            var filters = angular.equals({}, params.filter()) ? initFilters(params.filter(), $location.$$search) : params.filter();

            partners.filter({filter: filters.companyProfile, page: page, per_page: size, start: filters.start, end: filters.end, sorting:params.sorting()}).$promise.then(
                function(response){
                    var result = response.hits.map(function(single){
                        single._source.raw_companyProfile = single._source.companyProfile;
                        single._source.raw_email = single._source.email;
                        return single._source;
                    });
                    var orderedData = params.sorting() ? $filter('orderBy')(result, params.orderBy()) : result;
                    $scope.$parent.total = response.total;
                    params.total(response.total);
                    $defer.resolve(orderedData);
                }, function (error){
                    console.log(error);
                }
            );

        }
    });

And I've this for remove item from server and catch response

$scope.remove = function (pid) {
    SweetAlert.swal({
        ..............................
    }, function (isConfirm) {
        if (isConfirm) {
            partners.remove({action:pid}).$promise.then(function(response){
                var reload = function(){
                    console.log(4);
                    $scope.tableParams.reload();
                };

               var reject = function(){
                    console.log(1);
                    var sel = underscore.findWhere($scope.tableParams.data, {_id:pid});
                    console.log(sel);
                    console.log(2);
                    console.log($scope.tableParams.data);
                    $scope.tableParams.data = underscore.without($scope.tmp,sel);
                    console.log(3);
                    console.log($scope.tableParams.data);
                }

                var last = underscore.compose(reload,reject);

                last();

                SweetAlert.swal({
                  .................................
                });
            },function(err){
               ....................
            });

        } else {
            SweetAlert.swal({
               ..........
            });
        }
    });
};

But nothing happening when reload....

AmitMY commented 9 years ago

I cannot change the code for yours, because I lack context, but this works for me: Also, I dont know how to mark code, so just copy/paste it to view it correctly. My getData function:

getData: function ($defer, params) { var filteredData = params.filter() ? $filter('filter')(scope2[data], params.filter()) : scope2[data]; var orderedData = params.sorting() ? $filter('orderBy')(filteredData, params.orderBy()) : scope2[data];

            $defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
        }

My delete row function:

scope[table].removeRow = function (identifier, value) { scope2[data] = $.grep(scope2[data], function (v) { return v[identifier] != value; }); };

I call it like: removeRow('id',7); so it will remove all lines with id of 7.

at the end of the function you can have "this.reload()" or as i have done (because I have more functions like add etc..) do a watch:

scope2.$watch(data, function () { scope[table].reload(); }, true);

I am expanding the ngtable class to be called remotly, with a url, have an add, delete, and more settings, for my project I have to use it alot. If you found this useful, and think you have knoladge on the class, please check my issue: https://github.com/esvit/ng-table/issues/724

ZhangJianQ commented 8 years ago

I use this way to delete row from my list. `$scope.tableParams = new NgTableParams({ count:10,

    }, {
        // getData: function ($defer, params){
        //  $defer.resolve();
        // }
        total: 0,
        filterDelay:300,
        getData:function($defer, params){
            $http.get('representatives.json').success(function(d){
                $scope.representatives=d.results;
                $defer.resolve($scope.representatives);
            })
        }
    });
    $scope.removePre=function(){
        var reps=$scope.representatives;
        for(var i=reps.length-1; i>-1; i--){
            if(reps[i].selected==1){
                reps.splice(i, 1);
            }
        }
        $scope.tableParams.settings({
            dataset:$scope.representatives   //reset the data for ng-table
        })
    }`