AngularJS and DataTables integration
AngularJS and DataTables integration
alzhin
Posts: 2Questions: 1Answers: 0
I am adding a new row to DataTable and it's not being picked up by AngularJS. Can anybody show how to tie these two? Basically, I need that new row behave like the original row, i.e. execute callGetRow function. Any help is much appreciated.
<html ng-app>
<head>
<title>Example</title>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="http://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script>
function RESTCall($scope, $http) {
$scope.callGetRow = function(line) {
alert(line);
};
$scope.callAddNew = function() {
$('#example').DataTable().row.add(["2.0","Item 2", "Generic Desc", "2", 200]).draw();
};
}
</script>
</head>
<body>
<div ng-controller="RESTCall">
<table class="compact" id="example" cellspacing="0" >
<thead>
<tr>
<th></th>
<th>Item</th>
<th>Description</th>
<th>Qty</th>
<th>List $</th>
</tr>
</thead>
<tbody>
<tr ng-click="callGetRow('1/0')" id="1.0">
<td>1.0</td>
<td>Item 1</td>
<td>Generic Description</td>
<td>3</td>
<td>100</td>
</tr>
</tbody>
</table>
<button class="btn" type="button" ng-click="callAddNew()">Add new</button>
</div>
</body>
</html>
This discussion has been closed.
Answers
The solution is in the example below. Basically, I had to add drop adding rows using DataTable and use AngularJS to create content, compile it and use JQuery to add it to the selector in my HTML.
Things that needed to be done:
- Remove DataTables code
- Put my functions inside controller and inject Angular services I needed (scope, compile, element)
- use AngularJS to create content and compile it
- Add class name to tbody for JQuery to append the new row to