$('.button').click(function (event) {
event.preventDefault();
if ($('#GroupElement_Id option:selected').val() != '') {
var viewname = $('#GroupElement_Id option:selected').val();
// if data-table has been initialized then destroy it
if ($('#gem-table').hasClass('dataTable')) {
var table = $('#gem-table').DataTable();
// remove all rows. important to call before destroy.
table.clear();
// remove those enhancements and return the table to its original un-enhanced state
// call if create new tables based on new criteria with different initialisation settings
table.destroy();
// empty in case the columns change
$('#gem-table thead tr').empty();
$('#gem-table tbody').empty();
$('#gem-table tfoot tr').empty();
}
// Ajax call that gets the data
$.ajax({
type: 'GET',
contentType: 'application/json; charset=utf-8',
url: apiBaseUmbraco + 'gem/gemdataserverside/',
data:
{
'viewname': viewname,
'all': $('#all-gem-rows').is(':checked') ? 1 : 0
},
dataType: 'JSON',
beforeSend: function () {
$('.ajax-loader').show();
},
success: function (response) {
var tableColumns = jQuery.parseJSON(response).columns;
var columns = [];
var headers = '';
var footers = '';
// build up table header, footer
// and columns JSON array for the data-table
$.each(tableColumns, function (index, value) {
headers += '<th>' + value + '</th>';
footers += '<th>' + value + '</th>';
var headerObj = new Object();
headerObj['sName'] = value;
columns.push(headerObj);
});
// insert header and footer into the table
$('#gem-table thead tr').html(headers);
$('#gem-table tfoot tr').html(footers);
$('#gem-table').dataTable({
'bServerSide': true,
'sAjaxSource': apiBaseUmbraco + 'gem/gemdataserverside/?viewname=' + viewname + '&all=' + ($('#all-gem-rows').is(':checked') ? '1' : '0'),
'bProcessing': true,
'aoColumns': columns,
dom: 'T<"clear">lfrtip',
'tableTools': {
'sSwfPath': '/swf/copy_csv_xls_pdf.swf'
},
'responsive': true,
'lengthMenu': [[25, 50, 100, -1], [25, 50, 100, 'All']],
'drawCallback': function (settings) {
$('.ajax-loader').hide();
},
});
// scroll down to data-table
$('html, body').animate({
scrollTop: $('#gem-table_wrapper').offset().top
}, 1000);
},
error: function (result) {
alert('Service call failed: ' + result.status + ' Type :' + result.statusText);
}
});
} else {
}
});