No offense, but this is a typical mix-up of jQuery and Angular thinking, or a lack of understanding how Angular works. Here an attempt to wrap some jQuery logic into the angular digest loop. Read a great answer to the question “Thinking in AngularJS” if I have a jQuery background?
You cannot ever combine $(document).ready(function() {
and Angular. In fact, you can be very sure that your ready()
is executed long before Angular has finished its ng-repeat
business. And thats why you always get 1 for the row count (the header) and why the rows are dissappearing when you click on the headers. At the time you get row count there is not inserted any rows, and at the time you instantiate the dataTable()
, no rows have been inserted.
You can use $timeout
to force delay of the code, or force it into the next digest loop :
$scope.initDataTable = function() {
$timeout(function() {
$("#noCampaignData").hide();
//$("#example_paginate").hide();
var rowCount = $("#example tr").length;
console.log("Row count value is"+rowCount);
if (rowCount >= 0) {
console.log("Entered into Sorting");
$("#example").dataTable({
"pagingType" : "full_numbers",
"order" : [ [ 2, "desc" ] ]
});
}
}, 200)
}
or create a directive that instantiates the dataTable once the data is populated by ng-repeat
, as demonstrated in multiple answers for ng-repeat finish event :
.directive('repeatDone', function() {
return function(scope, element, attrs) {
if (scope.$last) { // all are rendered
scope.$eval(attrs.repeatDone);
}
}
})
...
<tr ng-repeat="campaign in campaignListData" repeat-done="initDataTable">
...
$scope.initDataTable = function() {
$("#noCampaignData").hide();
$("#example").dataTable({
...
});
}
Hope this will help you out.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…