Skip to content

Commit 3369f53

Browse files
committed
Datatables - update js to include action events for ajax rows
1 parent 044bf5a commit 3369f53

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

resources/assets/admin/js/titan/datatables.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,17 @@ function initDataTables(selector, options)
4646
options.sDom = "<'dt-toolbar'<'col-xs-12 col-sm-6'f><'col-sm-6 col-xs-12 hidden-xs'l>r>" +
4747
"t" +
4848
"<'dt-toolbar-footer'<'col-sm-6 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-6'p>>";
49+
options.drawCallback = function(settings) {
50+
$('[data-toggle="tooltip"]').tooltip();
51+
}
52+
53+
// datatable
54+
var table = $(selector).DataTable(options);
55+
56+
// reregister the tooltip events on table
57+
table.$('[data-toggle="tooltip"]').tooltip();
4958

50-
return $(selector).DataTable(options);
59+
return table;
5160
}
5261

5362
function getOrderBy(element)
@@ -68,12 +77,23 @@ function getOrderBy(element)
6877
return orderBy;
6978
}
7079

71-
function initActionDeleteClick()
80+
function initActionDeleteClick(element)
7281
{
7382
$('.dt-table').off('click', '.btn-delete-row');
83+
$('.dt-table').off('click', '.btn-confirm-modal-row');
84+
if(element) {
85+
element.off('click', '.btn-delete-row');
86+
element.off('click', '.btn-confirm-modal-row');
87+
}
7488

7589
// DELETE ROW LINK
7690
$('.dt-table').on('click', '.btn-delete-row', onActionDeleteClick);
91+
$('.dt-table').on('click', '.btn-confirm-modal-row', onConfirmRowlick);
92+
93+
if(element) {
94+
element.on('click', '.btn-delete-row', onActionDeleteClick);
95+
element.on('click', '.btn-confirm-modal-row', onConfirmRowlick);
96+
}
7797

7898
function onActionDeleteClick(e)
7999
{
@@ -94,4 +114,21 @@ function initActionDeleteClick()
94114

95115
return false;
96116
}
117+
118+
function onConfirmRowlick(e)
119+
{
120+
e.preventDefault();
121+
var formId = $(this).attr('data-form');
122+
var title = $(this).attr('data-original-title');
123+
title = '<strong>' + title + '</strong>';
124+
125+
var content = "Are you sure you want to " + title + "? ";
126+
$('#modal-confirm').find('.modal-body').find('p').html(content);
127+
$('#modal-confirm').find('.modal-footer').find('.btn-primary').on('click', function (e)
128+
{
129+
$('#' + formId).submit();
130+
});
131+
$('#modal-confirm').modal('show');
132+
return false;
133+
}
97134
}

resources/views/admin/partials/datatables.blade.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
if ($('#tbl-list').attr('data-server') == 'true') {
88
var options = {!! json_encode($options) !!}; // convert php array to js array
99
@if(isset($action) && $action == true || isset($action) == false)
10-
options.push({data: 'action', name: 'action', orderable: false, searchable: false}); // add actions column
10+
options.push({
11+
data: 'action',
12+
name: 'action',
13+
orderable: false,
14+
searchable: false,
15+
visible: true
16+
}); // add actions column
1117
@endif
12-
initDatatablesAjax('#tbl-list', "{{ Request::url().'/datatable' }}", options); // init datatables
18+
var table = initDatatablesAjax("{{ (isset($id)? $id: '#tbl-list') }}", "{{ (isset($url)? $url: request()->url().'/datatable') }}", options); // init datatables
1319
}
1420
})
1521
</script>

0 commit comments

Comments
 (0)