简体   繁体   中英

Delete object in array in Riot.js when i have multiple tags

I try to delete an object from an array but I can't access the array for some reason. How can I get the array object in the delete_task function?

In task.tag.html

<task>
    <button onclick={opts.delete_task}>{ title }</button>
</task>

In tasklist.tag.html

<tasklist>
    <todo title="test" delete_task={delete_task}></todo>

    <script>
        this.delete_task = function () {
          for (var i = tasks.length - 1; i >= 0; i--) {
           if (tasks[i].title === this.item.title) {
              tasks.splice(i, 1);
            }
          }
        };

        this.tasks = [{title: "test"}]
     </script>
</tasklist>

You can use parent word, according to the nested loops approach

And the item from the event, according to the events handling

In the result you could have something like that: https://jsfiddle.net/relaximus/q4t8e0sc/

Actually, as the click event is bubbling up, you can use onclick on the whole task tag, in this case you will not need to pass the delete handler as a function in the tag.

And just write

 onclick={ parent.deleteTask }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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