forked from pebsconsulting/pusher-realtime-jquery-datatable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
71 lines (66 loc) · 1.78 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const app = {
buildForm() {
return [
$('#name').val(),
$('#position').val(),
$('#office').val(),
$('#extn').val(),
$('#startDate')
.val()
.replace(new RegExp('-', 'g'), '/'),
`$${$('#salary').val()}`
];
},
sendToServer() {
const formData = this.buildForm();
axios.post('http://localhost:2000/record', formData)
.then(response => console.log(response));
},
addRow(dataTable, data) {
const addedRow = dataTable.row.add(data).draw();
addedRow.show().draw(false);
const addedRowNode = addedRow.node();
console.log(addedRowNode);
$(addedRowNode).addClass('highlight');
},
selectRow(dataTable) {
if ($(this).hasClass('selected')) {
$(this).removeClass('selected');
} else {
dataTable.$('tr.selected').removeClass('selected');
$(this).addClass('selected');
}
},
removeRow(dataTable) {
dataTable.row('.selected').remove().draw( false );
},
start() {
const dataTable = $('#realtime').DataTable({
data: dataSet,
columns: [
{ title: 'Name' },
{ title: 'Position' },
{ title: 'Office' },
{ title: 'Extn.' },
{ title: 'Start date' },
{ title: 'Salary' }
]
});
$('#add').on('click', this.sendToServer.bind(this));
const self = this;
$('#realtime tbody').on('click', 'tr', function(){
self.selectRow.bind(this, dataTable)();
});
$('#remove').on('click', this.removeRow.bind(this, dataTable));
// Pusher
var pusher = new Pusher('App Key', {
cluster: 'CLUSTER',
encrypted: true
});
var channel = pusher.subscribe('records');
channel.bind('new-record', (data) => {
this.addRow(dataTable, data);
});
}
};
$(document).ready(() => app.start());