Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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创建HTML表时,请使用行类而不是单元格的特定数据_Javascript_D3.js_Html Table - Fatal编程技术网

Javascript 使用D3创建HTML表时,请使用行类而不是单元格的特定数据

Javascript 使用D3创建HTML表时,请使用行类而不是单元格的特定数据,javascript,d3.js,html-table,Javascript,D3.js,Html Table,I'v从2级数组创建了一个具有D3的表: var columns = ["Id", "Book Title", "Author"]; var books = [ ["1", "The Caves of Steel", "Isaac Asimov"], ["2", "The Robots of Dawn", "Isaac Asimov"], ["3", "Brave New World", "Aldous Huxley"], ["4", "1984", "Geor

I'v从2级数组创建了一个具有D3的表:

var columns = ["Id", "Book  Title", "Author"];
var books = [
    ["1", "The Caves of Steel", "Isaac Asimov"],
    ["2", "The Robots of Dawn", "Isaac Asimov"],
    ["3", "Brave New World", "Aldous Huxley"],
    ["4", "1984", "George Orwell"],
];

var table = d3.select("body").append("table").attr("id", "bookTable");
var thead = table.append("thead");
var tbody = table.append("tbody");

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

var rows = tbody.selectAll("tr")
    .data(books)
    .enter()
    .append("tr").attr("data-id", function( d ){  return d[0]; });

var cells = rows.selectAll("td")
    .data(function ( d ) { return d; })
    .enter()
    .append("td")
    .text(function( d ) { return d; });
在本例中,数组是静态定义的,但在我的文件中,它是从数据库查询构建的。我需要包括行的id,因为用户可以删除任何行,但单击稍后使用jquery添加到每行的按钮,id是我如何从数据库中标识要删除的行,但我不希望它在表中显示为列,我希望将其作为表行上的“数据id”属性应用

现在,将在表中的表中创建id的列,并创建tr属性,这就是为什么上面的列数组中包含id,但这需要删除


我如何告诉d3在追加td时跳过id,而是将此数据用于行上的数据id属性?

最简单的方法可能是将数据设置为对象数组,而不仅仅是数组,例如

{id: 1, book: ["The Caves of Steel", "Isaac Asimov"], ...}
然后,您可以使用
id
属性设置每行的
数据id
,并为
书籍添加
td
s。下面是我用来让您的示例正常工作的代码:

// Add the header
thead.append("tr")
    .selectAll("th")
    .data(columns.slice(1, columns.length)) // skip the first column (IDs)
    .enter()
    .append("th")
        .text(function ( d ) { return d; })

// Reformat the nested Arrays into an Array of Objects,
// then set the 'data-id' of each row
var rows = tbody.selectAll("tr")
    .data(books.map(function(d){ return {id: d[0], book: d.slice(1, d.length)}; }))
    .enter()
    .append("tr").attr("data-id", function( d ){  return d.id; });

// Append the author and title for each book
var cells = rows.selectAll("td")
    .data(function ( d ) { return d.book; })
    .enter()
    .append("td")
    .text(function( d ) { return d; });

使用此代码时出现以下错误:“UncaughtTypeError:对象#没有方法“slice”。这是捕获第二次使用切片,在行下。@Alex:真奇怪,使用相同的代码我没有收到错误。你能不能把
console.log(d)在该行的
返回…
语句之前,告诉我它说了什么?当我这么做的时候,我明白了。哦,我知道发生了什么。我将数据更改为一个对象数组,因为我认为这就是你在回答的第一部分中的意思。当我这么做的时候,我得到了。当我把书本变量改回数组时,它起了作用,我得到了。@Alex:好的,我现在明白了,很抱歉弄糊涂了。如果您更改了数据结构,使其成为一个对象数组,那么您应该能够只使用
.data(book)
。这可能更容易阅读,但我不确定您的数据库是否可以提供这种格式的数据。否则它会像预期的那样工作吗?会的!我只是不知道您已经将数据格式化为d3代码中的一个对象数组,并假设我需要先这样做。这就是我复制和粘贴的结果。。。谢谢你的帮助!