简体   繁体   中英

JavaScript function for DT formatting (R shiny)

I need to design the R Shiny data table, using Java Script like that (like stairs on the DT bottom, which shows the prediction values - italic and gray).

在此处输入图像描述

I'm trying to use this function ( From this question ), but all my table is italic starting from 3rd column. How can I fix it? (suppose, that my for loop is incorrect)

  rowCallback <- function(rows){
c(
  "function(row, data, num){",
  sprintf("  var rows = [%s];", paste0(rows-1, collapse = ",")),
  "  for(let j=data.length; j >= 3; j--){",
  "    for(let i=j; i<data.length; i++){",
  "      $('td:eq('+i+')', row)",
  "        .css({'background-color': 'rgb(211,211,211)', 'font-style': 'italic', 'font-color': 'rgb(230,230,230)'});",
  "    }",
  "  }",
  "}"  
)

}

Not tested, I would try:

rowCallback <- c(
  "function(row, data, num, index){",
  "  var ncols = data.length;",
  "  for(let j=ncols; j > ncols-index; j--){",
  "    $('td:eq('+j+')', row)",
  "      .css({'background-color': 'rgb(211,211,211)', 'font-style': 'italic', 'font-color': 'rgb(230,230,230)'});",
  "  }",
  "}"  
)

Notes:

  • You don't need a R function, because the rows argument is not used.

  • $('td:eq('+j+')', row) selects the j-th element of the current row, so you don't have to loop over rows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM