簡體   English   中英

無法生成函數生成HTML表 <td> 元素第一次

[英]Function generating HTML table does not create <td> elements the first time

我做了一個基於您單擊的元素生成表的函數:

.on("click", function(d, i) {
                        table1title = "Sources for " + d.key + " in Warehouse " + d.values["0"].Warehouse + " :" ;
                        selecteddata = [];

                        columns = ["Country", "Amount(KG)", "Frequency", "Delivery by"];

                        for(i=0; i<d.values.length; i++){
                            selecteddata.push(
                                [
                                    d.values[i].Country,
                                    d.values[i]["Amount (kg)"],
                                    d.values[i].Frequency,
                                    d.values[i]["Delivered by"]
                                ]
                            )
                        }

                        genTable1();

                        })

 function genTable1(){

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

                    table1header.text(table1title);

                    var rowsTable1 = 
 bodyTable1.selectAll("tr").data(selecteddata);

                    rowsTable1.exit().remove();

                    rowsTable1.enter().append("tr");

                    var cellsTable1 = rowsTable1.selectAll("td")
                        .data(function(selecteddata){
                            console.log(selecteddata);
                            return d3.values(selecteddata)})

                    cellsTable1.enter().append("td").text(function(d){return d;})

                    cellsTable1.exit().remove();
                }

因此,當我第一次單擊這樣的元素時,該表的生成沒有問題。 會生成正確數量的<tr>元素,但不會生成<td>元素。 您會看到一個console.log ,我想在其中生成<td>元素,這是我第一次單擊某些內容時沒有任何輸出到控制台。

如何獲得第一輪要生成的元素?

另外,另一個錯誤是第一行永遠不會更新為新值,它停留在給定的第一個值上……

好吧,我自己弄清楚了(是的)。 我使用以下選擇層次結構對其進行了修復:

var cellsTable1 = bodyTable1.selectAll("tr").selectAll("td")
                    .data(function(selecteddata){
                        console.log(selecteddata);
                        return d3.values(selecteddata)})

我一直以為,僅引用對象會繼承先前聲明的層次結構,但我想不會。

我也能夠解決的另一個錯誤。 我在附加時執行了.text(),所以只有新添加的單元格才獲取新文本。 這樣解決了:

cellsTable1.enter().append("td")

bodyTable1.selectAll("tr").selectAll("td").text(function(d){return d;})

感覺有點像stackoverflow上的孤獨互動,但至少我可以為自己感到驕傲哈哈

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM