knockout databind jquery datatable to add a row
knockout databind jquery datatable to add a row
by Shrikrishna Gupta
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<table data-bind="dataTable: employees">
<thead>
<tr>
<th>Id</th>
<th>First</th>
<th>Last</th>
<th>Phone</th>
<th>Dept</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<p style="padding-top: 20px;">
Id:
<input data-bind="textInput: id" />
</p>
<p>
First:
<input data-bind="textInput: firstName" />
</p>
<p>
Last:
<input data-bind="textInput: lastName" />
</p>
<p>
phone:
<input data-bind="textInput: phone" />
</p>
<p>
dept:
<input data-bind="textInput: dept" />
</p>
<p>
<input type="button" value="add employee" data-bind="click: add" />
</p>
CSS
table.dataTable{width:100%;margin:0 auto;clear:both;border-collapse:separate;border-spacing:0}table.dataTable thead th,table.dataTable tfoot th{font-weight:bold}table.dataTable thead th,table.dataTable thead td{padding:10px 18px;border-bottom:1px solid #111}table.dataTable thead th:active,table.dataTable thead td:active{outline:none}table.dataTable tfoot th,table.dataTable tfoot td{padding:10px 18px 6px 18px;border-top:1px solid #111}table.dataTable thead .sorting,table.dataTable thead .sorting_asc,table.dataTable thead .sorting_desc{cursor:pointer;*cursor:hand}table.dataTable thead .sorting,table.dataTable thead .sorting_asc,table.dataTable thead .sorting_desc,table.dataTable thead .sorting_asc_disabled,table.dataTable thead .sorting_desc_disabled{background-repeat:no-repeat;background-position:center right}table.dataTable thead .sorting{background-image:url("../images/sort_both.png")}table.dataTable thead .sorting_asc{background-image:url("../images/sort_asc.png")}table.dataTable thead .sorting_desc{background-image:url("../images/sort_desc.png")}table.dataTable thead .sorting_asc_disabled{background-image:url("../images/sort_asc_disabled.png")}table.dataTable thead .sorting_desc_disabled{background-image:url("../images/sort_desc_disabled.png")}table.dataTable tbody tr{background-color:#ffffff}table.dataTable tbody tr.selected{background-color:#B0BED9}table.dataTable tbody th,table.dataTable tbody td{padding:8px 10px}table.dataTable.row-border tbody th,table.dataTable.row-border tbody td,table.dataTable.display tbody th,table.dataTable.display tbody td{border-top:1px solid #ddd}table.dataTable.row-border tbody tr:first-child th,table.dataTable.row-border tbody tr:first-child td,table.dataTable.display tbody tr:first-child th,table.dataTable.display tbody tr:first-child td{border-top:none}table.dataTable.cell-border tbody th,table.dataTable.cell-border tbody td{border-top:1px solid #ddd;border-right:1px solid #ddd}table.dataTable.cell-border tbody tr...
JavaScript
ko.bindingHandlers.dataTable = {
init: function(element, valueAccessor, allBindingsAccessor) {
var value = valueAccessor(),
rows = ko.toJS(value);
allBindings = ko.utils.unwrapObservable(allBindingsAccessor()),
options = allBindings.dataTableOptions || {},
$element = $(element);
$element.dataTable(options);
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$element.dataTable().fnDestroy();
});
},
update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
var value = valueAccessor(),
rows = ko.toJS(value);
console.log(rows);
$(element).find("tbody tr").remove();
var table = $(element).DataTable();
table.clear().draw();
$.each(rows, function(index, row) {
var myArray = [];
$.each(row, function(key, value) {
myArray.push(value);
});
table.row.add(myArray).draw().node();
});
}
}
function employee(id, firstName, lastName, phone, dept) {
var self = this;
this.id = ko.observable(id);
this.firstName = ko.observable(firstName);
this.lastName = ko.observable(lastName);
this.phone = ko.observable(phone);
this.dept = ko.observable(dept);
}
function model() {
var self = this;
this.employees = ko.observableArray('')
this.id = ko.observable('');
this.firstName = ko.observable('');
this.lastName = ko.observable('');
this.phone = ko.observable('');
this.dept = ko.observable('');
this.add = function() {
self.employees.push(new employee(
this.id(), this.firstName(), this.lastName(), this.phone(), this.dept()
));
// console.log(ko.toJSON(self.employees))
}
}
var mymodel = new model();
$(document).ready(function() {
ko.applyBindings(mymodel);
mymodel.employees.push(new employee('1', 'Joe', 'Smith', '333-657-4366', 'IT'))
});