【问题标题】:How to Add filter into a todolist application in Reactjs with using .filter如何使用 .filter 将过滤器添加到 Reactjs 中的 todolist 应用程序中
【发布时间】:2023-04-05 09:24:01
【问题描述】:

我是新手,正在尝试制作一个 todolist 网站,我已经完成了添加和删除和显示功能,只是尝试添加一个搜索功能,但我似乎无法让它工作,因为它没有过滤适当地。

我基本上希望能够使用搜索值过滤 todos.title 上的值。例如,如果我输入“ta”的值,它应该显示“取出垃圾”的待办事项项目或与该字符串匹配的任何项目。

当我尝试搜索时,它会随机输出过滤后的项目,我想知道我的过滤是否错误,或者我是否不喜欢正确显示。

我尝试将值传递到 todo.js 并在那里显示,但似乎这不是一种可行的方式,因为它应该保留在 App.js 中。

 class App extends Component {
 state = {
  todos: [
    {
      id: uuid.v4(),
      title: "take out the trash",
      completed: false
    },
    {
      id: uuid.v4(),
      title: "Dinner with wife",
      completed: true
    },
    {
      id: uuid.v4(),
      title: "Meeting with Boss",
      completed: false
    }
  ],
  filtered: []
};

// checking complete on  the state 

markComplete = id => {
  this.setState({
    todos: this.state.filtered.map(todo => {
      if (todo.id === id) {
        todo.completed = !todo.completed;
      }
      return todo;
    })
  });
};
//delete the item
delTodo = id => {
  this.setState({
    filtered: [...this.state.filtered.filter(filtered => filtered.id !== id)]
  });
};

//Add item to the list

addTodo = title => {
  const newTodo = { 
    id: uuid.v4(),
    title,
    comepleted: false
  };
  this.setState({ filtered: [...this.state.filtered, newTodo] });
};

// my attempt to do search filter on the value recieved from the search field (search):

search = (search) => {
  let currentTodos = [];
  let newList = [];
  if (search !== "") {
    currentTodos = this.state.todos;
    newList = currentTodos.filter( todo => {
      const lc = todo.title.toLowerCase();
      const filter = search.toLowerCase();
      return lc.includes(filter);
    });
  } else {
    newList = this.state.todos;
  }
  this.setState({
    filtered: newList
  });
  console.log(search);

};

componentDidMount() {
  this.setState({
    filtered: this.state.todos
  });
}

componentWillReceiveProps(nextProps) {
  this.setState({
    filtered: nextProps.todos
  });
}

render() {
  return (
    <div className="App">
      <div className="container">
        <Header search={this.search} />
        <AddTodo addTodo={this.addTodo} />
        <Todos
          todos={this.state.filtered}
          markComplete={this.markComplete}
          delTodo={this.delTodo}
        />
      </div>
    </div>
  );
}
}
export default App;

搜索值来自作为道具传递值的标头。我已经检查过了,它工作正常。

Todos.js

class Todos extends Component {
state = {
    searchResults: null
}

render() {

    return (
        this.props.todos.map((todo) => {
        return <TodoItem key={todo.id} todo = {todo} 
            markComplete={this.props.markComplete}
            delTodo={this.props.delTodo}
            />
     })
    );

}
}

TodoItem.js 只是显示项目的组件。

我不确定这是否足以 100% 理解问题,如果需要,我可以添加更多内容。

谢谢

【问题讨论】:

  • 我看到的一个问题是您使用了“过滤器”javascript 函数。这不是你要找的。我会建议你这样做:if (search) { return this.state.todos.map(todo =&gt; todo.match(`/${search.toLowerCase}/g`))}
  • 你完全混淆了过滤和待办事项状态

标签: reactjs filtering material-ui


【解决方案1】:

不确定您的脚本有什么问题。在我看来,当我尝试使用您的大部分逻辑进行重建时,它工作正常。请在此处查看工作演示:https://codesandbox.io/s/q9jy17p47j

只是我的猜测,可能是您的 &lt;TodoItem/&gt; 组件有问题,导致它无法正确呈现。也许您可以尝试使用诸如&lt;li&gt; 之类的原始元素来代替&lt;TodoItem/&gt; 之类的自定义元素。问题可能是您对 markComplete() 事物的逻辑(如果它正在执行隐藏元素工作)。

如果我遗漏了什么,请告诉我。谢谢。

【讨论】:

  • 我修好了,谢谢你的帮助,原来是我的头,我没有正确地将道具从头传递到应用程序那里搜索总是错误的。感谢您的帮助:P
  • 当然,没问题:)
猜你喜欢
  • 2010-12-02
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
  • 2011-02-04
  • 1970-01-01
相关资源
最近更新 更多