0

Below is my table's html code -

<table id="srchTable" style="width:100%; font-size:0.85em;"">
 <thead>
   <tr>
     <th>Name</th>
     <th>Age</th>
     <th>Salary</th>
   </tr>
 </thead>
 <tr ng-repeat="jsonSearchData in searchData">
   <td><a id="link1" href="" ng-click="openPopUp()">{{jsonSearchData.Name}}</a></td>
   <td>{{jsonSearchData.Age}}</td>
   <td>{{jsonSearchData.Salary}}</td>
 </tr>
</table>

This is js code for table -

$('#srchTable').dataTable();

I am populating table from json result. Below is the json -

[{  
      "Name":"Sam",
      "Age":"25",
      "Salary":"$25000"
 },
 {
      "Name":"Phillip",
      "Age":"25",
      "Salary":"$30000"
 }]

Now when I am clicking on sort icons table is showing No data available in table message. And same thing is happeing when I am trying to search in data table.
I tried below code but it didn't worked.

$('#srchTable').dataTable({
  "ordering":true,
  "searching": true
});

Please help.

mailmehere
  • 173
  • 1
  • 3
  • 14
  • I think, that code `$('#srchTable').dataTable()` run before the angular render table. Please try `setTimeout(function() { $('#srchTable').dataTable() }, 1000);` for test, and say me if it work – LinnTroll Dec 28 '15 at 08:10
  • @LinnTroll - Its not working. :( – mailmehere Dec 28 '15 at 09:00
  • so looking at this question, after making some comments on the other question you posted, I can see that you already tried to ask about the problem. What I am seeing here and in your other question is a common theme, you are trying to think of Angular.js in the same way you might think of jQuery, even though they accomplish things ***very differently***. **DON'T** use jQuery with angular unless you are designing new functionality. This is a good read: http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background/15012542?s=2|0.8371#15012542 – Claies Jan 12 '16 at 06:00

2 Answers2

1

In this case jquery is executed before angular render the data .
You don't need to use jquery here. Angular provides filters and sorting options .

example :

  <input type="search" ng-model="query">
    <table id="srchTable" style="width:100%; font-size:0.85em;"">
     <thead>
       <tr>
         <th>Name</th>
         <th>Age</th>
         <th>Salary</th>
       </tr>
     </thead>
     <tr ng-repeat="jsonSearchData in searchData | filter : query | orderBy : 'name'">
       <td><a id="link1" href="" ng-click="openPopUp()">{{jsonSearchData.Name}}   </a></td>
       <td>{{jsonSearchData.Age}}</td>
       <td>{{jsonSearchData.Salary}}</td>
     </tr>
    </table>
Vittal Das
  • 69
  • 8
0

Be sure to wrap your data rows in <tbody></tbody>.

        <table id="srchTable" style="width:100%; font-size:0.85em;"">
     <thead>
       <tr>
         <th>Name</th>
         <th>Age</th>
         <th>Salary</th>
       </tr>
     </thead>

<tbody><!-- start after </thead> Be sure not to include in loop -->

     <tr ng-repeat="jsonSearchData in searchData">
       <td><a id="link1" href="" ng-click="openPopUp()">{{jsonSearchData.Name}}</a></td>
       <td>{{jsonSearchData.Age}}</td>
       <td>{{jsonSearchData.Salary}}</td>
     </tr>

</tbody><!-- end after loop -->

</table>
Karin
  • 61
  • 1
  • 8