A selector for rows from X to Y?

A selector for rows from X to Y?

TablefakTablefak Posts: 35Questions: 8Answers: 0
edited July 2023 in Free community support

Say my table displays 10 rows. I can select them like this:

datatable.rows({"search": "applied"})

How do I further filter out rows from 3rd to 5th in this set, for example?

I tried using .splice, but it doesn't return an Api instance which I could further operate on.
I also tried using .filter, but my callback doesn't get called for every row (as I expected). Instead, it's only called once with a value like this:

This question has an accepted answers - jump to answer

Answers

  • TablefakTablefak Posts: 35Questions: 8Answers: 0

    I was able to solve it like this:

            const rowsIndexes = dtApi
                .rows({search: "applied", order: "applied"})
                .eq(0)
                .filter((rowID, i, dt) => {
                    return (i >= start) && (i <= end)
                })
                .toArray()
            dtApi.rows(rowsIndexes).select()
    

    But I'm wondering if there's a more elegant way.

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,953
    edited July 2023 Answer ✓

    Maybe use rows().indexes(). This example uses splice() to get the desired range of row indexes.

      var rowIndexes = table
      .rows({search: "applied", order: "applied"})
      .indexes()
      .splice(3, 3);
    
      table.rows().deselect();
      table.rows( rowIndexes ).select();
    

    https://live.datatables.net/qehubozu/2/edit

    Kevin

  • TablefakTablefak Posts: 35Questions: 8Answers: 0

    Hi, Kevin. Your code does what I want and is more elegant. Thank you.

Sign In or Register to comment.