Passing a global variable to state save? Draw?
Passing a global variable to state save? Draw?
Hello, I am working on my application and I am wondering if there is a way to pass an altered Javascript global variable (unrelated to the table) to a row callback and columns.render() call from the initial loading of the table to the next draw. I am using checkboxes to show and hide columns, but this changes the data within the tables, so I am maintaining an array of booleans that indicate whether or not to show a column (I am not using buttons because I need to check the visibility of each column upon redrawing). I use this array of booleans to alter the data to be shown in the table upon redrawing. Is there a method that would be helpful that I am missing? I've been looking at the DataTables for a week now trying to resolve this problem, so any help would be greatly appreciated.
Please let me know if you need any additional information from me. Thanks.
This question has an accepted answers - jump to answer
Answers
Alternatively, is it possible to access column.visible() from a row callback?
Hi,
I'm afraid I don't quite understand what you are trying to do. Are you able to illustrate with an example please?
Thanks,
Allan
I am making an app that compares product prices among sellers. I have checkboxes that allow the user to show / hide columns for the sellers. In another column (not hideable), I have my company's price, which includes displayed data from a hidden column that indicates the discount (the price difference between my company and the lowest price of the sellers whose columns are visible). Here is a visual:
For each row, of the visible sellers, the cell with the lowest price will be highlighted green, and the discount will be computed using the "green" price and the CDO price. The issue is that this constantly changes depending on which columns are visible. I tried to use a global array of booleans to store whether or not a sellers' column was visible and refer back to the array when 1. rendering the CDO column (to determine the discount) and 2. determining which cell to highlight green.
When I tried the array method, the table would simply re-draw itself and the array would be re-initialized to showing all columns (all true).
As an alternate approach, in a row callback function, I tried to call table.column(i).visible() but I receive an error in the console. So, is there any way to check for individual column visibility upon re-drawing the table?
Again, please let me know if you need any more information from me. Thanks.
As a note, the image is not accurate because the green highlighting is off. This is because I am in the process of trying to resolve the re-drawing issue; the coloring works well upon initialization (so $15.66, $56.86, and $75.01 would be highlighted green).
Ah! Thanks for the extra details.
rowCallback
is currently the way to do this. It will be called whenever the data in the table is updated, so you can modify the cell classes as needed.What was the error you got when using that method? Also what code were you using inside it?
Allan
This is my error when I call table.column(i).visible() :
Uncaught TypeError: Cannot read property 'column' of undefined
at jQuery.fn.init.rowCallback (localhost/:316)
at datatables.js:6648
at Function.map (jquery-3.3.1.js:443)
at _fnCallbackFire (datatables.js:6647)
at _fnDraw (datatables.js:3478)
at _fnReDraw (datatables.js:3562)
at _fnInitialise (datatables.js:4744)
at loadedInit (datatables.js:1306)
at loaded (datatables.js:6424)
at _fnLoadState (datatables.js:6435)
When I try calling this.column.visible(i), I get this error:
Uncaught TypeError: this.column is not a function
When I try calling column(i).visible, I get this error:
Uncaught ReferenceError: column is not defined
This is the code in the callback function:
this.api()
is how to get an API reference in therowCallback
method. e.g.Allan
Amazing! That solved most of the problem. Is there a way to call this.api() in a call to "render"? I get the error:
Uncaught TypeError: this.api is not a function
at render
Thanks again!
Note that I am calling "render" within the "columnDefs" array.
This is so that I can manipulate the data in the CDO column to show the updated discount -- the row callback has fixed the coloring issue, but now I need to change the discount appropriately and resort the column. If it is not possible to access the datatable API from "render," is it possible to manipulate which data is displayed and sorted in the row callback?
I just resolved this issue by using:
Based on this discussion: https://datatables.net/forums/discussion/comment/88687/#Comment_88687
Thanks so much for all of your help!! My table works beautifully now
I wouldn't normally recommand using the API in the
render
callback as there is a bit of overhead involved. What are you doing in it?Allan