Custom column filter for DT

I have a dataset with a nested list column which I am passing to DT's createdCell function.

However, I want the use the FIRST number in the list for the column's range slider. I know DT has an internal function that is used to generate these sliders based on the column's type, but I was wondering if there is a way to set a custom filter/range?

I don't want to use the render function because I do need the list data to persist into the DT object, but I want to filter based on other data. Is that possible? Maybe there's a searching method within datatables I can leverage??

library(dplyr)

dat <- data.frame(
  a = letters[1:2],
  b = 1:2,
  c = I(
    list(
      list(
        3,
        list(aa = 1, bb = 1),
        list(aa = 2, bb = 2)
      ),
      list(
        4,
        list(aa = 3, bb = 3),
        list(aa = 4, bb = 4)
      )
    )
  )
)



DT::datatable(
  dat,
  # can I make a custom filter
  # and for the third column set it to the first number
  # before the list?
  # creating a 3-4 range for column 3?
  filter = "top",
  options = list(
    # here I need the list so I cant just use the render function
  # maybe there's some argument where I can pass the array of values I want to use in the slider?
    columnDefs = c(list(list(targets = 3,
                             createdCell = DT::JS('
                               function(td, cellData, rowData, row, col) {
                                 console.log(cellData)
                               }
                             '))
                        )
                   )
    )
  )

Answering my own question with a hack where I create a column using the data I want in the range, and then make a hidden list column but use that in the draw function:

library(dplyr)

dat <- data.frame(
  a = letters[1:2],
  b = 1:2,
  c = 3:4,
  hidden = I(
    list(
      list(
        list(aa = 1, bb = 1),
        list(aa = 2, bb = 2)
      ),
      list(
        list(aa = 3, bb = 3),
        list(aa = 4, bb = 4)
      )
    )
  )
)



DT::datatable(
  dat,
  # can I make a custom filter
  # and for the third column set it to the first number
  # before the list?
  # creating a 3-4 range for column 3?
  filter = "top",
  rownames = FALSE,
  
  options=list(
    columnDefs = list(
      list(visible=FALSE, targets=3),
      list(targets = 2,
           createdCell = DT::JS('
                               function(td, cellData, rowData, row, col) {
                                 console.log(rowData[3])
                               }
                             ')
      )
  ))
)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.