1

angular-datatables在我的应用程序中使用。当ajax源返回空数组时,我不知道如何处理。我期待显示一些消息以指示“未找到记录”。

$scope.dtOptions = DTOptionsBuilder
.newOptions()
    .withOption('ajax', {         
     dataSrc: 'data',
     url: 'api_url',
     type: 'GET'
 })    
.withPaginationType('full_numbers');


$scope.dtColumns = [
    DTColumnBuilder.newColumn('id').withTitle('ID'),
    DTColumnBuilder.newColumn('firstName').withTitle('First name'),
    DTColumnBuilder.newColumn('lastName').withTitle('Last name').notVisible()
];

我的 Ajax 响应如下

{"status":false,"message":"No data were found","data":[]}

那么如何处理呢?

4

1 回答 1

1

提供language带有所需消息的结构。您要查找的密钥称为zeroRecords。小例子:

$scope.language = {
   "zeroRecords": "No records found",
}

$scope.dtOptions = DTOptionsBuilder.newOptions()
   .withOption('ajax', {         
       dataSrc: 'data',
       url: 'api_url',
       type: 'GET'
   })    
   .withPaginationType('full_numbers') 
   .withOption('language', $scope.language)

演示-> http://plnkr.co/edit/teJbmQA3wEnzvKEi63IL?p=preview


要捕获 404,只需设置一个错误处理程序:

$scope.dtOptions = DTOptionsBuilder.newOptions()
  .withOption('ajax', {         
    dataSrc: 'data',
    url: 'api_url',
    type: 'GET',
    error : function(xhr, ajaxOptions, thrownError){
      if (xhr.status==404) {
        //no specific action needed
      }
   }  
})    

这样,您将避免丑陋的 dataTables 警告警告框告诉用户出现 ajax 错误。该zeroRecords消息仍将显示。

于 2015-12-14T12:37:47.027 回答