Pre-selecting a row and making sure its on the current page...
Pre-selecting a row and making sure its on the current page...
Hi all!
I'm fairly new to both JS and JQuery, and very new to this plugin, but I must say it rocks!
The one thing I haven't managed to work out how to do, is when the table is rendered, how do I select a specific row and make sure it's on the displayed table page?
Obviously I can add the "selected" class to the row when I render the HTML, but I have the feeling this isn't the correct solution. It feels like triggering a click on the specific row using JQuery might be better, but then how do I ensure the relevant page of the data is displayed?
Thanks in advance!
Chris.
This question has accepted answers - jump to:
Answers
See if the
row().show()
plugin is what you ware looking for:https://datatables.net/plug-ins/api/row().show()
Kevin
Thanks for the link Kevin.
I had actually located this page but I'm not sure I fully understand how to use it. I found the example a bit confusing.
Here is an example I previously built to try it out:
http://live.datatables.net/hatojihe/3/edit
Click the button "Find Quinn". It uses the select extension to highlight the row. You can remove
.select()
if you don't want to highlight the row.Kevin
Thank you! That I understand!
Although Im a little puzzled by why you have
To stay on the current page. If I just had
.draw();
Datatables would display the first page. This is documented in thedraw()
docs.Kevin
I almost have this working perfectly now thanks to you!
The only problem is, if the value of variable `defaultReg' isn't in the table I get an error. Try as I might I cant get the code right to stop the error.
That is certain to be a limitation of my poor JavaScript rather than your excellent solution!
My approach would be to define
defaultReg
before the ajax call, for example:This way the variable exists and you won't get the error
defaultReg is not defined
. Assuming that is the error you are getting.Kevin
Sorry! I should have said the error is:
So it seems to be when the .draw clause is called. Im assuming that because the .row function didn't find a row, it is perhaps undefined?
I see. I think this example change will help:
http://live.datatables.net/gefemike/1/edit
It tries to find the row first. If one is returned then it will show the row otherwise it does
nothing.
Using
return data[0] === 'QuinnFlynn';
should result in no error and page 1 displayed. Changing to returndata[0] === 'Quinn Flynn';
should show the page Quinn is on.Kevin