Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I used a directive to buildup database after ng-repeat finished:

app.directive('repeatDone', function() {
    return function(scope, element, attrs) {
        if (scope.$last) {
            scope.$eval(attrs.repeatDone);
        }
    }
});

function InformationController($scope, $http) {
    $http.get("people").success(function(response) {
        $scope.people = response;
    });
    $scope.initDataTable = function() {
        $('#employeeTable').DataTable({
          
        });
    };
};
<table class="table table-striped table-bordered table-hover" id="employeeTable">
  <thead>
    <tr>
      <th>id</th>
      <th>Name</th>
      <th>Gender</th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="result in people" repeat-done="initDataTable()">
      <td>{{ result.id }}</td>
      <td>{{ result.name }}</td>
      <td>{{ result.gender }}</td>
    </tr>
  </tbody>
</table>

The data is displayed normally, but cannot use default sorting or searching.

Is there anything wrong? What should I do to enable the sorting and searching?

share|improve this question
    
Do you mean sorting by a specific column on load or when the user clicks on the column header? – jonmrich Aug 26 '15 at 14:05
    
@jonmrich yes, click the column header to sort. – Isaac Pei Aug 27 '15 at 2:53
up vote 0 down vote accepted

You cannot directly use datatables features in angular, since this is highly associated with execution sequences that gets overlapped and the functionality getting disturbed. You can induce a timeout specifically like below

setTimeout(function () {
   angular.element("#datatable").datatable();
  }, 10);

or do a tweak

add a blank element like this one below

$scope.temp = { "Name": "", "ID": "", "SSN": "" };

$scope.skills.unshift($scope.temp);

setTimeout(function () {
    $scope.skills.shift();
    $scope.apply;
}, 10);

What the above code does basically is adding a temp record(i.e blank) initially to the existing record collection and pull that back again, by doing this way datatable search,sort functionality is retrieved in angular.

Again this will work with a directive being created for datatable like below

app.directive('datatable', function () {
        return {
            restrict: 'AC',
            link: function (scope, element, attrs) {
                //
                scope.$watch('skills', function (newValue, oldValue) {
                    if (newValue != undefined) {
                        if (newValue.length > 0) {
                            var rows = element.find("tbody").find('tr');
                            var fixedColumn = 3;
                            if ($.fn.dataTable.isDataTable(element)) {
                                var tbl = $(element).dataTable();
                                tbl.fnClearTable();
                                for (var i = 0; i < rows.length; i++) {
                                    tbl.fnAddData($(rows[i]));
                                }
                            }
                            else {
                                element.DataTable({ paging: true, sorting: true, "order": [[0, "asc"]], columnDefs: [{ orderable: false, "targets": fixedColumn }] });
                            }
                            element.find('tbody').on('click', 'tr', function () {
                                $(this).addClass('selected');
                                $(this).siblings('tr').removeClass('selected');
                            });
                            element.fadeIn();
                        }
                    }
                }, true);
            }
        }
    });
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.