How to stop a Datatable loop

How to stop a Datatable loop

HieuHoangHieuHoang Posts: 6Questions: 3Answers: 0

I'm using rows().every of Datatable to compare a value in each rows. But I have so many rows, and I want to break the loop when find out the row include value I want.

I have tried as example below but it is not work:
Example 1: This example is not show any error

table.rows().every(function(rowIdx, tableLoop, rowLoop){
   if(this.nodes().to$().attr('id') == id){
     console.log('OK')
     return false
   }
})

Example 2: This example show a error when load the page Uncaught SyntaxError: Illegal break statement

table.rows().every(function(rowIdx, tableLoop, rowLoop){
   if(this.nodes().to$().attr('id') == id){
     console.log('OK')
     break
   }
})

So how can I do that?

Answers

  • kthorngrenkthorngren Posts: 13,606Questions: 25Answers: 3,204
    edited June 25

    This thread is asking the same question.

    Kevin

  • HieuHoangHieuHoang Posts: 6Questions: 3Answers: 0

    Thank Kevin

  • allanallan Posts: 54,574Questions: 1Answers: 8,534 Site admin

    Perhaps a better option in this case is to use a function selector:

    let matchingRows = table.rows((idx, data, node) => node.getAttribute('id') == id);
    

    Allan

Sign In or Register to comment.