I'm trying to populate data from a REST webservice in a table using ngTable. Data are shown, but they are neither sortable nor filterable. Filtering always returns empty list.
Here is a link to the API I am referencing: http://bazalt-cms.com/ng-table/
JS:
$scope.dataInstances = [];
$scope.getDataInstances = function() {
$http({
method : 'GET',
url : '/rest/v1/data/instances',
headers : {
"Authorization" : "Basic " + btoa("USERNAME" + ":" + "PASSWORD")
},
})
.success(function(data, status) {
$scope.dataInstances = data;
$scope.tableParams.reload();
// just some logging
console.log(JSON.stringify(data));
})
.error(function(data, status) {
alert("Error: " + status);
});
};
$scope.tableParams = new ngTableParams({
page: 1, // show first page
count: 10, // count per page
filter: {
},
sorting: {
date: 'asc' // initial sorting
}
},
{
total: $scope.dataInstances.length, // length of data
getData: function($defer, params) {
// use build-in angular filter
var filteredData = params.filter() ?
$filter('filter')($scope.dataInstances, params.filter()) :
$scope.dataInstances;
var orderedData = params.sorting() ?
$filter('orderBy')(filteredData, params.orderBy()) :
$scope.dataInstances;
params.total(orderedData.length); // set total for recalc pagination
$defer.resolve(orderedData.slice((params.page() - 1) * params.count(), params.page() * params.count()));
}
});
HTML:
<div ng-controller="myController" ng-init="getDataInstances()">
<table ng-table="tableParams" show-filter="true" class="table table-condensed">
<tr ng-repeat="dataInstance in dataInstances">
<td data-title="'Name'" sortable="'name'" filter="{ 'name' : 'text'}">{{dataInstance.name}}</td>
<td data-title="'Date'" sortable="'date'" filter="{ 'date' : 'text'}">{{dataInstance.date | date:'dd.MM.yyyy HH:mm' }}</td>
<td data-title="'Type'" sortable="'type'" filter="{ 'type' : 'text'}">{{dataInstance.type}}</td>
</tr>
</table>
</div>
You have any hints how to get this working? Tried different other possible solutions - and none worked - like:
- asynchronously populating an AngularJS ngTable with json data
- Loading JSON via AJAX with NgTable parameters
- Sorting ngTable doesn't work when heading gets clicked
Thanks in advance!
type
andname
columns? – Pedagogy