I'm trying to put a simple searchbox above my datatable. Actually I got this far from another answer but can't manage to get the box do some actual search. I think I'd describe the situation as "Seems like I can't make the box talk with the datatable"
My datatable looks like this now.
var datatable = null;
$(document).ready(function () {
$.extend(true, $.fn.dataTable.defaults, {
"searching": false,
"ordering": false
});
//var data = "3";
@*var dataSourceUrl = "@Url.Action( Inbox ? "InboxList" : "OutboxList" ,"Folder")";*@
@*var dataSourceUrl = "@Url.Action( Inbox ? "InboxListByType" : "OutboxList" ,"Folder")";*@
if (Type == 1) {
var dataSourceUrl = "@Url.Action( Inbox ? "ERPListByType" : "OutboxList" ,"Folder")";
} else if (Type == 2) {
var dataSourceUrl = "@Url.Action( Inbox ? "InboxListByType" : "OutboxList" ,"Folder")";
} else {
var dataSourceUrl = "@Url.Action( Inbox ? "OutboxListByType" : "OutboxList" ,"Folder")";
}
datatable = $('#expandabledatatable').dataTable({
//"sDom": "Tflt<'row DTTTFooter'<'col-sm-6'i><'col-sm-6'p>>",
//"processing": true,
//info: false,
serverSide: true,
ajax: {
"url": dataSourceUrl,
"data": { DocumentTypeId: DocumentTypeId },
"type": "POST"
},
columns: [
{
"data": "Id",
"render": function (data, type, row) {
return "<label><input type='checkbox' value='" + data + "' name='chkGrid'><span class='text'></span></label>";
}
},
{ "data": "@Html.Raw(Inbox ? "SenderCompany" : "ReceiverCompany")" },
{ "data": "DocumentTypeName" },
{
"data": "RegistrationDate",
"render": function (data, type, row) {
return moment(parseInt(data.substr(6))).format('DD.MM.YYYY hh:mm');
}
},
{
"data": "RegistrationCode",
"render": function (data, type, row) {
console.log(row);
return "<a href='@Url.Action("View","Folder")/" + row["Id"] + "'>" + data + "</a>";
}
},
{ "data": "CustomsTransportType" },
{ "data": "VehicleIdNo" },
{ "data": null, "defaultContent": "" },
{ "data": "ConsignorName" },
{ "data": "ConsigneeName" },
{ "data": "TotalNoOfPackages" },
{ "data": "TotalGrossWeight" }
],
iDisplayLength: 10,
language: {
"info": "Toplam kayıt : \_TOTAL\_<br/> Gösterilen : \_START\_ - \_END\_",
"paginate": {
"first": "İlk",
"last": "Son",
"next": "İleri",
"previous": "Geri"
}
}
});
var table = $('#expandabledatatable').DataTable();
$(function () {
$('#inpSearch').on('keyup', function () {
alert(this.value);
table
.search(this.value)
.draw();
});
});
});
It's clear I'm missing something but I can't understand what. Can anyone help me?
EDIT: This question was marked as a duplicate and having an answer on another question but I can't get those answers to work either. Probably I'm missing something.
I made some changes on datatable code and updated it inside the question. Datatables.net says columns.searchable is true by default so I removed that setting from my code.
I can get an alert when a key is pressed inside the textbox but no actual search gets performed. I'm using datatables version 1.10.5 by the way.
EDIT 2: I noticed each keystroke inside the text type input I put above the datatable causes the same query to be sent as a POST.
EDIT 3: I think this is not a case which I can handle with a jquery function. It seems like I need to do the job on server-side.