简体   繁体   English

如何将标签更改为<input>使用按钮在 react.js 中激活标签?

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

In the given code below, there is td tag which includes input tag inside it.在下面给定的代码中,有一个td标签,其中包含input标签。 I have disabled it initially so that the user can't change the data.我最初已将其禁用,以便用户无法更改数据。 Because of this if the data inside it is more than 2 lines or something then all of it will not be visible.因此,如果其中的数据超过 2 行或其他内容,那么所有数据都将不可见。

1) I want to make data fit as per its length, like 'textarea'. 1)我想让数据适合它的长度,比如'textarea'。 2) In the end, there is an "Edit" button, when I click on that button all the td tags should become input tags. 2)最后,有一个“编辑”按钮,当我点击那个按钮时,所有的td标签都应该变成输入标签。 Therefore I should be able to edit the data inside it.因此我应该能够编辑其中的数据。 Please can anyone help me with this?请问有人可以帮我吗? I want to achieve this using react.js Thank you in advance.我想用 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;

You need to do few things:你需要做几件事:

  1. turn form into edit mode needs a react state将表单转换为编辑模式需要一个反应 state
const [isEditing, setEditing] = useState(false);
  1. you need to 'activate' edit mode in edit function with您需要在编辑 function 中“激活”编辑模式
setEditing(true);
  1. you need to change rendering of <td> vs <input> based on isEditing variable您需要根据isEditing变量更改<td><input>的渲染
{isEditing ? <td>{item.values}</td> : <td><input value={item.values} /></td>}
  1. you need to add onChange prop for input to change edited data您需要为输入添加onChange道具以更改编辑的数据
<input value={item.values} onChange={(e) => item.values = e.target.value}  />

You have invalid HTML structure - missing <table> and mixing <div> with <tr> element on same level您有无效的 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> - How to store and pass the user input value using <button> tag in React.js? React.js onChange<input> 标签挂起 - React.js onChange <input> tag hangs 如何更改元素的父元素的样式 <NavLink> 在React.js中标记? - How to change styles of a parent element of a <NavLink> tag in React.js? 如何在React JS中增加td标签中输入字段的宽度 - how to increase the width of the input field in td tag in react JS 如何使用 html video 标签在 react.js 中播放视频? - How to play video in react.js using html video tag? 在中使用.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 如何使用 React.js 访问标签样式 - How to access to tag style with React.js 从 HTML 插入按钮<script> tag in React.js - Inserting button from HTML <script> tag in React.js 如何在td标签中插入输入标签? - How to insert input tag in to td tag? 使用表单标签时React.js&#39;意外令牌&#39;&lt; - React.js 'Unexpected token' < when using form tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM