DataTable shows 1 insted of empty record message - javascript

This is my code for the date table
var table = $('.MyNumbersTable').DataTable({
"lengthMenu": [
[10, 25, 50, 100, -1],
[10, 25, 50, 100, "All"]
],
"aaSorting": [],
"language": {
"zeroRecords": "You don't have any numbers."
}
});
The issue is when there are records in my numbers table it displays records, It is fine.
But when if there is no record in table it show '1' as record instead of display message.

Please see notes regarding language.zeroRecords:
https://datatables.net/reference/option/language.zeroRecords
Note that language.emptyTable is shown when there is simply no
information in the table at all (regardless of filtering), while this
parameter is used for when the table is empty due to filtering.
It seems you may wish to use language.emptyTable : https://datatables.net/reference/option/language.emptyTable

Related

Datatables checkboxes start with some checkboxes checked

Can someone help me setup the Checkboxes extension for Datatables to start with some some checkboxes checked? The documentation on this matter can be found here, and although I've done everything that's required, it still fails to do what's supposed to do.
Basically I'm passing an array of the IDs that I want to be checked upon pageload, and I'm checking the ID of each row to check if it's contained in the array, but still all rows start unchecked.
jQuery(function($) {
$('#questions-pool').DataTable({
ordering: false,
dom: '<"#upper-controls"lf>t<"#lower-controls"ip>',
lengthMenu: [
[10, 50, 100, -1],
[10, 50, 100, 'All']
],
initComplete: function(settings) {
var api = this.api();
var selected = [387, 386, 385, 384, 383, 382, 381, 380, 379, 378];
alert(selected);
api.cells(
api.rows(function(idx, data, node) {
alert(data[2]);
return (selected.indexOf(data[2]) >= 0) ? true : false;
}).indexes(),
0
).checkboxes.select();
},
columnDefs: [{
targets: 2,
checkboxes: {
selectAllPages: false,
}
}],
});
});
You can find a working fiddle here. Everything is working fine, except for the desired rows selection at startup.
Any help will be greatly appreciated. I'm struggling with this for 2 days already...
So there were two issues (and bare with me since I don't even know jQuery nor any of the packages you were using).
The first is that the selected-list contain numbers while the data from the DOM your comparing to contain strings. Which means that they will never match.
The second thing was this piece:
columnDefs: [{
targets: 2,
checkboxes: {
selectAllPages: false,
}
}],
I changed it to
'columnDefs': [
{
'targets': 0,
'checkboxes': {
'selectRow': true
}
}
],
because that's what the working example you passed used, and... I have no idea why but now it works, here's a working jsfiddle :)
https://jsfiddle.net/2y5hx1a4/12/
EDIT: Oh and a tip, it's way easier (imo) to debug using console.log("what you want to check") than alert :P

DataTables when choosing "All" in "Show Entries", No matching records found

Hello i am setting up my first DataTable and i like it very much. Everything works perfect except the All choice in te Show Entries dropdown. It show's nothing when i choose that (No matching records found) The other numbers do work perfectly.
I use this code:
$(document).ready(function(){
$('#empTable').DataTable({
aLengthMenu: [
[25, 50, 100, 200, -1],
[25, 50, 100, 200, "All"]
],
'processing': true,
'serverSide': true,
'serverMethod': 'post',
'ajax': {
'url':'ajaxfile.php'
},
'columns': [
{ data: 'last_name' },
{ data: 'email' },
{ data: 'gender' },
{ data: 'first_name' },
{ data: 'city' },
],
dom: 'lBfrtip',
buttons: [
'csv', 'excel', 'pdf','print'
],
});
});
Under ajaxfile.php you have used a limit() for limiting the record and that get the values like 25, 50, 100, 200, but for All it value is -1 means the query is like:
.... limit(-1);
That's why it is showing No matching records found.
Make some changes in your logic so that when you get All i.e. -1 as limit parameter, don't use the limit() in that case and fetch all records otherwise use it.
Try this, it will resolve the issue.

How to add unique attribute in TR?

I have a form. After submission the data is sent to database.
I want to show all submissions details in dashboard with dataTables.js, What how it looks like.
table = $('#entries').DataTable({
lengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, "All"]],
data: data_table,
aoColumnDefs: [{ aTargets: -1, bSortable: false, bSearchable: false}],
"aaSorting": [[0,'desc']],
columns: Submissions.data_columns,
createdRow:function(row,data,dataIndex){
console.log(subId);
for(i=0; i<subId.length; i++){
$(row).attr("subId",subId[i]);
}
}
});
subId is an array where i have submissions' Ids of the form. Is there a way that I could use to add subId's to each row?

datatables + lengthMenu + All + serverside processing + not working

This is a basic datatables fiddle.
This is the default and the dropdowns will show the follwoing Show options 10, 25, 50, 75 and 100 records:
Now what I would like to get working is the "lengthMenu": [ [10, 25, 50, -1], [10, 25, 50, "All"] ] which I can in this fiddle. But what if I use server side processing, this is where the All option does not work for me. The others do. When I select All it dispalys the data that it was showing and the at the bottom says No data found in the server
As far as i Know the only difference is the data comes from the server. Can anyone advise how i can trouble shoot this? As far as I can tell when I select All I am sending length:-1 and for 10 it is length:10 so not sure why All is not working
$(document).ready(function() {
var dataTable = $('#employee-grid').DataTable( {
"lengthMenu" : [[ 10, 25, 50, -1 ],[ '10', '25', '50', 'All' ]],
//"pageLength": 25,
"processing": true,
"serverSide": true,
"ajax":{
url :"employee-grid-data.php", // json datasource
type: "post", // method , by default get
error: function(){ // error handling
$(".employee-grid-error").html("");
$("#employee-grid").append('<tbody class="employee-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
$("#employee-grid_processing").css("display","none");
}
}
} );
} );
When using server-side processing, you should ignore the start and length request parameters ONLY IF the length parameters is -1 in your PHP script in order to return all records.
"lengthMenu": [ [10, 25, 50, -1], [10, 25, 50, "All"] ]
1)Means that "ALL" is assigned the value of -1.
2)that's the reason why when u select All your are sending length:-1 and for 10 it is length:10
3)that's the reason why you also get No data found in the server - error
4)to proper this give this in your Controller
var length = Request.Form.GetValues("length").FirstOrDefault() == "-1" ? Convert.ToString(YOUR_LIST.Count()) : Request.Form.GetValues("length").FirstOrDefault();
5)this will tell if length == -1 then take your list total count so that it displays all rows .
For me, changing the 1st 3 of these parameters worked :
dom: 'ZBlrtip',
lengthMenu: [[100, 200, -1], [100, 200, "All"]],
bLengthChange: true,
order: [8, "desc"],
paging: true,
pageLength: 100,
serverSide: true

How to change results per page value in datatables

Datatables has an option to select the number of records shown per page. The default value starts from 10, 25, 50 and 100. How can I change it to start from 5 instead of 10?
10 records is a bit too much and takes a lot of space in my current design.
Thanx!
http://datatables.net/
The fully correct answer would be to use both and display length to 5:
$(document).ready( function(){
$('#table').dataTable({
"iDisplayLength": 5,
"aLengthMenu": [[5, 10, 25, 50, -1], [5, 10, 25, 50, "All"]]
});
});
If you use JUST "iDisplayLength", then the dropdown will not have that length in options later or when the page loads (instead you will see the first option, IE 10 by default). If you JUST use "aLengthMenu", then your results will still default to 10 instead of the first menu option.
You will want to use the iDisplayLength parameter when you initialize the DataTable object. Here's the example they list in their documentation:
$(document).ready( function() {
$('#example').dataTable( {
"iDisplayLength": 50
} );
} )
More information can be found here: http://www.datatables.net/usage/options
$.extend(true, $.fn.dataTable.defaults, {
"lengthMenu": [[5, 10, 15, 20, 25], [5, 10, 15, 20, 25]],
"pageLength": 5
});
The answer solved my problem of needing the following scenario
$(document).ready( function(){
$('#table').dataTable({
"aLengthMenu": [[10, 25, 50, 100], ["10 Per Page", "25 Per Page", "50 Per Page", "100 Per Page"]]
});
});
I realize that this question is old, but the accepted answer does not answer the OP's question.
The answer is to override the aLengthMenu option when initializing the dataTable. See here: http://datatables.net/examples/advanced_init/length_menu.html
You can simply add:
"lengthMenu": [
[10, 25, 50, -1],
[10, 25, 50, "All"]
] // remember to add "," if you initialize more option manually
or if you only want to add this option
$('#tablename').dataTable( {
"lengthMenu": [ [10, 25, 50, -1], [10, 25, 50, "All"] ]
} );
which will give you a drop-down to select the number of records per page in pagination.
It hardly for the the data tables 1.9
"iDisplayLength": 50

Categories