Attempting to auto select rows on table creation

Attempting to auto select rows on table creation

greggo47greggo47 Posts: 8Questions: 4Answers: 0
edited July 2020 in Free community support

I'm attempting to auto select certain rows based on data I'm getting from an API inside the createdRow function, it does not seem to be working. Should I be doing this somewhere else?

createdRow: function (row, data, dataIndex) {
      // Create custom column design
      for (var i = 0; i < selectedPorts.length; i++) {
        if (selectedPorts[i].PortID == data.PortID) {
          groupTable.row(`:eq(${dataIndex})`).select();
          break;
        }
      }
    }

if I use groupTable.row(':eq(0)').select(); after the table has been created, it works fine (with a number and not dataIndex)

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923
    Answer ✓

    Using createdRow is probably too early to select rows. You are probably getting errors indicating groupTable is undefined because initialization is still in progress. I would use rowCallback. You can get an instance of the Datatables API and use rows().every() to iterate all the rows. Here is a simple example:
    http://live.datatables.net/pevonawu/1/edit

    Kevin

  • allanallan Posts: 63,213Questions: 1Answers: 10,415 Site admin

    How are you adding the row? If you are using row.add() or rows.add() they are auto-populated with a selection for the new rows. So you can do table.row.add(...).select();.

    Allan

  • greggo47greggo47 Posts: 8Questions: 4Answers: 0

    Thanks kthorngren, I did not use the rowCallback, but the drawCallback in the example you showed and it worked well.

This discussion has been closed.