How to highlight some specific rows on the print layout of jquery datatable?
How to highlight some specific rows on the print layout of jquery datatable?
im_nazmul
Posts: 8Questions: 1Answers: 0
I want to highlight specific rows while printing data from the jquery data table. I added a class to those rows by createdRow
and added some CSS style to that class. Now I want to keep that styling on those rows while printing. The code I've used to add class
"createdRow": function (row, data, dataIndex) {
var date = new Date(data.dateOfReg);
var days = datediff(date, new Date());
if (days >= 30 && parseInt(data.received) < 2000 && parseInt(data.payment) >= 2000) {
$(row).addClass('d-flag');
}
}
In the image, you can see some rows have a background color. I just want to keep that while printing data.
This question has accepted answers - jump to:
Answers
Hi,
You need to use the
customize
callback of theprint
button. With that you can inject styles or anything else for the print document. See also this example for how to use thecustomize
callback.Allan
Hello Allan,
On print layout all classes were removed and there's only raw css. How can i apply styles using this callback function?
Thank you.
This example here changes the background colour red on the print, you'll need to do something like that. If the print layout is removing the styles, that'll be an option on the print dialogue - for me, there's a checkbox called "background graphics" that enables the colour, see here:
Colin
Hello Colin,
In order to color those rows, I need to select them first. You can see I'm using a condition at
createdRow
to archive that. How can I do the same oncustomize
callback function?Thank you.
You'd need to select them using DOM methods. DataTables itself isn't running in the target window, it is just a plain table. So you would need to do something like:
It isn't ideal I know, but that's currently the way it needs to be done.
Allan
Hello Allan,
I know I can select rows this way but this will select all rows. If you look at the condition I want to select some specific rows, not all rows.
Thank you for your kind concern.
Looks like you are adding the class
d-flag
to the selected rows. does something like this work?Kevin
Hello Kevin
No. Because there is no class on the print layout.
Thank you
I guess thats right - should read what Allan says closer
Sounds like you will need to loop through all the rows and do your data comparisons similar to the
createdRow
. Maybe something like this:http://live.datatables.net/lorozuma/1/edit
Kevin
Hello Kevin,
Thanks for pointing me directly to what I'm not seeing. I have a question Is it possible to select a column by name instead of by index (
'td:eq(2)'
)?Thank you so much
You are limited by the jQuery selectors that are available for the generated HTML of the print preview.
If you really wanted to you could use
column().index()
with thecolumn-selector
of {string}:name to get the index by name. You can then use this in the:eq()
selector.Kevin
Thank you so much for your help Kevin
I've got the index of those columns by this method.
and that's well worked for me. Is there any suggestion that makes this code more efficient?
Thank You