2016-11-11 28 views
2

方案:我正在從.csv文件讀取有關國家/地區的數據。我能夠根據一列對這些數據進行分類,並根據得分過濾掉前30個國家。D3:如何添加額外的列到不在csv列中的表格

我正在從.csv文件標題獲取列名。另外,我想添加不在.csv文件中的「Rank」列。

我該怎麼做?

下面是我的生成一覽表代碼:

var table = d3.select("body").append("table") 
        .attr("style", "margin-left: 50px") 
        .attr("class", "table table-hover table-sm") 
        .attr("id", "tab_id"), 
       thead = table.append("thead") 
        .attr("class","tab-head"), 
       tbody = table.append("tbody"); 

       // Append the header row 
       thead.append("tr") 
        .selectAll("th") 
        .data(columns) 
        .enter() 
        .append("th") 
        .text(function(column) { return column; }); 

       // Create a row for each object in the data 
       var rows = tbody.selectAll("tr") 
        .data(top_nations) 
        .enter() 
        .append("tr"); 

       // create a cell in each row for each column 
       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") // sets the font style 
        .html(function(d) { return d.value; }); 

我可以與排名1-30這裏增加一個額外的列?或者當我正在閱讀數據並進行排序並獲得前30名記錄時,我需要做些什麼?

希望有任何幫助。

回答

2

嘗試

$('#table').find('tr').each(function(){ 
     $(this).find('td').eq(x).after('<td>&nbsp;</td>'); 
    }); 

x是您要創建後

+1

此方法不適合我的工作列的數量。我在瀏覽器控制檯上得到一個錯誤:Uncaught ReferenceError:$未定義。你能否提出可能的問題? –

+0

檢查在使用$()之前是否正確引用jQuery –

相關問題