2015-12-10 40 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

相關問題