Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript d3表格-向值添加特定超链接_Javascript_D3.js - Fatal编程技术网

Javascript d3表格-向值添加特定超链接

Javascript d3表格-向值添加特定超链接,javascript,d3.js,Javascript,D3.js,我有一个d3生成的表,并在第一列中添加了一个指向值(名称)的超链接。如何根据每个学生的StudentID使链接特定于每个学生。换句话说,我希望第一个学生的链接是http://mysite.php?studentid=129508,第二个学生的链接是http://mysite.php?studentid=129562,而不是每个学生的链接是http://mysite.php var data = [{"StudentID":"129508","FirstName":"Madison", "Math

我有一个d3生成的表,并在第一列中添加了一个指向值(名称)的超链接。如何根据每个学生的StudentID使链接特定于每个学生。换句话说,我希望第一个学生的链接是http://mysite.php?studentid=129508,第二个学生的链接是http://mysite.php?studentid=129562,而不是每个学生的链接是http://mysite.php

var data = [{"StudentID":"129508","FirstName":"Madison", "Math":"69.6","English":"64.1","History":"63.2"},          {"StudentID":"129562","FirstName":"Virginia","Math":"52.7","English":"64.1","History":"82.3"},          {"StudentID":"129770","FirstName":"Lucinda","Math":"93.4","English":"87.9","History":"84.6"},           {"StudentID":"129427","FirstName":"Ella","Math":"62.3","English":"64.1","History":"60.7"},          {"StudentID":"129203","FirstName":"Nicholas","Math":"74.9","English":"66.2","History":"73.2"}];
function tabulate(data, columns) {
    var table = d3.select("body").append("table")
        .attr("style", "margin-left: 100px")
        .style("border-collapse", "collapse")
        .style("border", "2px black solid"), 
    thead = table.append("thead"),
    tbody = table.append("tbody");

thead.append("tr")
    .selectAll("th")
    .data(columns)
    .enter()
    .append("th")
        .text(function(column) { return column; });

var rows = tbody.selectAll("tr")
    .data(data)
    .enter()
    .append("tr");

var cells = rows.selectAll("td")
    .data(function(row) {
        return columns.map(function(column) {
            return {column: column, value: row[column]};
        });
    })
    .enter()
    .append("td")
    .attr("style", "font-family: Courier") 
    .html(function(d, i) {  
                if (i === 0) { return "<a href=\"http://mysite.php\">" + d.value + "</a>";
                } else {return d.value;
                }
        });
return table;
}
var resultsTable = tabulate(data, ["FirstName", "Math", "English", "History"]);
var data=[{“StudentID”:“129508”,“FirstName”:“Madison”,“Math”:“69.6”,“English”:“64.1”,“History”:“63.2”},{“StudentID”:“129562”,“FirstName”:“Virginia”,“Math”:“52.7”,“English”:“64.1”,“History”:“82.3”},{“StudentID”:“129770”,“FirstName”:“Lucinda”,“Math”:“93.4”,“English”:“87.9”,“History”:“84.6”},{“StudentID”:“129427”,“名字”:“埃拉”,“数学”:“62.3”,“英语”:“64.1”,“历史”:“60.7”},{“学生ID”:“129203”,“名字”:“尼古拉斯”,“数学”:“74.9”,“英语”:“66.2”,“历史”:“73.2”});
功能表格(数据、列){
var table=d3.选择(“主体”).追加(“表格”)
.attr(“样式”,“左边距:100px”)
.style(“边框塌陷”、“塌陷”)
.样式(“边框”,“2件黑色实心”),
thead=table.append(“thead”),
tbody=table.append(“tbody”);
附件(“tr”)
.selectAll(“th”)
.数据(列)
.输入()
.附加(“th”)
.text(函数(列){返回列;});
变量行=tbody.selectAll(“tr”)
.数据(数据)
.输入()
.附加(“tr”);
变量单元格=行。选择全部(“td”)
.数据(功能(行){
返回columns.map(函数(列){
返回{column:column,value:row[column]};
});
})
.输入()
.附加(“td”)
.attr(“样式”、“字体系列:Courier”)
.html(函数(d,i){
如果(i==0){返回“”;
}else{返回d.value;
}
});
返回表;
}
var resultsTable=表格(数据,[“名字”,“数学”,“英语”,“历史]);
需要两个更改

首先,在生成数据时,还需要存储id:

var cells = rows.selectAll("td")
        .data(function (row) {
        return columns.map(function (column) {
            console.log(row)
            return {
                column: column,
                value: row[column],
                id: row.StudentID //storing the id
            };
        });
    })
第二个链接是url中的id

if (i === 0) {
            console.log(d)
            return "<a href=\"http://mysite.php?studentid="+d.id+"\">" + d.value + "</a>";
        } else {
            return d.value;
        }
    });
if(i==0){
控制台日志(d)
返回“”;
}否则{
返回d值;
}
});
工作代码