javascript - 单击时删除项目并将这些项目移动到列表底部

标签 javascript reactjs sorting react-bootstrap strikethrough

我正在尝试编写一个代码,当用户单击列表中的某个项目时,该列表上的项目应该被删除,并将该项目带到列表的底部。

ItemList.js

 <ul className="list-group my-4">
        <h2 className="text-uppercase text-center custom-heading">todo list</h2>

        <div className="card card-body my-4 custom-card-bg">
          <pre>
            {items
              .slice(0)
              .reverse()
              .map(index => {
                return <ToDoItem key={index.id} name={index.name} />;
              })}
          </pre>
        </div>
      </ul>

上面给出的代码是如何将数据添加到列表中

TodoItem.js

 <li className="list-group-item text-capitalize d-flex justify-content-between my-1 custom-input">
        <h6>{name}</h6>
        <div className="todo">
          <span className="mx-2 text-success">
            <i className="fas fa-pen" />
          </span>
          <span className="mx-2 text-danger">
            <i className="fas fa-trash" />
          </span>
        </div>
      </li>

上面的代码是item的样式

App.js

 class App extends Component {
      constructor(props) {
        super(props);
        this.state = {
          items: [],
          currentItem: {
            name: "",
            id: ""
          },
          editItem: false
        };
      }

    addItems = e => {
        e.preventDefault();
        const newItems = this.state.currentItem;
        if (newItems.name !== "") {
          const itemNames = [
            { ...newItems, isStriked: false },
            ...this.state.items
          ];
          this.setState({
            items: itemNames,
            currentItem: {
              name: "",
              id: ""
            },
            editItem: false
          });
        }
      };

      completedItem = id => {
        const complete = this.state.items.find(name => name.id === id);

        if (complete.isStriked === false)
          this.setState({
            items: [
              ...this.state.items.filter(name => name.id !== id),
              { ...complete, isStriked: true }
            ]
          });
      };

      render() {
        return (
          <div>
            <div
              className="container-fluid d-flex vh-100 flex-column"
              style={{ height: "100vh" }}
            >
              <div className="row">
                <div className="col-10 mx-auto col-md-8 mt-4 mb-2 custom-row-bg">
                  <h2 className="text-center text-uppercase mt-3 custom-heading">
                    input todo
                  </h2>
                  <InputItems
                    name={this.state.currentItem.name}
                    handleInputChanges={this.handleInputChanges}
                    addItems={this.addItems}
                  />
                  <ItemList
                    items={this.state.items}
                    completedItem={this.completedItem}
                  />
                </div>
              </div>
            </div>
          </div>
        );
      }
    }

    export default App;

上面提到的代码解释了我如何决定处理该方法的项目,我无法理解如何使其在 ItemList 组件中工作?

最佳答案

要能够访问 ItemList 中的 onClick 事件,您需要使其 ToDoItem 组件可点击:

<ToDoItem
    onClick={() => {
      console.log(index);
    }}
    key={index.id}
    name={index.name}
/>

ToDoItem中:

In ToDoItem you are free to decide which element the click should start.

//...
const { name, onClick } = this.props;
//...
   <li
        onClick={onClick}
        className="list-group-item text-capitalize d-flex justify-content-between my-1 custom-input"
      >
//...
<小时/>

更新:

let { items } = this.state;
let to = 0;
let from = 0;
items = items.map((item, index) => {
   if (item.id === id) {
     item.completed = true;
     from = index;
   }
   return item;
});
// here i'm move the object in top. Because you use revert array.
// If you don't want to reverse array: to = items.length
items.splice(to, 0, items.splice(from, 1)[0]);

关于javascript - 单击时删除项目并将这些项目移动到列表底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60854846/

相关文章:

javascript - 带有外部 map 着色的谷歌地图多边形

javascript - 在调整大小事件上触发多个函数

javascript - 在 Redux 状态下存储具有原型(prototype)函数的对象

javascript - ReactJS,如何在另一个组件的渲染方法中渲染一个组件?

javascript - 如何诊断此错误?

javascript - 使用 Parse Server 返回的数据更新状态后,无法让 React 组件重新渲染

javascript - 终极版。将状态值传递给 reducer

c - 如何修复选择排序交换访问冲突

algorithm - 是否有时间复杂度为 O(N) 的排序算法可用?

java - 有谁知道如何使用 array.sort() 对堆栈进行排序?我只想按 x 坐标打印点的有序列表。任何想法?