简体   繁体   English

为待办事项添加任务

[英]Add tasks for todo list

I am trying to add tasks for each todo list that has a specific title.我正在尝试为每个具有特定标题的待办事项列表添加任务。

Can I get a specific todo list by its id and add some tasks to it?我可以通过它的 id 获取特定的待办事项列表并向其中添加一些任务吗? I am new to javascript, so I searched google about adding lists for a specific list with no results :(我是 javascript 新手,所以我在谷歌上搜索了有关为特定列表添加列表但没有结果的信息:(

class Model {
  constructor() {}

  this.todos = [
      { 
        id: 1,
       title: 'Outside',
       text: 'Running',
       complete: false,
       tasks: [
       { id: 1, text: 'Run a marathon', complete: false},
       { id: 2, text: 'Run with freinds', complete: false}
       ]
    },
    { 
        id: 2,
       title: 'Garden',
       text: 'Plant',
       complete: false,
       tasks: [
       { id: 1, text: 'Plant a garden', complete: false},
       { id: 2, text: 'Water the garden', complete: false}
       ]
    }];

    addTodo(todoText) {
        const todo = {
        id: this.todos.length > 0 ? this.todos[this.todos.length - 1].id + 1 : 1,
        text: todoText,
        complete: false,
        tasks: []
    }

    this.todos.push(todo)
  }
  }

Is it true to do like addTodo function for adding a tasks for a specific todo list like this?像这样为特定的待办事项列表添加任务的 addTodo 函数是真的吗?

addTodoTask(todoTaskText) {
        const todoTask = {
        id: this.todos.tasks.length > 0 ? this.todos[this.todos.tasks.length - 1].id + 1 : 1,
        text: todoText,
        complete: false,
    }

    this.todos.tasks.push(todoTask)
  }

and how to add a list of a list in javascript like:以及如何在 javascript 中添加列表列表,例如:

<ul>
<li>Running
<ul>
<li>Run a marathon</li>
<li>Run with freind</li>
</ul>
</li>
</ul>

You could make each class handle rendering its own content and just map the list items consecutively while rendering from the top-down.您可以让每个类处理呈现自己的内容,并在自上而下呈现时连续映射列表项。

Edit: The render() methods make use of ES6 template literals .编辑: render()方法使用 ES6 模板文字 These are special strings that allow you embed variabes and expressions without the use of string concatenation.这些是特殊的字符串,允许您在不使用字符串连接的情况下嵌入变量和表达式。

 const main = () => { let todoList = new TodoList({ todos : getData() }) document.body.innerHTML = todoList.render() } class TodoTask { constructor(options) { this.id = options.id this.text = options.text this.complete = options.complete } render() { return `<li>[${this.id}] ${this.text} (${this.complete})</li>` } } class TodoEntry { constructor(options) { this.id = options.id this.title = options.title this.text = options.text this.complete = options.complete this.tasks = [] if (options.tasks) { options.tasks.forEach(task => this.addTask(task)) } } addTask(task) { this.tasks.push(new TodoTask(Object.assign({ id : (this.tasks.length || 0) + 1 }, task))) } render() { return `<li> [${this.id}] ${this.title} (${this.complete}) <ul>${this.tasks.map(task => task.render()).join('')}</ul> </li>` } } class TodoList { constructor(options) { this.todos = [] if (options.todos) { options.todos.forEach(todo => this.addTodo(todo)) } } addTodo(todo) { this.todos.push(new TodoEntry(Object.assign({ id : (this.todos.length || 0) + 1 }, todo))) } render() { return `<ul>${this.todos.map(todo => todo.render()).join('')}</ul>` } } function getData() { return [{ id: 1, title: 'Outside', text: 'Running', complete: false, tasks: [{ id: 1, text: 'Run a marathon', complete: false }, { id: 2, text: 'Run with freinds', complete: false }] }, { id: 2, title: 'Garden', text: 'Plant', complete: false, tasks: [{ id: 1, text: 'Plant a garden', complete: false }, { id: 2, text: 'Water the garden', complete: false }] }] } main() // entry

To add a task your todo, you should have a way of knowing which todo list you're updating.要将任务添加到您的待办事项,您应该有办法知道您正在更新哪个待办事项列表。 Like using the todo's id.就像使用 todo 的 id 一样。

For example your addTaskToTodo will looks like so.例如你的 addTaskToTodo 看起来像这样。

addTask(todoId, taskObject) {
  // find that todos index
  const todoIndex = this.todos.findIndex(todo => todo.id ===todoId);
  // using that index update the tasks
  this.todos[todoIndex].tasks.push(taskObject)
} 

This assumes your taskObject already has all the properties.这假设您的 taskObject 已经具有所有属性。 If you need to manually update its id, you can also do that before pushing by checking the length of the tasks and incrementing by 1.如果您需要手动更新其 id,您也可以在推送之前通过检查任务的长度并增加 1 来执行此操作。

I made an example of how to use dictionaries instead of arrays, and also a random ID.我举了一个例子,说明如何使用字典而不是数组,还有一个随机 ID。 I think you will find it much cleaner and simpler:我想你会发现它更干净、更简单:

class Model {
  constructor() { }

  todos = {
    1: {
      id: 1,
      title: 'Outside',
      text: 'Running',
      complete: false,
      tasks: {
        1: { id: 1, text: 'Run a marathon', complete: false },
        2: { id: 2, text: 'Run with freinds', complete: false }
      }
    },
    2: {
      id: 2,
      title: 'Garden',
      text: 'Plant',
      complete: false,
      tasks: {
        1: { id: 1, text: 'Plant a garden', complete: false },
        2: { id: 2, text: 'Water the garden', complete: false }
      }


    }
  }


  getRandomId = () => {
    return '_' + Math.random().toString(36).substr(2, 9);
  }

  addTodo(todoText) {
    const id = this.getRandomId();
    const todo = {
      id,
      text: todoText,
      complete: false,
      tasks:{}
    }

    this.todos[id] = todo;
  }

  addTodoTask(todoTaskText,todoId) {//Pass also the id of the todo, to know where this task belongs to.
    const id = this.getRandomId();
    const todoTask = {
      id,
      text: todoTaskText,
      complete: false,
    }

    this.todos[todoId].tasks[id] = todoTask
  }
}

This way you could easily edit/remove both todos and tasks, just by their id, without using any messy Array.filter and such通过这种方式,您可以轻松地编辑/删除待办事项和任务,只需通过它们的 id,而无需使用任何凌乱的 Array.filter 等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM