所以我希望将来有其他人会有同样的问题.下面的代码是大约25个不同帖子的结果,这些帖子在数据表网站上有很多和很多阅读.我最终能够完成我原本想做的所有事情 – 它只是让我永远研究如何做到这一点!这是我解决它的方式:
首先,我对databaseSearch.php的查询:
$searchQuery = "SELECT * FROM alldata where $searchBy like '%$searchValue%' LIMIT 400" ; //limiting helps with that memory overflow error in my original question
mysqli_set_charset($con, 'utf8');
$searchResult = mysqli_query($con, $searchQuery);
然后,从带回的数据中创建一个数组:
while ($row = mysqli_fetch_row($searchResult)) {
$item = array();
$item["id"] = $row[0];
$item["dateReceived"] = $row[1];
$item["comments"] = $row[2];
$output[] = $item;
}
$out = array('aaData' => $output);
echo json_encode($out);
然后,整个数据表代码:
function format ( d ) {
// `d` is the original data object for the row
return '
';}
$.fn.dataTable.ext.errMode = 'throw'; //shows errors in console
var table = $('#table').DataTable( {
ajax : {
url : 'databaseSearch.php' ,
dataSrc : 'aaData' ,
type : 'POST',
data: {searchBy:searchBy, searchValue:searchValue, options:options},
},
//the "createdRow" function allows me to do additional things with the rows I output.
"createdRow" : function (row,data,index) {
$('td',row).eq(0).attr('id', 'customer-' + index); //like create an id
$('td',row).eq(1).attr('id', 'location-' + index);
$('td',row).eq(2).attr('id', 'zip-' + index);
$('td',row).eq(3).attr('id', 'projectName-' + index);
$('td',row).eq(3).attr('id', 'zDate-' + index);
$('td',row).eq(7).attr('id', 'ID-' + index);
//This sections handles the icons that are placed in the first cell
//This adds either a valid or invalid flag to the first cell
if ( data["zDate"]) {
SelectedDate = new Date(data["zDate"]);
if (SelectedDate > CurrentDate) {
zImage = $('').attr('src', '../../img/valid.png');
$('td',row).eq(0).append(zImage);
//adding this class allows me to absolutely position the debit image for each line.
$('td',row).eq(0).addClass( 'icons' ); //or add classes...
} else {
zImage = $('').attr('src', '../../img/invalid.png');
$('td',row).eq(0).append(zImage); //or apply images...
$('td',row).eq(0).addClass( 'icons' );
}
}
},
// "columns" is the top level
s that will be created.columns : [
//{ className : 'details-control'},
{ data : 'customer' },
{ data : 'location' },
{ data : 'zip' },
{ data : 'projectName' },
{ data : 'ID' },
],
"columnDefs": [
{ className: "details-control", "targets": [ 0 ] }
],
"lengthMenu": [ 25, 50, 101 ],
"oLanguage": {
"sSearch": "Filter Results: "
}
});