0
嗨角js專家,
我很困惑使用$資源,指令和分頁。我想要的是創建一個表格指令,可以排序,過濾和分頁。
有一些已經創建的表組件,但我在我的angularjs學習過程的開始,所以我想了解如何創建這樣的事情。
另外我讀了很多論壇和stackoverflow線程,我有一個部分解決方案,但仍然沒有工作解決方案。
所以我創建了一個指令:
app.directive("customTable", function($compile, $document) {
function link(scope, element, attrs) {
var html = "<table><tr>";
html += "<td style='width: {{ h.width }} px;white-space: nowrap;' ng-repeat='h in headers'>";
html += "<input style='width: {{ h.width }};display: block;' type='text' ng-model='filters[h.column]'> </td> </tr>";
html += "<tr ng-repeat='record in customTableData";
var htmlTableTd = "";
var arrayLength = scope.headers.length;
for (var i = 0; i < arrayLength; i++) {
html += " | filter:{" + scope.headers[i].column + ":filters." + scope.headers[i].column + "}";
htmlTableTd += "<td>{{ record." + scope.headers[i].column + " }}</td>";
}
html += "'>";
html += htmlTableTd;
html += "</tr> </table>";
element.html(html);
$compile(element.contents())(scope);
}
return {
restrict: "E",
link: link
};
});
和控制器:
app.controller("administration.service.controller", function ($scope, $resource) {
$scope.headers = [
{column: "name", width: "150px"},
{column: "serviceType", width: "100px"}
];
$scope.filters = {};
var serviceManager = $resource("list");
$scope.customTableData = serviceManager.query();
});
我試圖用引導分頁,但我應該在指令(在ngRepeat)使用一些過濾customTableData。然而,由於customTableData是承諾,並且構建指令時尚未處理,因此它是空的,即過濾後的customTableData也將爲空。一種解決方案是在控制器中使用then(...)方法,並從控制器調用指令的功能,但我不知道該怎麼做,它似乎不是一個優雅的解決方案。
有沒有針對我的問題的優雅解決方案?
謝謝,
尤