第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

向 DT 表的父行添加圖標(biāo)

向 DT 表的父行添加圖標(biāo)

精慕HU 2022-11-27 16:32:57
問(wèn)題: 將表情符號(hào)包含到父/子表會(huì)使子表靜音。如何克服這個(gè)困難?增加的問(wèn)題是包含幾乎任何特殊字符都會(huì)導(dǎo)致相同的結(jié)果。我懷疑答案在回調(diào)中,但它擊敗了我的 JS 技能。編碼:library(shiny)library(DT)library(tidyverse)### == script knicked from https://github.com/rstudio/shiny-examples/issues/9shinyApp(    ui = fluidPage(DT::DTOutput("tbl")),    server = function(input, output, session) {        df <- tibble(names=c("Joan", "Michael", "Vincent"), hex =c("&#129409;","&#129409;","&#129409;"), DOB=c("2020-04-05", "2020-04-05","2020-04-05"))            df <- df %>%                as.data.frame() %>%                nest(-names, -hex)                        # add control column            data <- df %>% {bind_cols(data_frame(' ' = rep('&#9658;',nrow(.)))                                                  ,.)}                        # get dynamic info and strings            nested_columns         <- which(sapply(data,class)=="list") %>% setNames(NULL)            not_nested_columns     <- which(!(seq_along(data) %in% c(1,nested_columns)))            not_nested_columns_str <- not_nested_columns %>% paste(collapse="] + '_' + d[") %>% paste0("d[",.,"]")                        # The callback            # turn rows into child rows and remove from parent            callback <- paste0("                    table.column(1).nodes().to$().css({cursor: 'pointer'});                    // Format data object (the nested table) into another table                    var format = function(d) {                      if(d != null){                        var result = ('<table id=\"child_' + ",not_nested_columns_str," + '\">').replace('.','_') + '<thead><tr>'                        for (var col in d[",nested_columns,"]){                          result += '<th>' + col + '</th>'                        }                        result += '</tr></thead></table>'                        return result                      }else{                        return '';                      }                    }
查看完整描述

1 回答

?
慕尼黑的夜晚無(wú)繁華

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超6個(gè)贊

做帶孩子的datatable,還是用我博客上給的方法比較好。目前回調(diào)代碼有一些錯(cuò)誤,我需要更新這篇文章,所以我在這里為你的案例提供完整的代碼:

library(DT)


NestedData <- function(dat, children){

  stopifnot(length(children) == nrow(dat))

  g <- function(d){

    if(is.data.frame(d)){

      purrr::transpose(d)

    }else{

      purrr::transpose(NestedData(d[[1]], children = d$children))

    }

  }

  subdats <- lapply(children, g)

  oplus <- ifelse(lengths(subdats), "&oplus;", "") 

  cbind(" " = oplus, dat, "_details" = I(subdats), 

        stringsAsFactors = FALSE)

}



Dat <- NestedData(

  data.frame(

    names = c("Joan", "Michael", "Vincent"), 

    hex = c("&#129409;","&#129409;","&#129409;"),

    stringsAsFactors = FALSE

  ),

  children = list(

    data.frame(DOB = "2020-04-05", stringsAsFactors = FALSE),

    data.frame(DOB = "2020-04-05", stringsAsFactors = FALSE),

    data.frame(DOB = "2020-04-05", stringsAsFactors = FALSE)

  )

)



## whether to show row names

rowNames = FALSE

colIdx <- as.integer(rowNames)

## the callback

parentRows <- which(Dat[,1] != "")

callback <- JS(

  sprintf("var parentRows = [%s];", toString(parentRows-1)),

  sprintf("var j0 = %d;", colIdx),

  "var nrows = table.rows().count();",

  "for(let i = 0; i < nrows; ++i){",

  "  var $cell = table.cell(i,j0).nodes().to$();",

  "  if(parentRows.indexOf(i) > -1){",

  "    $cell.css({cursor: 'pointer'});",

  "  }else{",

  "    $cell.removeClass('details-control');",

  "  }",

  "}",

  "",

  "// --- make the table header of the nested table --- //",

  "var formatHeader = function(d, childId){",

  "  if(d !== null){",

  "    var html = ", 

  "      '<table class=\"display compact hover\" ' + ",

  "      'style=\"padding-left: 30px;\" id=\"' + childId + ", 

  "      '\"><thead><tr>';",

  "    var data = d[d.length-1] || d._details;",

  "    for(let key in data[0]){",

  "      html += '<th>' + key + '</th>';",

  "    }",

  "    html += '</tr></thead></table>'",

  "    return html;",

  "  } else {",

  "    return '';",

  "  }",

  "};",

  "",

  "// --- row callback to style rows of child tables --- //",

  "var rowCallback = function(row, dat, displayNum, index){",

  "  if($(row).hasClass('odd')){",

  "    $(row).css('background-color', 'papayawhip');",

  "    $(row).hover(function(){",

  "      $(this).css('background-color', '#E6FF99');",

  "    }, function(){",

  "      $(this).css('background-color', 'papayawhip');",

  "    });",

  "  } else {",

  "    $(row).css('background-color', 'lemonchiffon');",

  "    $(row).hover(function(){",

  "      $(this).css('background-color', '#DDFF75');",

  "    }, function(){",

  "      $(this).css('background-color', 'lemonchiffon');",

  "    });",

  "  }",

  "};",

  "",

  "// --- header callback to style header of child tables --- //",

  "var headerCallback = function(thead, data, start, end, display){",

  "  $('th', thead).css({",

  "    'border-top': '3px solid indigo',", 

  "    'color': 'indigo',",

  "    'background-color': '#fadadd'",

  "  });",

  "};",

  "",

  "// --- make the datatable --- //",

  "var formatDatatable = function(d, childId){",

  "  var data = d[d.length-1] || d._details;",

  "  var colNames = Object.keys(data[0]);",

  "  var columns = colNames.map(function(x){",

  "    return {data: x.replace(/\\./g, '\\\\\\.'), title: x};",

  "  });",

  "  var id = 'table#' + childId;",

  "  var options = {",

  "    'dom': 't',",

  "    'data': data,",

  "    'columns': columns,",

  "    'autoWidth': true,",

  "    'deferRender': true,",

  "    'info': false,",

  "    'lengthChange': false,",

  "    'ordering': data.length > 1,",

  "    'order': [],",

  "    'paging': false,",

  "    'scrollX': false,",

  "    'scrollY': false,",

  "    'searching': true,",

  "    'sortClasses': false,",

  "    'rowCallback': rowCallback,",

  "    'headerCallback': headerCallback",

  "  };",

  "  var hasChild = colNames.indexOf('_details') > -1",

  "  if(!hasChild){",

  "    var columnDefs = ",

  "      {'columnDefs': [{targets: '_all', className: 'dt-center'}]};",

  "    var subtable = $(id).DataTable(",

  "      $.extend(options, columnDefs)",

  "    );",

  "  } else {",

  "    var columnDefs = {",

  "      'columnDefs': [",

  "        {targets: -1, visible: false},",

  "        {targets: 0, orderable: false, className: 'details-control'},",

  "        {targets: '_all', className: 'dt-center'}",

  "      ]};",

  "    var subtable = $(id).DataTable(",

  "      $.extend(options, columnDefs)",

  "    ).column(0).nodes().to$().css({cursor: 'pointer'});",

  "  }",

  "};",

  "",

  "// --- display the child table on click --- //",

  "// array to store id's of already created child tables",

  "var children = [];", 

  "table.on('click', 'td.details-control', function(){",

  "  var tbl = $(this).closest('table'),",

  "      tblId = tbl.attr('id'),",

  "      td = $(this),",

  "      row = $(tbl).DataTable().row(td.closest('tr')),",

  "      rowIdx = row.index();",

  "  if(row.child.isShown()){",

  "    row.child.hide();",

  "    td.html('&oplus;');",

  "  } else {",

  "    var childId = tblId + '-child-' + rowIdx;",

  "    if(children.indexOf(childId) === -1){", 

  "      // this child has not been created yet",

  "      children.push(childId);",

  "      row.child(formatHeader(row.data(), childId)).show();",

  "      td.html('&CircleMinus;');",

  "      formatDatatable(row.data(), childId, rowIdx);",

  "    }else{",

  "      // this child has already been created",

  "      row.child(true);",

  "      td.html('&CircleMinus;');",

  "    }",

  "  }",

  "});")


datatable(

  Dat, 

  callback = callback, rownames = rowNames, escape = -c(colIdx+1, 3),

  options = list(

    paging = FALSE,

    searching = FALSE,

    columnDefs = list(

      list(

        visible = FALSE, 

        targets = ncol(Dat)-1+colIdx

      ),

      list(

        orderable = FALSE, 

        className = "details-control", 

        targets = colIdx

      ),

      list(

        className = "dt-center", 

        targets = "_all"

      )

    )

  )

)


查看完整回答
反對(duì) 回復(fù) 2022-11-27
  • 1 回答
  • 0 關(guān)注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)