Javascript 向D3生成的表添加链接

Javascript 向D3生成的表添加链接,javascript,html,d3.js,Javascript,Html,D3.js,我想添加一个链接到我使用d3从表格数据创建的表。请快速查看我的代码片段: var数据=[ {'Engine':'Google','Founded':'1998','Monthly Visitors':4840295000','Site':'www.Google.com'}, {'Engine':'Baidu','Founded':'2000','Monthly Visitors':1471079000,'Site':'www.Baidu.com'}, {'Engine':'Yahoo','Fo

我想添加一个链接到我使用d3从表格数据创建的表。请快速查看我的代码片段:

var数据=[
{'Engine':'Google','Founded':'1998','Monthly Visitors':4840295000','Site':'www.Google.com'},
{'Engine':'Baidu','Founded':'2000','Monthly Visitors':1471079000,'Site':'www.Baidu.com'},
{'Engine':'Yahoo','Founded':'1995','Monthly Visitors':1038375000','Site':'www.Yahoo.com'},
{'Engine':'Bing','Founded':'2009','Monthly Visitors':203482000','Site':'www.Bing.com'},
{'Engine':'AOL','Founded':'1991','Monthly Visitors':39961000','Site':'www.aolsearch.com'}
];
var列=['Engine'、'Founded'、'Monthly Visitors'、'Site'];
var table=d3。选择('body')。追加('table'),
thead=table.append('thead'),
tbody=table.append('tbody');
thead.append('tr')
.selectAll('th')
.数据(列)
.输入()
.append('th')
.text(函数(列){返回列;});
var rows=tbody.selectAll('tr')
.数据(数据)
.输入()
.append('tr');
变量单元格=行。选择全部('td')
.数据(功能(行){
返回columns.map(函数(列){
返回{column:column,value:row[column]};
});
})
.输入()
.append('td'))
.文本(功能(d){
如果(d.column==='Site'){
//console.log('现在做什么?')
}
返回d值;
});

您只需将
.text
更改为
.html

并添加
'
或者像这样在数据的return语句中添加条件

if (d.column === 'Site') {
  return "<a href="+ d.value +">" + d.value + "</a>"
}
if(d.column=='Site'){
返回“”
}
代码如下:

var数据=[
{'Engine':'Google','Founded':'1998','Monthly Visitors':4840295000','Site':'www.Google.com'},
{'Engine':'Baidu','Founded':'2000','Monthly Visitors':1471079000,'Site':'www.Baidu.com'},
{'Engine':'Yahoo','Founded':'1995','Monthly Visitors':1038375000','Site':'www.Yahoo.com'},
{'Engine':'Bing','Founded':'2009','Monthly Visitors':203482000','Site':'www.Bing.com'},
{'Engine':'AOL','Founded':'1991','Monthly Visitors':39961000','Site':'www.aolsearch.com'}
];
var列=['Engine'、'Founded'、'Monthly Visitors'、'Site'];
var table=d3。选择('body')。追加('table'),
thead=table.append('thead'),
tbody=table.append('tbody');
thead.append('tr')
.selectAll('th')
.数据(列)
.输入()
.append('th')
.text(函数(列){返回列;});
var rows=tbody.selectAll('tr')
.数据(数据)
.输入()
.append('tr');
变量单元格=行。选择全部('td')
.数据(功能(行){
返回columns.map(函数(列){
返回{column:column,value:row[column]};
});
})
.输入()
.append('td'))
.html(函数(d){
如果(d.column==='Site'){
返回“”
}
返回d值;
});

您可以将以下内容添加到

.style("cursor", "pointer")
.on("click", function(d) { window.open(d.Site, "_blank"); })
紧接着:

.append('tr')

很酷的想法,从来没有想过从数据方面来处理它。@ArashHowaida或者,如果您知道您的数据是链接,您可以在html返回语句中添加一个条件,请参阅编辑!
.append('tr')