class
class
https://live.datatables.net/vohiyelo/3/edit
Error as seen in the test case:
Trying to add two classes to all odd rows.
How to add two classes to all odd rows that needs to work with both client side and server side process?
https://live.datatables.net/vohiyelo/3/edit
Error as seen in the test case:
Trying to add two classes to all odd rows.
How to add two classes to all odd rows that needs to work with both client side and server side process?
Answers
That will remove the
oddclass from any existing rows in the body (for cases when a row was odd, but has been moved to become even!), and then adds it on.However, if it is just for styling, then just use CSS:
Allan
Thanks for the reply.
Two more matters to get clarity.
1- Difference between (in simple words)
a) "rowCallback": function ()
b) "drawCallback": function ()
2 How to add it rowCallback / drawCallback function on a button click ?
george.
The
option rowCallbackanddrawCallbackdocs explain it best.You can use a
console.log()statement in each callback to see the behavior and when the run.Call
draw()to execute theoption rowCallbackanddrawCallbackfunctions.Kevin
Thanks for the reply.
Actually don't know the logic required to add the below with a button click
"drawCallback": function () {
$('#example tbody tr').removeClass('odd');
$('#example tbody tr:odd').addClass('odd')
}
can you show me onetime ?
george
Like this:
https://live.datatables.net/vohiyelo/7/edit
Not sure having a button for just redrawing the table to execute that particular code in
drawCallbackis very useful.drawCallbackwill run anytime the table is drawn for example anytime the table is sorted, searched or changing the page.What are you trying to do with the button?
Kevin
What are you trying to do with the button?
Answer - only call or function the 'drawCallback' on that button click.
I don't know that 'how to do that.
george
As I said to execute the
drawCallbackfunction call thedraw()API. This is what my example shows:https://live.datatables.net/vohiyelo/7/edit
However maybe you just want to execute an independent function when clicking a button. Something like this:
https://live.datatables.net/vohiyelo/9/edit
Sorry, I'm still not clear on what exactly your requirement is.
Kevin
Maybe this example of calling the
drawCallbackfunction using a button click will be more clear:https://live.datatables.net/vohiyelo/14/edit
If you still have questions then please provide details of what you want to do within the function when the button is clicked. Understanding your requirements will allow us to provide better suggestions.
Kevin
Hi,
Your question '..........what you want to do.........' ?
For example the drawCallback function.
So normally we can use it as
but new thing was 'how to do that on a button click'
So many ways on few days we play with our own knowledge and findings.
And when we think that unable to solve us that time we make a post.
Now few days we playing on with how to do with odd and even columns for the same drawCallback function.
$('#example tbody td:even').addClass('even-column');
we try with these
td:nth-child(even)
td:even
tbody td:nth-child(even)
tr td:nth-child(even)
etc.
Hope you understand us ?
How to do it with odd and even column for the same drawCallback function ?
george & friends.
Set a variable when the button is clicked and check for that variable in the draw callback, which is exactly what Kevin showed in his latest example. Perhaps you want it to toggle? Updated example here.
Allan
Hi,
The last question was how to use with odd and even column to add a class with using drawCallback function method
The test case at https://live.datatables.net/vohiyelo/20/edit
As mentioned in the previous query / reply we express some of the methods we used ?
Or whether it is impossible to add a calss to all even and odd column using drawCallback function ?
If it is not possible then what's the next best method ?
george and friends.
Your CSS was:
But you are applying the class to
tdelements. Use:Allan
There are no restrictions to using `-option drawCallback. It is a standard Javascript function. However it only executes when the Datatable is redrawn.
Right click and inspect on of the rows. You can see the
red-oddclass is added to the odd columns:You don't have a CSS defined to apply any styling for a
tdwith that class. For example:https://live.datatables.net/redizuxu/1/edit
Kevin
Hi,
td.red-odd {
color: violet;
}
and
drawCallback: function () {
https://live.datatables.net/redizuxu/2/edit
everything works fine until we hide a column.
If we hide a column then the odd column class is not changing.
then clicking for the next page everything goes scramble.
george and friends
Use the
column-visibilityevent to calldraw()when the visibility changes. Also you will want to remove thered-oddclass from alltds indrawCallback. Updated example:https://live.datatables.net/redizuxu/4/edit
Kevin
Just to add to my last comment. Many Datatables actions that affect the columns displayed, like responsive or colReorder, have events that fire so you can perform an action when these events occur.
Kevin
Hi,
Test case at https://live.datatables.net/tuxohazo/2/edit
What we trying to achieve is 'add a class to that hyperlink if clicked'.
But at the test case if click a link in any page other than the first page the class appear to all links in the first page only.
we trying to add the class only the clicked link.
not yet success.
george and friends
This isn't a Datatables issue. First start by clicking on the
Live previewbutton. The button is the upward/right facing arrow in the top right corner of theOutputtab. Now you can see the behavior of your page.When clicking a link only that link has the class toggled. The other link on the page is unaffected.
However if you reload the page then all the links on that page have the class added. Its due to this code:
You have the same URL for each link so this is applying the restored localStorage to all the
https://www.w3schools.com/inks on the page. If the page ha unique links then the code should work. If they aren't unique then you will need to save something like the row ID (row().id()) or row index (row().index()) or some other unique item from each row.Kevin
There is one change you will need to make. Once Datatables initializes then only those rows on the current page are in the document. Use
rows().nodes()as part of the jQuery selector to find all the rows:https://live.datatables.net/tuxohazo/3/edit
Note that I changed the URLs to be unique.
Kevin
Use the
:visitedpseudo class.As Kevin says, this isn't a DataTables issue. General CSS / JS questions are bast directed to StackOverflow.
Allan
Hi,
Kevin - one more thing is there that how to focus on each cell rather than unique id to the link ?
may be same link is applicable for different matters ?
Allan - why we contacted datatables.net is you are the creators. So you know things better than anyone ?
We like to express our words as like you that data contains anything and the requirements may differ as well ?
So we believe every data and requirements get noticed for improvements to get versatile solutions ?
note : same like heath and safety methods / procedures that every incidents and accidents may reported ?
george and friends.
My point was that for general Javascript and CSS questions, you should ask on StackOverflow or similar. For DataTables specific questions, yes, of course ask here.
Without knowing your data structure it's hard to say specifically. I would set a unique ID for each row. If you are using
ajaxordataloaded data then use therowIdto tell Datatables the row property to use as the ID.In the click event use
row().id()to get the ID and store that in the loacalStorage array. Usecell().node()to fetch the appropriate column when restoring the links, for example:Updated test case:
https://live.datatables.net/lekavaku/1/edit
I added the
idattribute to each row and reverted the URLs to the same link.You may need to clear the localStore to remove the stored URL's.
Kevin
Hi,
DataTable('#example', {
"createdRow": function(row, data, dataIndex) {
// Assign a sequential ID starting from 1
$(row).attr('id', 'row-' + (dataIndex + 1));
}
Whether the above is the correct method ?
and updated case at https://live.datatables.net/tuxohazo/5/edit
but the class is not adding once clicked the link.
george and friends.
First your test case isn't running as you expect. This error is showing in the browser's console:
You have this:
The Datatable is initialized in line 1. Line 4 is generating an error due to incorrect syntax. See the docs for the two ways Datatables is initialized.
First remove line 1 so you don't get the Warning: Cannot reinitialise DataTable error. Next add
newto line 4 like this:Using
createdRowto add the row ID's is too late for Datatable to read the IDs, during initialization, for use with therow().id()API. Datatables reads each row of an HTML sourced table and will keep track of the assigned ID of eachtrin it's data cache.createdRowhappens after this initial loading of the HTML table.Instead use jQuery to get the ID attribute in the click event, for example:
https://live.datatables.net/tuxohazo/7/edit
Using
createdRowmay not work reliably. If the HTML table rows change order or quantity on reload then the IDs won't be the same. It's best to have the IDs as part of the original data source so they are always the same.Kevin
Hi,
$(document).ready(function() {
var table = $('#example').DataTable(); // Initialize your DataTable
});
Whether possible to add other functions like createdRow / drawCallback / etc. etc. as a standalone method without changing the above initialization method ?
2- How to find all available functions like createdRow / drawCallback / etc. etc. from your website ?
3- What are the best methods to add other functions to both client and server side processing after initializing with the above method ?
george
The Initializing Datatables docs docs state this:
Essentially if you want to use non-default settings then add them into the initialization object.
These can be found by clicking the Reference link found on the left side menu of this page. The Options link contains all the docs for the initialization options.
Datatables doesn't support adding options after initialization. You would need to reinitialize Datatables with the new set of options. To reinitialize you will need to destroy the current Datatable.
Kevin
Hi,
For example column 1.
How to use / express each state visibility to do something ?
How to make the length menu to any other number (for example 25 rows per page and total selections have 10, 20, 25. 50 etc.) rather than the first one as default setting ?
Rebecca / George & friends.
Hi,
https://live.datatables.net/tuxohazo/7/edit
How to add
table.on('draw', function() {}
and
table.on('draw', function() {}
Hanna / George & friends.
Hi,
But read it as
drawCallback: function(settings) {}
Hanna / George & friends.