Please help to solve: DataTables warning: table id=example - Requested unknown parameter '1' for row
Please help to solve: DataTables warning: table id=example - Requested unknown parameter '1' for row
edes
Posts: 13Questions: 2Answers: 0
my HTML:
<table class="display table table-striped table-bordered table-hover responsive nowrap" id="example" cellspacing="0" cellpadding="0">
<!-- <table id="std_details" class="display select" width="100%" cellspacing="0">-->
<thead>
<tr>
<th><input name="select_all" value="1" id="example-select-all" type="checkbox"></th>
<th>First name</th>
<th>Last name</th>
<th>Parent Mobile No</th>
<th>Class</th>
<th>Engl</th>
<th>Bios</th>
<th>Kisw</th>
<th>Geog</th>
<th>Hist</th>
<th>Phys</th>
<th>Maths</th>
<th>Civics</th>
<th>Chem</th>
<th>Average</th>
<th>Position</th>
</tr>
</thead>
<tfoot>
<tr>
<th></th>
<th>First name</th>
<th>Last name</th>
<th>Parent Mobile No</th>
<th>Class</th>
<th>Engl</th>
<th>Bios</th>
<th>Kisw</th>
<th>Geog</th>
<th>Hist</th>
<th>Phys</th>
<th>Maths</th>
<th>Civics</th>
<th>Chem</th>
<th>Average</th>
<th>Position</th>
</tr>
</tfoot>
</table>
**MY JS**:
//
// Updates "Select all" control in a data table
//
function updateDataTableSelectAllCtrl(table){
var $table = table.table().node();
var $chkbox_all = $('tbody input[type="checkbox"]', $table);
var $chkbox_checked = $('tbody input[type="checkbox"]:checked', $table);
var chkbox_select_all = $('thead input[name="select_all"]', $table).get(0);
// If none of the checkboxes are checked
if($chkbox_checked.length === 0){
chkbox_select_all.checked = false;
if('indeterminate' in chkbox_select_all){
chkbox_select_all.indeterminate = false;
}
// If all of the checkboxes are checked
} else if ($chkbox_checked.length === $chkbox_all.length){
chkbox_select_all.checked = true;
if('indeterminate' in chkbox_select_all){
chkbox_select_all.indeterminate = false;
}
// If some of the checkboxes are checked
} else {
chkbox_select_all.checked = true;
if('indeterminate' in chkbox_select_all){
chkbox_select_all.indeterminate = true;
}
}
}
$(document).ready(function (){
// Array holding selected row IDs
var rows_selected = [];
var table = $('#example').DataTable({
"bProcessing": true,
"bServerSide": true,
'ajax':{
url: 'php/server_side.php',
type: 'POST'
},
'columnDefs': [{
'targets': 0,
'searchable':false,
'orderable':false,
'width':'1%',
'data':'text',
'className': 'dt-body-center',
'render': function (data, type, full, meta){
return '<input type="checkbox">';
}
}],
'order': [1, 'asc'],
'rowCallback': function(row, data, dataIndex){
// Get row ID
var rowId = data[0];
// If row ID is in the list of selected row IDs
if($.inArray(rowId, rows_selected) !== -1){
$(row).find('input[type="checkbox"]').prop('checked', true);
$(row).addClass('selected');
}
}
});
// Handle click on checkbox
$('#example tbody').on('click', 'input[type="checkbox"]', function(e){
var $row = $(this).closest('tr');
// Get row data
var data = table.row($row).data();
// Get row ID
var rowId = data[0];
// Determine whether row ID is in the list of selected row IDs
var index = $.inArray(rowId, rows_selected);
// If checkbox is checked and row ID is not in list of selected row IDs
if(this.checked && index === -1){
rows_selected.push(rowId);
// Otherwise, if checkbox is not checked and row ID is in list of selected row IDs
} else if (!this.checked && index !== -1){
rows_selected.splice(index, 1);
}
if(this.checked){
$row.addClass('selected');
} else {
$row.removeClass('selected');
}
// Update state of "Select all" control
updateDataTableSelectAllCtrl(table);
// Prevent click event from propagating to parent
e.stopPropagation();
});
// Handle click on table cells with checkboxes
$('#example').on('click', 'tbody td, thead th:first-child', function(e){
$(this).parent().find('input[type="checkbox"]').trigger('click');
});
// Handle click on "Select all" control
$('thead input[name="select_all"]', table.table().container()).on('click', function(e){
if(this.checked){
$('#example tbody input[type="checkbox"]:not(:checked)').trigger('click');
} else {
$('#example tbody input[type="checkbox"]:checked').trigger('click');
}
// Prevent click event from propagating to parent
e.stopPropagation();
});
// Handle table draw event
table.on('draw', function(){
// Update state of "Select all" control
updateDataTableSelectAllCtrl(table);
});
// Handle form submission event
$('#frm-example').on('submit', function(e){
var form = this;
// Iterate over all selected checkboxes
$.each(rows_selected, function(index, rowId){
// Create a hidden element
$(form).append(
$('<input>')
.attr('type', 'hidden')
.attr('name', 'id[]')
.val(rowId)
);
});
// FOR DEMONSTRATION ONLY
// Output form data to a console
$('#example-console').text($(form).serialize());
console.log("Form submission", $(form).serialize());
// Remove added elements
$('input[name="id\[\]"]', form).remove();
// Prevent actual form submission
e.preventDefault();
});
});
This question has accepted answers - jump to:
This discussion has been closed.
Answers
Have you tried the steps here:
https://datatables.net/manual/tech-notes/4
What is the JSON response from the server?
Kevin
Am not so much good in coding.
sorry, How do I check JSON response?
But the error I get on browser is
DataTables warning: table id=example - Requested unknown parameter '1' for row 0. For more information about this error, please see http://datatables.net/tn/4
Also when I look in developer option in browser I get:
{"draw":0,"recordsTotal":9,"recordsFiltered":9,"data":[{"First name":"Edes","Last name":"Severin","Parent Mobile No":"25571762483","class":"V","Engl":"91","Bios":"90","Kisw":"92","Geog":"93","Hist":"96","Phys":"95","Maths":"98","Civics":"94","Chem":"97"},{"First name":"Edes","Last name":"Severin","Parent Mobile No":"25571762483","class":"V","Engl":"90","Bios":"92","Kisw":"93","Geog":"91","Hist":"96","Phys":"95","Maths":"94","Civics":"98","Chem":"97"},{"First name":"Edes","Last name":"Severin","Parent Mobile No":"+2557176248","class":"V","Engl":"90","Bios":"92","Kisw":"92","Geog":"92","Hist":"98","Phys":"95","Maths":"97","Civics":"96","Chem":"98"},{"First name":"John","Last name":"Charles","Parent Mobile No":"25576762436","class":"III","Engl":"80","Bios":"75","Kisw":"76","Geog":"81","Hist":"42","Phys":"93","Maths":"85","Civics":"65","Chem":"57"},{"First name":"Allen","Last name":"Kianga","Parent Mobile No":"25511111100","class":"IVB","Engl":"62","Bios":"36","Kisw":"45","Geog":"86","Hist":"92","Phys":"71","Maths":"64","Civics":"31","Chem":"52"},{"First name":"Jackline","Last name":"Leonard","Parent Mobile No":"25568819950","class":"VIC","Engl":"50","Bios":"83","Kisw":"95","Geog":"70","Hist":"68","Phys":"37","Maths":"96","Civics":"81","Chem":"84"},{"First name":"Rose","Last name":"Mary","Parent Mobile No":"+2557176248","class":"III","Engl":"90","Bios":"92","Kisw":"94","Geog":"98","Hist":"97","Phys":"94","Maths":"95","Civics":"93","Chem":"92"},{"First name":"Edes","Last name":"Severin","Parent Mobile No":"+2557176248","class":"VA","Engl":"92","Bios":"95","Kisw":"96","Geog":"98","Hist":"98","Phys":"98","Maths":"95","Civics":"96","Chem":"92"},{"First name":"Edes","Last name":"Severin","Parent Mobile No":"+2557176248","class":"Edes Shayo ","Engl":"52","Bios":"63","Kisw":"72","Geog":"81","Hist":"95","Phys":"98","Maths":"95","Civics":"97","Chem":"99"}]}
Looks like you need to add
columns.data
to your DataTable config. You will need something like this:Need to add a
data
object for each column using the name of the field returned in your JSON.Kevin
where do I add, because I tried but still same message.
Ooohhoo Great it has worked.
Thanks a great Kelvin.
Sorry Kevin not Kelvin.
I got something else appeared,
select all checkbox doesn't work and by click on any part of the row it check the box but also doesn't work
Anywhere in the Datatables initialization config. But you can put it just above
columnDefs
if you want.I count 13 columns returned in the json but 15 columns in your table.
Looks like you want the first column to be a checkbox so you need to define that column in your table header, otherwise it will be placed in your First Name column. Then your 13 columns in the json will populate 13 of the table column leaving these:
I'm guessing you want to calculate these columns. If you don't populate all the column you will get the
Unknown parameter
error. Maybe remove these two for testing until you are ready to generate the data.Kevin
Yes, now the checkbox is working.
Hello Kevin,
My sort, search option doesn't work.
Can you help me out
I don't have much experience with service side processing code but if you post new a new forum topic with your questions/issues others may be able to help.
Kevin
Okay, thanks a great.
Hi,
i having problems in get selected id is not posted in php
form append hidden values not get in php page.....