Add search boxes to datatable.
Add search boxes to datatable.
classic12
Posts: 228Questions: 60Answers: 4
I have tried loads of different ways to get this to work.
I am using this link
https://www.datatables.net/release-datatables/examples/api/multi_filter_select.html
What is the correct syntax to get this working please?
Cheers
Steve Warby
var editor; // use a global for the submit and return data rendering in the examples
var selectedQuoteID;
var table;
// add filter boxes
btnGetData.onclick=function(){
$("#DataTable1").dataTable().fnDestroy();
$('#DataTable1').empty();
table = $('#DataTable1').DataTable( {
"order": [[0, 'desc']],
dom: "Bfrtip",
autoWidth : true,
responsive: true,
ajax: "http://toolfolks.com/surplusAdmin3/php/upload-many.php",
columns: [
{ data: "quotes.quoteID" , width : '25px', title : 'Quote No'},
{ data: "quotes.custID" , width : '25px', title : 'Cust No'},
{ data: "quotes.quoteDate" , width : '25px', title : ' Date'},
{ data: "quotes.quoteTitle" , width : '150px', title : ' Title'},
{ data: "quotes.notes" , width : '150px', title : ' Notes'},
{ data: "quotes.notesInternal" , width : '150px', title : ' Notes Internal'},
{ data: "sites.name" },
{
data: "files",
render: function ( d ) {
return d.length ?
d.length+' image(s)' :
'No image';
},
title: "Image"
}
],
select: true
} );
// Apply the search
table.columns().every( function () {
var that = this;
$( 'input', this.footer() ).on( 'keyup change', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
} );
} );
};
btnAddSearch.onclick=function(){
$('#DataTable1 tfoot th').each( function () {
var title = $(this).text();
$(this).html( '<input type="text" placeholder="Search '+title+'" />' );
} );
//var table2 = $('#DataTable1').DataTable();
table.columns().every( function () {
var that = this;
$( 'input', this.footer() ).on( 'keyup change', function () {
if ( that.search() !== this.value ) {
that
.search( this.value )
.draw();
}
} );
} );
}
This question has accepted answers - jump to:
This discussion has been closed.
Answers
I have also added
The filter works but only on the notes column no matter what value I put in .columns(6)
here www.toolfolks.com/surplusAdmin4
click getData
Cheers
Steve Warby
I'm struggling here guys.
I have also tried to use the CKEditor plugin but no errors in the console but it just does not show up.
The only things I can think of is
otherwise I get an error stating I can't re-initialise the datatable.
Could this be 'killing' the additional code ( out of my depth here)
So maybe I don't have them in the correct order ( image shown).
www.toolfolks.com/surplusAdmin6CKEditor/Screen Shot 2017-11-11 at 18.23.30.png
Other than that I'm at a loss.
www.toolfolks.com/surplusAdmin6CKEditor
Cheers
Steve Warby
There doesn't appear to be a footer in any of the tables on the linked page. As such,
$( 'input', this.footer() )
isn't going to find anything. Actually, worse than that, it will select all inputs sincethis.footer()
is undefined.Allan
I have tried this
Still nothing shows.
http://toolfolks.com/getTest/
Hit button 'local2'
Any ideas guys?
Cheers
Steve Warby
Unless I'm missing something, there still aren't any footer elements in the table - I would expect to see a
tfoot
element with a row and cells in it if you want to use a footer.Allan
I'm nearly there.
I am using this code to ad a footer and inputs with class searchEdit
I get error:
TypeError: table.columns is not a function. (In 'table.columns( 0 )', 'table.columns' is undefined).
line 176 column 18
What am I missing here ?
Also How do I say ( this column search ie the column I am in).
Cheers
Steve Warby
Somewhere before using it you need to assign
table
to the Datatable API using something like this:var table = $('#DataTable1').DataTable();
or you can try this:
Kevin
I do var table = $('#DataTable1').DataTable(); when I load the data. Before the search stuff.
I have used your code and a search is performed but no results.
http://toolfolks.com/getTest/
click local then add footer
Also How do I search on just the column I am in and not columns(0)
Cheers
Steve Warby
Looks like its working for column 0.
You probably will need to change your code to look like the example:
https://datatables.net/examples/api/multi_filter.html
Kevin
When I enter P in column 0 i get no results. Same in all the edit fields. Where do you get a filter ?
I have changed the code to:
and still get the same result. Updated at http://toolfolks.com/getTest/
I think the problem is your code is placing the
tfoot
below thetbody
which I believe is not proper HTML format. You caninspect
the footer to see where it is placed. The placement seems to affect how the input event works.I copied your code to this test case:
http://live.datatables.net/micujoka/1/edit
Open the console then type in one of the columns. You will see what you typed is output 6 times, one for each column. The search is iterating over each column not just the one you are typing in.
This example is the same code except the footer is in the HTML and the code to append the footer is commented.
http://live.datatables.net/mogilofi/1/edit
Now typing in the column outputs once in the console and the search works just for that column.
I'm not sure how to programmatically place the footer in the proper place. Maybe SO will have the answer or someone on this forum.
Kevin
The placement of the
tfoot
is correct, as long as it is a child of thetable
.The actual problem is that the
tfoot
needs to be present before initialising the datatable.So either put it in the html like in Kevin's second example, or swap these lines in Kevin's first example:
If there is no tfoot element present at the time of initialisation, then
this.footer()
for a column will returnundefined
and therefor add an event listener for every input on the page for every column. Every column will listen to a change in every input.Thanks for the clarification @HPB. Always learning something new.
Kevin
Thanks for the help guys but I am just not getting this.
What code do I need to add as I presume the IDE's component is not providing the footer itself.
If I use on a button
I get the row but inside
I then use
And I get the edits but with the same issue.
Assume idiot level please.....
Cheers
Steve Warby
Like HPB said try swapping the order of these two lines:
Kevin
My code on the addFooter button is now:
I get the same issue.
http://toolfolks.com/getTest/
hit get Footer button.
Your function is being run when it is already a DataTable. The table has been initialised when the page was loaded. So its already too late to add a footer (unless you destroy it, add one and then create a new DataTable).
This is an exact copy and paste of that function from your page on an uninitialised DataTable: http://live.datatables.net/kasezugi/1/edit .
Allan
Hi Allan,
just had this from NSBasic techies.
https://discuss.nsbasic.com/t/datatable-adding-edits-not-working/624/5
Ive added the stuff correctly ( I think ).
But it still fails.
What Allan is saying is that you are initializing the Datatable then clicking the 'Footer' button to add the footer. The point is you need to add the footer before initializing the Datatable. Maybe, as Allan indicated, you can try
destroy()
then adding the footer and reinitializing your Datatable.Kevin
Still not got this guys.
My codea folloes
Click Delete Table.
Click Add Footer Only. ( From what I see I dont think this actaully adds a footer.
Click Add Edits ( They appear in the table)
Click Add Data. Table populated.
I got this from the NSBasic guys.
AppStudioSupportLeader8h
Footers require some extra code modules.
You'll need to add these files to your manifest:
You'll need to add this to systemheaders:
So I have done
I have dragged the 2 files into my project.
I have added
<link rel="stylesheet" href="fixedHeader.bootstrap.min.css"/>
to extra headers.
I still have the same issue.
http://toolfolks.com/getTest/
FixedHeader has nothing to do with adding search inputs to the footer. FixedHeader allows the header to stay at the top of the scrolling window:
https://datatables.net/extensions/fixedheader/
And you are getting this error when trying to load it:
GET http://toolfolks.com/getTest/dataTables.fixedHeader.min.js 404 (Not Found)
So, unless you want that feature you don't need to load it.
Let's fix your "Add footer" button. Try using the below:
Notice the table is destroyed then the footer is added then the Datatable is initialized.
You currently have this:
We are swapping these two lines of code.
NOTE:
$('#DataTable1').dataTable().fnDestroy();
is the old command format from DT version 1.9. Since you are using a 1.10.x version you should try using the current version of the commands. In this case it would be$('#DataTable1').DataTable().destroy();
. Notice theD
in Datatable.Your
Add data
button is destroying and emptying the table then initializing Datatable without adding the footer first and the code to build the search inputs:$('#DataTable1').empty();
removes all the child nodes of the table including the footer.EDIT: Mis-spoke about
.empty()
. If you use that you won't be able to add the footer because the header is not there to clone. You probably will want to remove$('#DataTable1').empty();
from the Add data function.Kevin
Thanks Kevin,
got round sorting this out.
I have now added them into the header.
Many thanks
Cheers
Steve Warby
One more issue,
the newly created search edits seems to be messing up the responsive settings
www.surplusanywhere.com/surplusAnywhere7
Any ideas ?
Cheers
Steve Warby
Another issue I have just noticed.
When I click into the edits that column performs a sort. ( as if the sort icon has been clicked ).
Any ideas ?
Hi guys,
Bump on this,
anyone know why the sort is triggering and the responsive is now screwed?
Cheers
Steve Warby
I'm not sure what to do with your link:
www.surplusanywhere.com/surplusAnywhere7
Anyway I assume the responsive issue is that when expanding the hidden columns the search input appears instead of the column name. I copied your code into a test case:
http://live.datatables.net/torineco/1/edit
To fix both issues you can use
orderCellsTop
. You will need to create two header rows. The top will contain the title and sorting. The second will contain the search inputs. Additionally you will need to change the first line in your last code post to this:$('#dtDataChanged thead tr:eq(1) th').each( function (e) {
This way the search input is only applied to the second heading row.
Working example:
http://live.datatables.net/rumakepe/1/edit
I found that responsive doesn't support complex headers so the second row is not affected by responsive. Not sure how to workaround this. Maybe one of the responsive events can be used to hide/show the second header as needed.
Kevin
Hi Kevin,
thanks for the help so far.
www.surplusanywhere.com/surplusAnywhere7 was to show that the responsive elements are not working correctly.
On my app I don't see the 2 header rows only one. On mine it simply puts the edits in the row where the titles were.
Maybe something to do with the NSBasic IDE ????
I didn't see a Datatable so was not sure what to look at.
You need to create the two headers. You can see this in the HTML tab.
Kevin
Sorry click on the deals tab for the datatable
I am using NSBasic where I simply drag a datatable component onto the form.
Not sure how to add the extra header.
I'll ask in the NSBsic forum.
I asked in the NSBasic forum and they said I would have to use the API.
Is there an API to add another header ?
Cheers
Steve Warby