列的条件格式化

列的条件格式化

问题描述:

我需要帮助DT :: datatable的条件格式化。我想在下面的例子中突出显示几个名字,用斜体字打印。需要突出显示的名称是向量 name.highlight

I need some help with conditional formatting for DT::datatable. I would like to highlight a couple of names in the following example by printing them in italics. The names which need to be highlighted are in a vector name.highlight <- c("ABC","JKL")

require(DT)

mydf <- data.frame(name=c("ABC","DEF","GHI","JKL","MNO","PQR"), value=1:6)
DT::datatable(mydf)

根据我所看到的此处 here ,好像我需要使用render。我不知道如何编写JS代码,或者我可以如何传递所有需要突出显示的字符串的向量/容器。

Based on what I see here and here, seems like I need to use render. I have no idea how to write the JS code or how I can pass in a vector/container with all the strings which need to be highlighted.

datatable(mydf, options = list(columnDefs = list(list(
targets = 0, render = JS("function(data, type, full, meta) {", ..., "}")
))))

谢谢。

datatable(mydf, options = list(columnDefs = list(list(
    targets = 0, render = JS(
        "function(data, type, full, meta) {",
            "italic_words=['ABC','JKL']",
            "return type === 'display' && italic_words.indexOf(data) != -1 ?",
            "'<i>'+data+'</i>' : data;",
        "}")
))))

我在javascript函数中定义了italic_words变量。该变量包含所有要使用的斜体字的数组。然后我使用indexOf()javascript函数。如果名称不在变量italic_words中,则此函数将返回-1,名称将不会被斜体显示。

I defined the italic_words variable in the javascript function. The variable contains an array of all the words you want in italic. Then I used the indexOf() javascript function. If the name isn't in the variable italic_words, this function will return -1, and the name will not be italicized.