如何过滤r DT中包含超链接的列

时间:2017-08-09 16:40:58

标签: r dt

我有一个专栏" url"其中包含超链接。链接文本是整数。如何使列过滤在DataTables中工作,以便使用链接文本而不是超链接的html执行过滤?以下是一个例子:

static async sendEmail (from, to, subject, text) {
        return new Promise((resolve, reject) => {
            let message = {
                from,
                to,
                subject,
                text
            };
            AMMailing.transporter.sendMail(message, function (err, response) {
                if (err) {
                    reject(err);
                } else {
                    resolve(response);
                }
            });
        });
    }

在上面的数据表中,我想根据url列中的值1和2过滤行。这是目前发生的事情 image

2 个答案:

答案 0 :(得分:1)

library(DT)

render <- c(
  "function(data, type, row){",
  "  if(type === 'display'){",
  "    var a = '<a href=\"' + row[3] + '\">' + data + '</a>';",
  "    return a;",
  "  } else {",
  "    return data;",
  "  }",
  "}"
)

df <- data.frame(
  Val = c("A", "B"), 
  url = c("google", "stackoverflow"),
  href = c("https://www.google.com", "https://www.stackoverflow.com")
)
datatable(df, escape = FALSE, filter = list(position = "top"), 
          options = list(
            columnDefs = list(
              list(targets = 2, render = JS(render)),
              list(targets = 3, visible = FALSE)
            )
          )
)

enter image description here

答案 1 :(得分:0)

你需要url文本是整数吗?如果没有,快速而肮脏的方法是重建html节点以使url的字符串可见。

df <- data.frame(Val = c(1,2), url = c("<a href='https://www.google.com'>1</a>", "<a href='https://www.bbc.com'>2</a>")      
df$string_url <-paste("<a href ='", (str_match(df$url, "'(.*?)'")[,2]), "'>", (str_match(df$url, "'(.*?)'")[,2]), "</a>", sep = "" )    
datatable(df[c("Val","string_url")], escape = FALSE, filter = list(position = "top"))