Hi,
I want to create list of person using datatable-editable, but it seems the datatables can't work if we use ng-repeat, is there any js need to be setup for datatable-editable for ng-repeat? i already add 2 js (datatables.bootstrap.js & datatables.min.js)
HTML
<table class="table table-striped table-hover table-bordered" id="sample_editable_1" >
<thead>
<tr>
<th> Username </th>
<th> Full Name </th>
<th> Email Address </th>
<th> Notes </th>
<th> Edit </th>
<th> Delete </th>
</tr>
</thead>
<tbody>
<tr ng-repeat="person in vm.persons">
<td>{{person.name}}</td>
<td>{{person.surname}}</td>
<td>{{person.emailAddress}}</td>
<td></td>
<td>
<a class="edit" href="javascript:;"> Edit </a>
</td>
<td>
<a class="delete" href="javascript:;"> Delete </a>
</td>
</tr>
<tr>
<td> lisa </td>
<td> Lisa ong </td>
<td> [email protected] </td>
<td class="center"> power user </td>
<td>
<a class="edit" href="javascript:;"> Edit </a>
</td>
<td>
<a class="delete" href="javascript:;"> Delete </a>
</td>
</tr>
<tr>
<td> lisa </td>
<td> Lisa Wong </td>
<td> [email protected] </td>
<td class="center"> new user </td>
<td>
<a class="edit" href="javascript:;"> Edit </a>
</td>
<td>
<a class="delete" href="javascript:;"> Delete </a>
</td>
</tr>
</tbody>
</table>
JS
function restoreRow(oTable, nRow) {
var aData = oTable.fnGetData(nRow);
var jqTds = $('>td', nRow);
for (var i = 0, iLen = jqTds.length; i < iLen; i++) {
oTable.fnUpdate(aData[i], nRow, i, false);
}
oTable.fnDraw();
}
function editRow(oTable, nRow) {
var aData = oTable.fnGetData(nRow);
var jqTds = $('>td', nRow);
jqTds[0].innerHTML = '<input type="text" class="form-control input-small" value="' + aData[0] + '">';
jqTds[1].innerHTML = '<input type="text" class="form-control input-small" value="' + aData[1] + '">';
jqTds[2].innerHTML = '<input type="text" class="form-control input-small" value="' + aData[2] + '">';
jqTds[3].innerHTML = '<input type="text" class="form-control input-small" value="' + aData[3] + '">';
jqTds[4].innerHTML = '<a class="edit" href="">Save</a>';
jqTds[5].innerHTML = '<a class="cancel" href="">Cancel</a>';
}
function saveRow(oTable, nRow) {
var jqInputs = $('input', nRow);
oTable.fnUpdate(jqInputs[0].value, nRow, 0, false);
oTable.fnUpdate(jqInputs[1].value, nRow, 1, false);
oTable.fnUpdate(jqInputs[2].value, nRow, 2, false);
oTable.fnUpdate(jqInputs[3].value, nRow, 3, false);
oTable.fnUpdate('<a class="edit" href="">Edit</a>', nRow, 4, false);
oTable.fnUpdate('<a class="delete" href="">Delete</a>', nRow, 5, false);
oTable.fnDraw();
}
function cancelEditRow(oTable, nRow) {
var jqInputs = $('input', nRow);
oTable.fnUpdate(jqInputs[0].value, nRow, 0, false);
oTable.fnUpdate(jqInputs[1].value, nRow, 1, false);
oTable.fnUpdate(jqInputs[2].value, nRow, 2, false);
oTable.fnUpdate(jqInputs[3].value, nRow, 3, false);
oTable.fnUpdate('<a class="edit" href="">Edit</a>', nRow, 4, false);
oTable.fnDraw();
}
var table = $('#sample_editable_1');
var oTable = table.dataTable({
"lengthMenu": [
[5, 15, 20, -1],
[5, 15, 20, "All"] // change per page values here
],
"pageLength": 5,
"language": {
"lengthMenu": " _MENU_ records"
},
"columnDefs": [{ // set default column settings
'orderable': true,
'targets': [0]
}, {
"searchable": true,
"targets": [0]
}],
"order": [
[0, "asc"]
] // set first column as a default sort by asc
});
var tableWrapper = $("#sample_editable_1_wrapper");
var nEditing = null;
var nNew = false;
$('#sample_editable_1_new').click(function (e) {
e.preventDefault();
if (nNew && nEditing) {
if (confirm("Previose row not saved. Do you want to save it ?")) {
saveRow(oTable, nEditing); // save
$(nEditing).find("td:first").html("Untitled");
nEditing = null;
nNew = false;
} else {
oTable.fnDeleteRow(nEditing); // cancel
nEditing = null;
nNew = false;
return;
}
}
var aiNew = oTable.fnAddData(['', '', '', '', '', '']);
var nRow = oTable.fnGetNodes(aiNew[0]);
editRow(oTable, nRow);
nEditing = nRow;
nNew = true;
});
table.on('click', '.delete', function (e) {
e.preventDefault();
if (confirm("Are you sure to delete this row ?") == false) {
return;
}
var nRow = $(this).parents('tr')[0];
oTable.fnDeleteRow(nRow);
alert("Deleted! Do not forget to do some ajax to sync with backend :)");
});
table.on('click', '.cancel', function (e) {
e.preventDefault();
if (nNew) {
oTable.fnDeleteRow(nEditing);
nEditing = null;
nNew = false;
} else {
restoreRow(oTable, nEditing);
nEditing = null;
}
});
table.on('click', '.edit', function (e) {
e.preventDefault();
/* Get the row as a parent of the link that was clicked on */
var nRow = $(this).parents('tr')[0];
if (nEditing !== null && nEditing != nRow) {
/* Currently editing - but not this row - restore the old before continuing to edit mode */
restoreRow(oTable, nEditing);
editRow(oTable, nRow);
nEditing = nRow;
} else if (nEditing == nRow && this.innerHTML == "Save") {
/* Editing this row and want to save it */
saveRow(oTable, nEditing);
nEditing = null;
alert("Updated! Do not forget to do some ajax to sync with backend :)");
} else {
/* No edit in progress - let's start one */
editRow(oTable, nRow);
nEditing = nRow;
}
});
result: (only 2 static record will be shown in datatables)
lisa Lisa ong <a href="mailto:[email protected]">[email protected]</a> power user Edit Delete
lisa Lisa Wong <a href="mailto:[email protected]">[email protected]</a> new user Edit Delete
1 Answer(s)
-
0
Hi,
Datatables is a jQuery based library. As you probably know, Angularjs does not like jQuery libraries much. Because, Angular has it's own binding mechanism and updates DOM independently. So, for ng-repeat, angular dynamically builds table, but datatables read table only once. That's why it can not know that angular updated it. You have a few options:
- datatables library may have some 'update' method that you can use after angular binding. See it's docs since I did not use datatables before.
- use jQuery, instead of angular's ng-repeat
- search for an angular wrapper for datatables