无法在React中向表格添加行。

huangapple go评论56阅读模式
英文:

Can't add rows to table in react

问题

I have a react component. In a useEffect I'm trying to create a new row and add the row to the table. I tried using insertRow, but that didn't work either. The element does exist and I am able to add a mousedown listener to it.

const createRow = () => {
  const row = document.createElement("tr");
  for (let j = 0; j < 3; j++) {
    const td = document.createElement("td");
    row.appendChild(td);
  }
  return row;
};

useEffect(() => {
  if (id !== "-1") {
    const element = document.getElementById(id);
    element?.addEventListener("mousedown", () => {
      console.log("here");
    });

    const row = createRow();
    element?.append(row); // also tried appendChild
    // yes there should be a removeEventListener, but this is just to debug
  }
}, [id]);

(Note: The code portion is provided in both English and Chinese, as requested.)

英文:

I have a react component. In a useEffect I'm trying to create a new row and add the row to the table. I tried using insertRow, but that didn't work either. The element does exist and I am able to add a mousedown listener to it.

<!-- language: lang-js -->

const createRow = () =&gt; {
  const row = document.createElement(&quot;tr&quot;);
  for (let j = 0; j &lt; 3; j++) {
    const td = document.createElement(&quot;td&quot;);
    row.appendChild(td);
  }
  return row;
};

<!-- end snippet -->

<!-- language: lang-js -->

useEffect(() =&gt; {
  if (id !== &quot;-1&quot;) {
    const element = document.getElementById(id);
    element?.addEventListener(&quot;mousedown&quot;, () =&gt; {
      console.log(&quot;here&quot;);
    });

    const row = createRow();
    element?.append(row); // also tried appendChild
   // yes there should be a removeEventListener, but this is just to debug
  }
}, [id]);

<!-- end snippet -->

答案1

得分: 2

Your createRow 函数正常工作,问题在于你创建了三个 td 并将它们附加到创建的 tr,但这些 td 是空的,所以函数返回如下内容:

&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;

这是一个具有三个空表格数据的表格行,这就是为什么无法观察到它的原因,所以根据相同的逻辑,你必须向你的 td 添加文本:

const createRow = () => {
    const row = document.createElement("tr");
    for (let j = 0; j < 3; j++) {
      const td = document.createElement("td");
      // 添加以下两行
      const content = document.createTextNode("Test");
      td.appendChild(content);
      row.appendChild(td);
    }
    return row;
};

这将返回:

&lt;tr&gt;
  &lt;td&gt;Test&lt;/td&gt;
  &lt;td&gt;Test&lt;/td&gt;
  &lt;td&gt;Test&lt;/td&gt;
&lt;/tr&gt;

注意: 这是纯JavaScript,如果你正在使用React,建议使用refs来操作DOM元素,而不是通过ID获取它们。

英文:

Your createRow function is working fine the problem is that you are creating three td and appending them to the created tr but those td are empty so the function is returning this :

&lt;tr&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
  &lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;

It is a table row with three empty table data, that's why it can't be observed, so with the same logic you have to append text to your td:

const createRow = () =&gt; {
    const row = document.createElement(&quot;tr&quot;);
    for (let j = 0; j &lt; 3; j++) {
      const td = document.createElement(&quot;td&quot;);
       // add these two lines
      const content = document.createTextNode(&quot;Test&quot;);
      td.appendChild(content);
      row.appendChild(td);
    }
    return row;
  };

This will retun:

&lt;tr&gt;
  &lt;td&gt;Test&lt;/td&gt;
  &lt;td&gt;Test&lt;/td&gt;
  &lt;td&gt;Test&lt;/td&gt;
&lt;/tr&gt;

Note: This is vanilla Javascript, if you are using react, it is recommanded to use refs to manipulate DOM elements instead of getting them by id

huangapple
  • 本文由 发表于 2023年5月30日 08:12:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定