【问题标题】:delete row after how to refresh datatable angularjs?如何刷新数据表angularjs后删除行?
【发布时间】:2017-08-14 13:50:13
【问题描述】:

我想在刷新数据表后删除行或使用 angularjs 重新加载数据表。 我已经完成了这段代码显示数据列表和删除行。

app.controller("userscontroller", ["$scope", "$http", "DTOptionsBuilder", "DTColumnBuilder", "userservice","$compile"


function ($scope, $http, DTOptionsBuilder, DTColumnBuilder, userservic,$compile) {       

$scope.dtColumns = [            
    DTColumnBuilder.newColumn("fullName", "Full Name").withOption('name', 'firstname'),
    DTColumnBuilder.newColumn("username", "Name").withOption('name', 'username'),
    DTColumnBuilder.newColumn("email", "Email").withOption('name', 'email'), 
    DTColumnBuilder.newColumn(null).withTitle('Action').withOption('defaultContent', ' ').notSortable()
        .renderWith(function (data, type, full, meta) {
            if (data.UserCount > 1)
                return '<button class="btn btn-primary" ng-click="delete(' + data.id + ');"><i class="fa fa-eye"></i>' + '</button>';                    
        })          
]

$scope.dtOptions = userservice.GetAllUser(DTOptionsBuilder)
.withOption('processing', true)
.withOption('serverSide', true)
.withPaginationType('full_numbers')
.withDisplayLength(50)
.withOption('aaSorting', [3, 'desc'])

 function createdRow(row, data, dataIndex) {
    $compile(angular.element(row).contents())($scope);
}
}]);

这里是我的删除功能:

$scope.delete= function (id) {
        if (confirm("Are you sure want to delete?") == true) {
            var getData = userservice.Deleteuser(id);
            getData.then(function (Msg) {
                if (Msg.statusText == "OK") {
                //here what code i written i don't know
                }
            }, function (err) {

            });
        }
    }

这是我的html代码=>

<table id="tbluserList" datatable="" dt-options="dtOptions" dt-columns="dtColumns" dt-instance="dtInstance" class="table table-hover"> </table>

【问题讨论】:

    标签: jquery angularjs datatable


    【解决方案1】:

    试试下面的代码,

    if (Msg.statusText == "OK") {
        var index = -1;     
        var users = $scope.users;// Let you have all users here
        for( var i = 0,len=users.length; i < len; i++ ) {
            if( users[i].id === id ) {
                index = i;
                break;
            }
        }
        if( index === -1 ) {
            alert( "Something gone wrong" );
        }
        $scope.users.splice( index, 1 );        
    }
    

    一条捷径,

    在您的函数中传递$index 并使用$index 对它进行切片,

    return '<button class="btn btn-primary" ng-click="delete('+data.id+',$index);">...';                    
    

    和删除功能

    $scope.delete = function (id,$index) {
       ....
       if (Msg.statusText == "OK") {
          $scope.users.splice($index, 1 );
       }
       ....
    }
    

    删除操作后尝试reloadData

    //Add below dtOptions
    $scope.dtInstance = {};
    ...
    
    $scope.delete = function (id) {
        ....
        if (Msg.statusText == "OK") {
            $scope.dtInstance.reloadData();
        }
        ...
    }
    

    【讨论】:

    • 意思是说我需要先获取 $scope.users 中的所有用户,然后再编写代码?
    • 我在这里发现了同样的场景hello-angularjs.appspot.com/removetablerow
    • 但我没有在 html 中使用这种类型的表,但我添加了我的 html 也有问题,所以你可以很好地理解我想说的。
    • 我也尝试过这一波,但我在控制台中遇到错误 reloadData() is not a function。
    猜你喜欢
    • 1970-01-01
    • 2017-06-28
    • 2012-08-03
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    相关资源
    最近更新 更多