簡體   English   中英

如何將標簽更改為<input>使用按鈕在 react.js 中激活標簽?

[英]How to change <td> tag into <input> active tag in react.js using button?

在下面給定的代碼中,有一個td標簽,其中包含input標簽。 我最初已將其禁用,以便用戶無法更改數據。 因此,如果其中的數據超過 2 行或其他內容,那么所有數據都將不可見。

1)我想讓數據適合它的長度,比如'textarea'。 2)最后,有一個“編輯”按鈕,當我點擊那個按鈕時,所有的td標簽都應該變成輸入標簽。 因此我應該能夠編輯其中的數據。 請問有人可以幫我嗎? 我想用 react.js 來實現這個提前謝謝你。

import React, {useEffect, useState} from 'react';
import '../src/tabledata.css';

  function TableData() {
    const [data, getData] = useState([])

useEffect(() => {
  fetchData()
},[])

const fetchData = () => {
  fetch("/api")
  .then((res) =>
  res.json())

  .then((response) => {
    console.log(response);
    getData(response);
  })
}

function edit() {


  
}

return (
  // whole body
  <div className='body'>
    <h1 className='heading'>GCP DOCUMENT AI DATA</h1> {/* heading */}

    <div> {/* container for table */}
    <div className='tbl'> {/* div for table inside container */}
    <tbody>
      <tr> {/* headings in table */}
        <th>Keys</th>
        <th>Values</th>
      </tr>

      {data.map((item,i) => (
        <tr key={i}> {/* 1st column keys and 2nd column values */}
          <td>{item.keys}</td>
          <td><input className='tdValueInput' type="text" value={item.values} disabled/></td>
        </tr>
      ))}
      <div> {/* div for buttons */}
        <input type="submit" className='bttn-edit' value="Edit" onClick={edit}/>
        <input type="submit" className='bttn-approve' value="Approve"/>
      </div>
    </tbody>
    </div>
  </div>
  </div>
);
  }

export default TableData;

你需要做幾件事:

  1. 將表單轉換為編輯模式需要一個反應 state
const [isEditing, setEditing] = useState(false);
  1. 您需要在編輯 function 中“激活”編輯模式
setEditing(true);
  1. 您需要根據isEditing變量更改<td><input>的渲染
{isEditing ? <td>{item.values}</td> : <td><input value={item.values} /></td>}
  1. 您需要為輸入添加onChange道具以更改編輯的數據
<input value={item.values} onChange={(e) => item.values = e.target.value}  />

您有無效的 HTML 結構 - 缺少<table>並將<div><tr>元素混合在同一級別

在中使用.map<div> 標簽-react.js</div><div id="text_translate"><p> 我真的很感激任何幫助。 所以該項目是一個反應項目。</p><p> 這是我獲取<a href="https://i.stack.imgur.com/WelCP.png" rel="nofollow noreferrer">的內容:獲取請求</a></p><p>這是我從服務器響應的內容: <a href="https://i.stack.imgur.com/e2QG3.png" rel="nofollow noreferrer">Response from server</a></p><p> 我試了一下 Postman,看看我收到的是什么類型的 json。 這是它返回的內容: <a href="https://i.stack.imgur.com/yg6IE.png" rel="nofollow noreferrer">Postman 測試</a></p><p>我遇到的問題是我無法 map 收到的數據。 一旦輸入鏈接,我只想在首頁上以一種很好的方式顯示接收到的數據。</p><p> 是否可以將 map 放在 div 標簽內? 或者還有另一種方法嗎?</p><p> <em><strong>請注意該項目是作為功能組件編寫的</strong></em></p></div>

[英]Using .map in <div> tag - react.js

暫無
暫無

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

相關問題 如何<button>在React.js中</button>使用<button>標簽</button>存儲和傳遞用戶輸入值<button>?</button> React.js onChange<input> 標簽掛起 如何更改元素的父元素的樣式 <NavLink> 在React.js中標記? 如何在React JS中增加td標簽中輸入字段的寬度 如何使用 html video 標簽在 react.js 中播放視頻? 在中使用.map<div> 標簽-react.js</div><div id="text_translate"><p> 我真的很感激任何幫助。 所以該項目是一個反應項目。</p><p> 這是我獲取<a href="https://i.stack.imgur.com/WelCP.png" rel="nofollow noreferrer">的內容:獲取請求</a></p><p>這是我從服務器響應的內容: <a href="https://i.stack.imgur.com/e2QG3.png" rel="nofollow noreferrer">Response from server</a></p><p> 我試了一下 Postman,看看我收到的是什么類型的 json。 這是它返回的內容: <a href="https://i.stack.imgur.com/yg6IE.png" rel="nofollow noreferrer">Postman 測試</a></p><p>我遇到的問題是我無法 map 收到的數據。 一旦輸入鏈接,我只想在首頁上以一種很好的方式顯示接收到的數據。</p><p> 是否可以將 map 放在 div 標簽內? 或者還有另一種方法嗎?</p><p> <em><strong>請注意該項目是作為功能組件編寫的</strong></em></p></div> 如何使用 React.js 訪問標簽樣式 從 HTML 插入按鈕<script> tag in React.js 如何在td標簽中插入輸入標簽? 使用表單標簽時React.js&#39;意外令牌&#39;&lt;
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM