2015-12-10 41 views
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":[]} 

那么如何来处理呢?

回答

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 
     } 
    } 
})  

通过,你将避免难看的DataTable警告警告框告诉用户有一个ajax错误。 zeroRecords消息仍将显示。

+0

我明白这个问题。当我返回空数据响应时,我正在设置404头。如果我从api更改为200状态标题,似乎设置默认消息“表中没有可用数据”。但它是有用的答案,有办法处理404响应头。 – Gowri

+0

有没有使用json源代码中“找不到数据”的方法? – Gowri

+0

@Gowri,请参阅关于404的更新。 – davidkonrad

相关问题