distinguish between init draws and search draw

distinguish between init draws and search draw

NevoNevo Posts: 3Questions: 1Answers: 0
edited March 2023 in Free community support

Hi All,

Im using the draw() event and want to know if there is a way dto distinguish between draw types.

For example, init draw will be used to build a select element but a search draw only to filter row results without re-build the select element options.

//Init draw 
$table.on('draw.dt', function (e) {
    var results = $table
                        .column( $index )
                        .data()
                        .unique();
   //build the select element
})

//Search draw
$table.on('draw.dt', function (e) {
    
   //Do search stuff
})

Thank you,
Nevo

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    Use init for the first build.

    Allan

  • NevoNevo Posts: 3Questions: 1Answers: 0

    Thank you Allan,

    The init function will not be a good option in my case as it fires when the table complete construct.

    The rows are added later (after filtering).

    So the sequence is table construct -> rows draw.

    Thats why i wanted to find a way to distinguish between the two.
    Is there a way to pass a variable with the draw?
    something like: .draw('init') and .draw('reload')?

    Thanks,
    Nevo

  • kthorngrenkthorngren Posts: 21,341Questions: 26Answers: 4,954

    When you say init you mean when new rows are initially added to the table, right? In this case there is nothing to pass into the draw event to note if the draw is a result of adding rows.

    One option might be to use a global variable as a flag. Set it true when you add the rows. In the draw event check the flag, if true rebuild the select lists. At the end of the draw function always set the flag false.

    Kevin

  • NevoNevo Posts: 3Questions: 1Answers: 0

    Thank you Kevin,

    I thought about such solution but hoping to avoid more maintenance.

    Nevo.

Sign In or Register to comment.