d3 adding data attribute conditionally

后端 未结 5 1383
悲&欢浪女
悲&欢浪女 2021-02-01 14:01

I\'m creating a table with d3 to be used by the FooTable jquery plugin and this requires having some data- attributes in the header row. But not all columns have all the data a

5条回答
  •  伪装坚强ぢ
    2021-02-01 14:51

    You can use the .filter() function to only operate on the subset of the selection that you need to set attributes for, e.g.

    var th = d3.select(selection).select("thead").selectAll("th")
            .data(colspec)
            .enter().append("th")
            .text(function(d) { return d["data-name"]; });
    th.filter(function(d) { return ("data-class" in d); })
            .attr("data-class", function(d) {
                return d["data-class"];
            });
    

提交回复
热议问题