【问题标题】:Add edit fields on table row on click in reactjs without using react-table library在不使用 react-table 库的情况下单击 reactjs 在表行上添加编辑字段
【发布时间】:2018-10-16 21:55:36
【问题描述】:

如何更改在 JSX 表中单击的特定行以更改为输入可编辑行而不使用 react-table 等任何库?

<table className="table-data">
 <tbody>
 <tr>
    <th>Name</th>
    <th>Age</th>
    <th>Class</th>
    <th>Section</th>
 </tr>   {this.state.students.map((item,key) => {return


<Fragment key={key}>
          <tr key={key} onClick={()=> 
    {this.setState({clientIsEditing:true},this.edit(key,item))}}> 
    <td>{item[1]}</td>    
    <td>{item[2]}</td>    
    <td>{item[3]}</td>  
    <td>{item[4]}</td> 
    </tr>  
    </Fragment>
    <tbody>
    </table>})}

以上是显示表格的代码。 this.state.students 有来自 db 的学生详细信息。我希望在单击时将特定表行更改为一行输入字段。请帮忙。我是反应世界的新手。

【问题讨论】:

    标签: reactjs html-table edit jsx


    【解决方案1】:

    如果我正确理解您的问题,您似乎需要跟踪其他状态以确定“学生”行当前是否处于编辑模式。也许您可以通过执行以下操作来实现:

    <table className="table-data">
     <tbody>
     <tr>
        <th>Name</th>
        <th>Age</th>
        <th>Class</th>
        <th>Section</th>
     </tr>   
    
     {  this.state.students.map((item,key) => {
    
        const editField = (value, index) => {
    
          // Clone students data before mutation
          const students = this.state.students.map(item => ({ ...item }))
    
          // Update field by index of current student
          students[key][index] = value
    
          // Trigger re-render
          this.setState({ students })
        }
    
       return (
        <tr key={key} className={ item.editing ? 'editing' : '' } onClick={()=> {
    
          // Clone students data before mutation
          const students = this.state.students.map(i => ({ ...i, editing : item.editing && i===item }))
    
          // Toggle editing flag of this current student (ie table row)
          students[key].editing = true; 
    
          // Trigger re-render
          this.setState({
            clientIsEditing:true, // This might not be needed ?
            students
          })
    }
        }> 
        <td>{ item.editing ? <input value={item[1]} onChange={ e => editField(e.target.value, 1) } /> : <span>{item[1]}</span> }</td>    
        <td>{ item.editing ? <input value={item[2]} onChange={ e => editField(e.target.value, 2) } /> : <span>{item[2]}</span> }</td>    
        <td>{ item.editing ? <input value={item[3]} onChange={ e => editField(e.target.value, 3) } /> : <span>{item[3]}</span> }</td>  
        <td>{ item.editing ? <input value={item[4]} onChange={ e => editField(e.target.value, 4) } /> : <span>{item[4]}</span> }</td> 
        </tr>  )
      })
      }
    
      </tbody>
      </table>
    

    Here is a working jsFiddle as well - 希望对您有所帮助!

    【讨论】:

    • 上述代码出现以下错误。TypeError: _this3.edit is not a function'students' is not defined no-undef(在 onClick 事件中)
    • 传播运算符抛出错误,Syntax error: Unexpected token, expected , 对不起,我很傻,我对 React 编程完全陌生..
    • 我会试试的。同时,你能告诉我item.editing在编辑什么吗?道具从哪里来
    • 对不起@dacre,它在 onclick 函数中抛出语法错误。
    • 当然,让我知道它是怎么回事 - 是的,editing 最初是未定义的,但是在用户单击表格行之后,editing 字段被添加到学生并定义为 @987654329 @/false 取决于切换说明。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2019-05-31
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 2020-03-13
    • 2021-11-25
    相关资源
    最近更新 更多