如何避免数据表中行回调和排序之间的混淆 [英] How to avoid mixup between rowcallback and sorting in datatable

查看:67
本文介绍了如何避免数据表中行回调和排序之间的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助先前的



,然后排序:(带有叉号



 库(发光)
库(DT)

mtcars<-as.data.table(mtcars [1:15,)
ui<-fluidPage(
#actionButton('SubmitRemoval','排除选定的行'),
#actionButton('UndoRemoval','包括完整数据'),
#br(),
DTOutput('metadataTable')



服务器<-函数(输入,输出,会话){

值<-reactValues()

rowCallbackMeta =函数(行){
c(
function(row,data,num,index){,
sprintf( var rows = [%s];,paste0(rows-1,塌陷=,)),
if(rows.indexOf(num)> -1){,
for(var i = 0; i< data.length; i ++){,
$('td:eq('+ i +')',row) ,
.css({'color':'rgb(211,211,211)','font-style':'italic'});,
},
} ,
$('td:eq(3)',行).html(data [3] .toExponential(2));,
}

}
output $ metadataTable<-DT :: renderDataTable({

行<-values $ RowsRemove

#mtcars1<-cbind(Selected ='< span style = color:#31C769; font-size:18px>< i class = fa fa-check< / i>< / span>',mtcars)
mtcars1<-cbind(Selected ='< span style = color:red; font-size:18px< i class = glyphicon glyphicon-ok< / i> // span>',mtcars)

print(rows)
#if(!is.null(rows)){
mtcars1 $ Selected [rows]<-'< span style = color:red; font-size:18px>< i class = glyphicon glyphicon-remove< / i< / s pan>'
#}

Table_opts<-list(
dom ='frtipB',
search = F,
pageLength = 50,
searchHighlight = TRUE,
colReorder = TRUE,
fixedHeader = TRUE,
按钮= list('copy','csv',
list(
扩展=集合,
文本='取消选择',
操作= DT :: JS(函数(e,dt,node,config){
Shiny.setInputValue('SubmitRemoval', true,{priority:'event'});
})
),
list(
extend = collection,
text ='Restore',
action = DT :: JS( 函数(e,dt,node,config){
Shiny.setInputValue('UndoRemoval',true,{priority:'event'});
})

),
分页= TRUE,
deferRender = TRUE,
columnDefs = list(list(class(className ='dt-right',targets ='_all'))),
rowCallback = JS(rowCallbackMeta(rows)),
scrollX = T,
scrollY = 440

DT :: datatable(mtcars1,
escape = FALSE,
扩展名= c('Buttons','ColReorder','FixedHeader','Scroller'),
选择= c('multiple'),
行名= FALSE

选项= Table_opts

})


watchEvent(values $ RowsRemove,{
print (看到行删除)
values $ Datafiles_meta_Selected<-values $ Datafiles_meta_Selected [-c(values $ RowsRemove),]
})

watchEvent(input [[' SubmitRemoval']],{
if(is.null(values $ RowsRemove)){values $ RowsRemove<-as.numeric()}
values $ RowsRemove<-unique(c(values $ RowsRemove,输入[[ metadataTable_rows_selected]]))
})

watchEvent(input [[ UndoRemoval]],{
values $ RowsRemove<-NULL
values $ Datafiles_meta_Selected<-values $ Datafiles_meta
}}

}

ShinyApp(ui,server)


解决方案

您正在使用的 num javascript选择要变灰的行是基于当前显示的行号



您可以尝试替换中的 if 语句rowCallbackMeta 函数通过:

  if(data [0] .search('remove')> -1)

这会在数据的第一列中查找删除以排除行,并且之所以起作用,是因为排除行时,第一列中的glyphicon已更新为< i class = glyphicon glyphicon-remove< / i>


With the help of a previous question, I can now style selected rows (intended for the user to select rows to be excluded from further analysis), but I have found out that sorting the datatable after executing the functionality to exclude rows (gray them out and add a different icon, keeps the icon in the correct row, but grays out the wrong rows.

here is the table after deselecting rows 2,3 and 4 before sorting:

and after sort: (with the crosses at the right rows, but the graying out not.

   library(shiny)
      library(DT)

  mtcars <- as.data.table(mtcars[1:15, )
  ui <- fluidPage(
    # actionButton('SubmitRemoval', 'Exclude selected rows'),
    # actionButton('UndoRemoval', 'Include full data'),
    # br(),
    DTOutput('metadataTable')

  )

  server <- function(input, output,session) {

    values <- reactiveValues()

    rowCallbackMeta = function(rows){
      c(
        "function(row, data, num, index){",
        sprintf("  var rows = [%s];", paste0(rows-1, collapse = ",")),
        "  if(rows.indexOf(num) > -1){",
        "    for(var i=0; i<data.length; i++){",
        "      $('td:eq('+i+')', row)",
        "        .css({'color': 'rgb(211,211,211)', 'font-style': 'italic'});",
        "    }",
        "  }",
        "    $('td:eq(3)', row).html(data[3].toExponential(2));",
        "}"  
      )
    }
    output$metadataTable <-  DT::renderDataTable({

      rows <- values$RowsRemove

      # mtcars1 <- cbind(Selected ='<span style = "color:#31C769 ; font-size:18px"><i class="fa fa-check"></i></span>', mtcars)
      mtcars1 <- cbind(Selected ='<span style = "color:red ; font-size:18px"><i class="glyphicon glyphicon-ok"></i></span>', mtcars)

      print(rows)
      # if(!is.null(rows)) { 
      mtcars1$Selected[rows] <- '<span style = "color:red ; font-size:18px"><i class="glyphicon glyphicon-remove"></i></span>' 
      # }

      Table_opts <- list(
        dom = 'frtipB',
        searching = F,
        pageLength = 50,
        searchHighlight = TRUE,
        colReorder = TRUE,
        fixedHeader = TRUE,
        buttons = list('copy', 'csv',
                       list(
                         extend = "collection",
                         text = 'Deselect', 
                         action = DT::JS("function ( e, dt, node, config ) {
                                       Shiny.setInputValue('SubmitRemoval', true, {priority: 'event'});
                                     }")
                       ),
                       list(
                         extend = "collection",
                         text = 'Restore', 
                         action = DT::JS("function ( e, dt, node, config ) {
                                       Shiny.setInputValue('UndoRemoval', true, {priority: 'event'});
                                     }")
                       )
        ),
        paging    = TRUE,
        deferRender = TRUE,
        columnDefs = list(list(className = 'dt-right', targets = '_all')),
        rowCallback = JS(rowCallbackMeta(rows)),
        scrollX = T,
        scrollY = 440
      )
      DT::datatable(mtcars1, 
                    escape = FALSE, 
                    extensions = c('Buttons', 'ColReorder', 'FixedHeader', 'Scroller'),
                    selection = c('multiple'),
                    rownames = FALSE
                    ,
                    options = Table_opts
      )
    })


    observeEvent(values$RowsRemove, {
      print('seeing rows remove')
      values$Datafiles_meta_Selected  <-  values$Datafiles_meta_Selected[-c(values$RowsRemove),]
    })

    observeEvent(input[['SubmitRemoval']], { 
      if(is.null(values$RowsRemove)) { values$RowsRemove <- as.numeric()}
      values$RowsRemove <- unique(c(values$RowsRemove, input[["metadataTable_rows_selected"]]))
    })

    observeEvent(input[["UndoRemoval"]], { 
      values$RowsRemove <- NULL
      values$Datafiles_meta_Selected <- values$Datafiles_meta
    })

  }

  shinyApp(ui, server)

解决方案

The num you are using in your javascript to select rows to gray out is based on the row number on the current display so not impacted by sorting.

You could try replacing your if statement in your rowCallbackMeta function by:

if(data[0].search('remove') > -1)

This looks for "remove" in the first column of the data to exclude rows, and works because your glyphicon in the first column is updated to <i class="glyphicon glyphicon-remove"></i> when you exclude rows.

这篇关于如何避免数据表中行回调和排序之间的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆