horizontal scroll in datatable.net not aligning headers with actual column data on scrolling.
On scrolling table body gets moved but headers doesn't. I want to set headers scrollable.
My Code sample ::--------
$('#abc').dataTable({
"aaData": userContactGridData,
"bAutoWidth":false,
"aoColumnDefs": [
{ "bSortable": false, "aTargets": ["icon", "adminRoleIcon", "gearIcon"] },// disable sorting on first and last column
{ "sWidth": "20px", "aTargets": ["icon", "gearIcon"] },
{ "sWidth": "200px", "aTargets": ["userName"] },
{ "sClass": "icon", "aTargets": ["icon"] },
{ "sClass": "gearIcon", "aTargets": ["gearIcon"] },
{ "sClass": "userName", "aTargets": ["userName"] },
{ "sClass": "adminRoleIcon", "aTargets": ["adminRoleIcon"] },
{ "bVisible": false, "aTargets": ["adminRoleIcon"] },
],
"sDom": 'C<"H"Tfr>t<"F"ip>',
"iDisplayLength": 6,
"bProcessing": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aaSorting": [[2, "asc"]], // sort by name by default
"sScrollX": "100%",
"bScrollCollapse": true
});
Try adding sScrollX: 100% and bScrollCollapse: true
Then your code look like as below
var oTable;
$(document).ready(function () {
oTable = $("#yourElementName").dataTable({
"sScrollX": "100%",
"bScrollCollapse": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aoColumnDefs": [{ "aTargets": [0], "bSortable": true },
{ "aTargets": ['_all'], "bSortable": false}],
"aaSorting": [[0, 'asc']]
});
setTimeout(function () {
oTable.fnAdjustColumnSizing();
}, 10);
});
Related
Datatables allows you to use shift+select, while also allowing you to select single items without losing previously selected items by having the select style set to multi+shift.
'select': {style': 'multi+shift'}
However, for this datatable, that doesn't seem to work at all. What am I missing?
var $testTable = $("#test-list").DataTable({
"dom": '<"dataTables_top"i<"dataTables_custom_buttons">f>rt'+ '<"dataTables_bottom"ilp>',
"autoWidth": false,
"lengthChange": 100,
"lengthMenu": [[10, 25, 100, 200, 500, 1000], [10, 25, 100, 200, 500, 1000]],
"paging": true,
"pagingType": "full_numbers",
"stripeClasses": [ 'odd', 'even' ],
"order": [[1, 'asc']],
"language": {
"emptyTable": '<h3>Test...</h3>',
"search" : '',
"sLengthMenu" : "_MENU_ Per Page",
"info": "Showing _START_ to _END_ of _TOTAL_",
paginate: {
first: '<i class="fad fa-step-backward"></i>',
previous: '<i class="fad fa-backward"></i>',
next: '<i class="fad fa-forward"></i>',
last: '<i class="fad fa-step-forward"></i>'
}
},
'select': 'multi+shift',
"aoColumnDefs": [
{
"targets": [-1,-2,-3,-4,-5,-6,-7,-8,-9,-11],
'searchable': false
},
{
"targets": 'no-sort',
"orderable": false,
},
{
"targets": [8,11],
"visible": false
},
{
"iDataSort": 8,
"aTargets" : [7]
},
{
"iDataSort": 11,
"aTargets" : [10]
},
{
"targets": -1,
'searchable': false
}
],
"initComplete": function() {
......
},
"drawCallback": function(settings) {
......
}
});
I have a table is using datatable and Ajax to get data on load.
const oTable = $('#product').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "/ajax/admin/getproductlist/ajax.php",
"type": "POST"
},
"responsive": true,
"columnDefs": [
{
"targets": [-1],
"searchable": false,
"orderable": false
},
{
"targets": [1, 9, -2],
"width": "5%",
},
{
"targets": [1, -1],
"width": "10%",
},
{
"targets": [0, 2, 5, 6, 7, 8],
"width": "15%",
},
{
"targets": [3, 4],
"width": "30%",
"orderable": false, << This worked
"visible": false << DOESN't work
}
],
"columns" : [
{ "data": "sku" },
{ "data": "master" },
{ "data": "thumbs", "orderable": false }, << This worked
{ "data": "name", "orderable": false }, << This worked
{ "data": "name_fr", "visible" : false }, << DOESN't work
{ "data": "product_tags", "orderable": false }, << This worked
{ "data": "brand_tags", "orderable": false }, << This worked
{ "data": "label_tags", "orderable": false }, << This worked
{ "data": "mer_tags", "orderable": false }, << This worked
{ "data": "status", "orderable": false }, << This worked
{ "data": "quantity_in_stock" },
],
"initComplete": function(settings, json) {
$('#dataTables-list-Product_filter input').unbind();
$('#dataTables-list-Product_filter input').bind('keyup', function(e) {
if (e.keyCode == 13) {
oTable.search(this.value).draw();
}
});
$('#dataTables-list-Product_filter input').bind('change', function(e) {
oTable.search(this.value).draw();
});
},
});
I want to hide the columns when table finishes loading.
Let's take an example for name_fr. I tried to add visible: false to config option but somehow this column always shows up again after page stop loading. The orderable: false works fine.
I did add break point to debug the process. When table initialized, this column was hidden but after that something called it showing up again.
The column was only hidden when I click on checkbox that I make a function to show/hide.
$('input[id^="toggle_vis_"]').on('click', function(e) {
var column = oTable.column($(this).attr('data-column'));
$(this).val() == 1 ? column.visible(false) : column.visible(true);
});
So, I tried to but this to ready() function to hide column when page loaded.
$(document).ready(function() {
var column = oTable.column(4);
column.visible(false);
});
And...it didn't work as well. :(
Would you please give me an idea how can I solve this problem?
Thank you #andrewjames to help me find out my problem.
There was a conflict between orderable: false and responsive: true.
The option responsive: true brought back my columns when finishing loading.
Solution: Remove or edit responsive to false
var oTable = $('#Product').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "/ajax/admin/getproductlist/ajax.php",
"type": "POST"
},
...
...
"responsive": false,
...
...
});
Cheers.
I am trying to use "Fixed columns" feature in datatables with remote data pulled using ajax. Everything works perfectly without any errors. However, the "Fixed columns" feature doesn't work. Below is my code
$('#dataTable').DataTable({
"order": [[ groupColumn, "asc" ]],
"rowReorder": true,
"dom": 'l<"toolbar">frtip',
"scrollX": true,
"bscrollCollapse": true,
"displayLength": 25,
"fixedColumns": true,
"columnDefs": [
{ "visible": false, "targets": groupColumn },
{ "orderable": true, "targets": groupColumn },
{ "orderable": false, "targets": "_all" }
],
"processing": false,
"pageLength": 60,
"ajax": {
url: "http://example.com/output",
type: 'post',
data: {id:id}
},
"columns": {
//column definitions provided here
}
});
I am using the latest version Jquery DataTables.net, and have a table that display a total of 10 records per page, with a max count of 1004.
However, in the info bar, this reads:
Showing 1 to 5 of 1.004
the default of my table is as follows:
var oMessageDate = $("#messageDateDT").DataTable({
dom: "<'row'<'col-sm-12'<'pull-right'T><'pull-left'l>r<'clearfix'>>>t<'row'<'col-sm-12'<'pull-left'i><'pull-right'p><'clearfix'>>>",
stateSave: true,
pageLength: 10,
lengthMenu: [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]],
columns: [
{ data: "MessageReference", sWidth: "15%" },
{ data: "Beneficiary" },
{ data: "Currency", sWidth: "5%" },
{ data: "Amount" },
{ data: "MessageDate", sWidth: "15%" },
{ data: "MessageType", sWidth: "5%" },
{ data: "Direction", sWidth: "5%"},
{ data: "Assigned", sWidth: "10%" },
{ data: "Status", sWidth: "17%" },
{ data: "Message" },
{ data: "MessageId", sWidth: "5%" },
{ data: "StatusCode" }
],
"autoWidth": false,
"pagingType" :"full_numbers",
language: {
"decimal": "-",
"thousands": ".",
"infoEmpty": "No entries to show",
"lengthMenu": "Display _MENU_ records",
"processing": "Loading data",
searchPlaceholder: "on everything",
"zeroRecords": "No records to display",
"aria": {
"sortAscending": ": activate to sort column ascending",
"sortDescending": ": activate to sort column descending"
}
},
"columnDefs": [
{ "visible": false, "targets": 9 },
{ "visible": false, "targets": 10 },
{ "visible": false, "targets": 11 }
]
});
Can you please advise, how I can correct this.
Your screenshot shows
Showing 1 to 10 of 1.004 entries
And your default pageLength option is 10 which would give you 101 pages.
I have a datatable having 12 columns.
oTable = $('#NotificationsTable').dataTable({
"bDestroy": true,
"bFilter": true,
"bJQueryUI": true,
"bPaginate": true,
"bsorting": true,
"iDisplayStart": 0,
"aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
"iDisplayLength": 25,
"bLengthChange": true,
"bServerSide": true,
"sAjaxSource": "URL",
"fnServerParams": function (aoData) {
aoData.push({ "name": "param", "value": "page" }
);
},
"aoColumns": [
{ "mData": "Col0", "sClass": "center" },
{ "mData": "Col1", "sClass": "center" },
{ "mData": "Col2", "sClass": "center" },
{ "mData": "Col3", "sClass": "center wrapword" },
{ "mData": "Col4", "sClass": "center" },
{ "mData": "Col5", "sClass": "center" },
{ "mData": "Col6", "sClass": "center" },
{ "mData": "Col7", "sClass": "center" },
{ "mData": "Col8", "sClass": "center" },
{ "mData": "Col9", "sClass": "center" },
{ "mData": "Col10", "sClass": "center" },
{ "mData": "Col11", "bSortable": true }
],
"fnInitComplete": function (oSettings, json) {
//Some function
},
}
});
oTable .columnFilter({
sPlaceHolder: "head:after", aoColumns: [
{ type: "text" }, { type: "text" }, { type: "text" }, { type: "text" },
{ type: "text" }, null, { type: "text" }, { type: "text" },
{ type: "text" }, { type: "text" }, { type: "text" }, { type: "text" }
]
});
Here I want to disable the NEXT button of the datatable when "ALL" entries are shown.
FYI: I am having one row already in the datatable but I never wanted to show that to the user.
The Next button always have the id <table_id>_next, the length menu <table_id>_length. in dataTables 1.9.x there is individual CSS classes for each button's enabled or disabled mode. Toggle those classes when length value is -1, the value of All. Finally unbind the Next page event from the button.
$('select[name="NotificationsTable_length"]').on('change', function() {
var $nextButton = $("#NotificationsTable_next");
if ($(this).val() == -1) {
$nextButton.toggleClass('paginate_disabled_next');
$nextButton.unbind('click');
} else {
$nextButton.toggleClass('paginate_enabled_next');
}
}).trigger('change');
1.9.4 demo -> http://jsfiddle.net/stgLt59d/
In dataTables 1.10.x there is just a single .disabled class targeting all the pagination buttons.
$('select[name="NotificationsTable_length"]').on('change', function() {
var $nextButton = $("#NotificationsTable_next");
if ($(this).val() == -1) {
$nextButton.addClass('disabled');
$nextButton.unbind('click');
} else {
$nextButton.removeClass('disabled');
}
}).trigger('change');
demo -> http://jsfiddle.net/20q6adng/