ColVis and stateSave

ColVis and stateSave

trongarttrongart Posts: 222Questions: 51Answers: 0

In this test case, certain cells of the Extn column are set to have a green background as set in createdRow. Everything is correct when the table loads. However, if I hide the Extn column and reload the table without it (state is saved with stateSave) and then unhide the Extn column, the same cells no longer have the green background.

Is there a way for stateSave to keep the setting from createdRow?

This question has accepted answers - jump to:

Answers

  • trongarttrongart Posts: 222Questions: 51Answers: 0
    edited August 2022

    Is there maybe a way to run the createdRow function every time a column is unhidden with ColVis:

    table.on( 'buttons-action', function ( e, buttonApi, dataTable, node, config ) {
            console.log('Run createdRow here');
    } );
    
  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    The rowCallback will run each time the row is drawn versus option createdRow which runs only once when the row is created. Use the column-visibility to execute draw() to force rowCallback to run. See this example:
    http://live.datatables.net/pusoquju/7/edit

    Kevin

  • trongarttrongart Posts: 222Questions: 51Answers: 0

    @kthorngren Thank you so much for your help with this!

    I have to use createdRow instead of rowCallback as the latter drains performance considerably especially for large tables and data. Is there maybe a way to keep createdRow in the initial table code and to run rowCallback only within the column-visibility event?

    Or alternatively, is it possible to run the specific setting

    if(data['extn']>=5000){ $(row).find('.data3').css('background-color', 'rgb(204, 255, 204)'); }
    within column-visibility event?

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    edited August 2022 Answer ✓

    I think you can use rows().every() with the selector-modifier of { page: "current" } to loop the rows on the page. Instead of var data = this.data();, as shown in the examples, use var node = this.node(); to get the row().node().

    Kevin

  • trongarttrongart Posts: 222Questions: 51Answers: 0

    @kthorngren Thank you for this suggestion! I applied rows().every() with node and the selector-modifier in this test case, but the same problem still occurrs: live.datatables.net/jocixaru/1/edit.

    Not sure if I've done this right:

    Would I need to run this inside rows().every()?

            if(data['extn']>=5000){
                $(row).find('.data3').css('background-color', 'rgb(204, 255, 204)');
            }
    

    Or can it rerun createdRow?

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    edited August 2022

    The createdRow runs only once when the row is created. There is nothing to force it to run again.

    See if this example does what you want:
    http://live.datatables.net/vesadezu/1/edit

    The { page: "current" } is used with roes().every(). Pasted your above code snippet into the loop and changed $(row) to $(node).

    Kevin

  • trongarttrongart Posts: 222Questions: 51Answers: 0
    edited August 2022

    Kevin, appreciate your help- This works well and I applied the code analogously into my project by adding table.on( 'column-visibility.dt',....

    However, the performance drain is the same as using rowCallback instead of createdRow for some reason.

    How is this possible when rows().every() only gets accessed after ColVis button click? If not, is there a way to limit rows().every() only to table.on( 'column-visibility.dt',...?

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    edited August 2022

    Guess I don't understand the question. The rows().every() is in the column-visibility event and should only run when the column visibility changes. The rows that rows().every() loops are those displayed on the page with { page: "current" }. How many rows do you have on the page?

    I don't believe the table.draw() is needed. It was used to run rowCallback.

    Kevin

  • trongarttrongart Posts: 222Questions: 51Answers: 0

    Removing table.draw() was the solution! Thank you so much!!

Sign In or Register to comment.