สีพื้นหลังในพื้นที่ส่วนหัวของชื่อแถวของ DataTable (ซ้ายบน)

การใช้ scrollX = TRUE จะทำให้สีพื้นหลังของช่องว่างระหว่างชื่อ colnames และชื่อแถวของ datatable ไม่ถูกต้อง การเปลี่ยนเป็น scrollX = FALSE ใช้งานได้ แต่จะลบเอฟเฟกต์ fixedColumns ด้วย

output$monthly_summary_FA <- renderDataTable({
mytable <- FA_table() %>% column_to_rownames('Time') %>% as.matrix() %>% t() %>% as.data.frame()
colnames(mytable) <- paste0('<span style="color:black;font-family:Arial Narrow, sans- 
serif;">',colnames(mytable),'</span>')
rownames(mytable) <- paste0('<span style="color:black;font-family:Arial Narrow, sans-serif;font- 
weight: bold;">', rownames(mytable),'</span>')
datatable(mytable,
        extensions = c('Scroller','FixedColumns'),class="compact",escape=FALSE,
        options = list(ordering=FALSE,scrollX = FALSE,deferRender = TRUE,dom = 't',
                       columnDefs = list(list(className = 'dt-center', targets = "_all")),
                       fixedColumns = list(leftColumns = 1))
        ) %>% 
formatPercentage(c(1:ncol(mytable)), 0) %>% 
formatStyle(target = 'row',columns = colnames(mytable),
            backgroundColor = "skyblue",'text-align'="left",'scrollbar-width'= 'thin','-webkit-box-sizing'= 'content-box', 
            '-moz-box-sizing'= 'content-box', 'box-sizing'= 'content-box','table-layout'='fixed') %>% 
formatStyle(c(0), `border-right` = "solid 1px") })

scrollX = TRUE

scrollX = FALSE

อัปเดต: เพิ่ม mytable dataframe อินพุตสำหรับ renderDataTable ตารางของฉัน


person ksh22    schedule 27.07.2020    source แหล่งที่มา
comment
คงเป็นเรื่องยากที่จะช่วยคุณโดยไม่ต้องตอบโต้ โปรดระบุ dataframe ตัวอย่างสำหรับ FA_table()   -  person YBS    schedule 27.07.2020
comment
ขอบคุณ @YBS มีการเพิ่ม mytable ในการอัปเดต   -  person ksh22    schedule 27.07.2020


คำตอบ (1)


ลองสิ่งนี้

output$monthly_summary_FA <- renderDataTable({
    # mytable <- my_data %>% column_to_rownames('Time') %>% as.matrix() %>% t() %>% as.data.frame()
    # colnames(mytable) <- paste0('<span style="color:black;font-family:Arial Narrow, sans-serif;">',colnames(mytable),'</span>')
    colnames(mytable)[c(1:ncol(mytable))] <- paste0('<span style="color:',c("red"),'">',colnames(mytable)[c(1:ncol(mytable))],'</span>')
    rownames(mytable) <- paste0('<span style="color:black;font-family:Arial Narrow, sans-serif;font-weight: bold;">', rownames(mytable),'</span>')
    datatable(mytable,
              extensions = c('Scroller','FixedColumns'),class="compact",escape=FALSE,
              options = list(ordering=FALSE,scrollX = FALSE,deferRender = TRUE,dom = 't',
                             columnDefs = list(list(className = 'dt-center', targets = "_all")),
                             initComplete = JS(
                               "function(settings, json) {",
                               "$(this.api().table().header()).css({'background-color': '#87ceeb', 'color': '#fff'});",
                               "}"),
                             fixedColumns = list(leftColumns = 1))
    ) %>%
      formatPercentage(c(2:ncol(mytable)), 0) %>%
      formatStyle(target = 'row',columns = colnames(mytable),
                  backgroundColor = "skyblue",'text-align'="left",'scrollbar-width'= 'thin','-webkit-box-sizing'= 'content-box',
                  '-moz-box-sizing'= 'content-box', 'box-sizing'= 'content-box','table-layout'='fixed') %>%
      formatStyle(c(0), `border-right` = "solid 1px") 
  })

คุณจะได้รับ

เอาท์พุท

person YBS    schedule 27.07.2020