如何在select vue.js中获得SPECIFC选择?

发布于 2025-01-31 19:50:42 字数 3434 浏览 0 评论 0原文

你好吗? 我正在研究Vue,并且我不知道该去哪里的当前任务。 我有一个选择,当我单击时,我需要在屏幕上显示与该选择相对应的内容。例如,将“待办事项”选项放在选择中时,只有结论= false的任务才能出现在屏幕上。我只走了这么远,我需要帮助才能继续。你能帮助我吗?谢谢

这是我的应用

<template>
  <div id="app">
    <h1>Lista de Tarefas</h1>
    <List :data="list" @remove="handleRemove"/>
    <Form @add="addNewTask" @onChange="handleN"/>
  </div>
</template>

<script>
import List from "./components/List.vue";
import Form from "./components/Form.vue";

export default {
  components: {
    List,
    Form,
  },
  data() {
    return {
      list: [],
    };
  },
  methods: {
    addNewTask(newTask) {
      this.list.push(newTask);
    },
    handleRemove(item) {
      const index = this.list.findIndex(i => i.id === item.id)
      this.list[index].excluded = true
    },
    handleN(item) {
      const index = this.list.findIndex(i => i.id === item.id)
      this.list[index].concluded = true
    }
  },
};
</script>

这是我的列表。

<template>
  <ul>
    <select v-model="selected" @change="onChange($event)">
      <option disabled value="">Escolha a visualização</option>
      <option v-for="option in options" :key="option.text">
        {{ option.text }}
      </option>
    </select>

    <li v-for="item in itens" :key="item.id">
      <input type="checkbox" id="checkbox" v-model="item.concluded" />
      <label for="checkbox"> {{ item.description }} </label>
      <button @click="() => $emit('remove', item)">Excluir</button>
    </li>
  </ul>
</template>

<script>
export default {
  props: {
    data: {
      type: Array,
      default: () => {},
    },
  },
  data() {
    return {
      selected: "",
      options: [
        { text: "Todos", value: "1" },
        { text: "A fazer", value: "2" },
        { text: "Concluído", value: "3" },
        { text: "Deletado", value: "4" },
      ],
    };
  },
  computed: {
    itens() {
      return this.data.filter((item) => item.excluded === false);
    },
  },
  methods: {
    onChange(event) {
      console.log(event.target.value);
      return this.data.filter((item) => item.concluded === false);
    },
  },
};
</script>

这是我的形式。

<template>
  <form @submit.prevent="handleNewTask">
    <input type="text" v-model="newTask" placeholder="Insira a tarefa"/>
    <input type="submit" value="Adicionar"/>
  </form>
</template>

<script>
import Task from '../types/Task.js'

export default {
  data() {
    return {
      newTask: "",
    };
  },
  methods: {
      handleNewTask() {
          this.$emit('add', new Task(this.newTask))
          this.newTask = ''
      }
  },
};
</script>

这是我的任务。

export default class {
    constructor(description) {
        this.description = description,
        this.id = Math.random(),
        this.concluded = false,
        this.excluded = false
    }
}

我看一些教程,阅读文档和一些堆叠的问题,但我真的无法离开这里 事先感谢您的帮助

How are you?
I'm studying Vue and I'm stuck on the current task not knowing where to go.
I have a select that when I click I need to show on screen only what corresponds to that selection. For example, when placing the "to do" option in the select, only the tasks with a concluded=false should appear on the screen. I've only gotten this far and I need help to continue. Can you help me? Thanks

This is my App.vue

<template>
  <div id="app">
    <h1>Lista de Tarefas</h1>
    <List :data="list" @remove="handleRemove"/>
    <Form @add="addNewTask" @onChange="handleN"/>
  </div>
</template>

<script>
import List from "./components/List.vue";
import Form from "./components/Form.vue";

export default {
  components: {
    List,
    Form,
  },
  data() {
    return {
      list: [],
    };
  },
  methods: {
    addNewTask(newTask) {
      this.list.push(newTask);
    },
    handleRemove(item) {
      const index = this.list.findIndex(i => i.id === item.id)
      this.list[index].excluded = true
    },
    handleN(item) {
      const index = this.list.findIndex(i => i.id === item.id)
      this.list[index].concluded = true
    }
  },
};
</script>

This is my List.vue

<template>
  <ul>
    <select v-model="selected" @change="onChange($event)">
      <option disabled value="">Escolha a visualização</option>
      <option v-for="option in options" :key="option.text">
        {{ option.text }}
      </option>
    </select>

    <li v-for="item in itens" :key="item.id">
      <input type="checkbox" id="checkbox" v-model="item.concluded" />
      <label for="checkbox"> {{ item.description }} </label>
      <button @click="() => $emit('remove', item)">Excluir</button>
    </li>
  </ul>
</template>

<script>
export default {
  props: {
    data: {
      type: Array,
      default: () => {},
    },
  },
  data() {
    return {
      selected: "",
      options: [
        { text: "Todos", value: "1" },
        { text: "A fazer", value: "2" },
        { text: "Concluído", value: "3" },
        { text: "Deletado", value: "4" },
      ],
    };
  },
  computed: {
    itens() {
      return this.data.filter((item) => item.excluded === false);
    },
  },
  methods: {
    onChange(event) {
      console.log(event.target.value);
      return this.data.filter((item) => item.concluded === false);
    },
  },
};
</script>

This is my Form.vue

<template>
  <form @submit.prevent="handleNewTask">
    <input type="text" v-model="newTask" placeholder="Insira a tarefa"/>
    <input type="submit" value="Adicionar"/>
  </form>
</template>

<script>
import Task from '../types/Task.js'

export default {
  data() {
    return {
      newTask: "",
    };
  },
  methods: {
      handleNewTask() {
          this.$emit('add', new Task(this.newTask))
          this.newTask = ''
      }
  },
};
</script>

And this is my Task.js

export default class {
    constructor(description) {
        this.description = description,
        this.id = Math.random(),
        this.concluded = false,
        this.excluded = false
    }
}

I watch some tutorials, read the documentation and some StackOverflow questions but I really can't get out of here
Thanks in advance for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趴在窗边数星星i 2025-02-07 19:50:42

基于您如何构建应用程序,我们唯一关注的问题应该是list.vue文件。

您的目标是根据选择(SELECTION属性)过滤结果。但是,您的问题是您甚至在任何地方都没有使用它。

我知道您在onChange方法上硬编码过滤器,但这首先是错误的,因为您并没有真正更改任何内容(您正在返回数组),其次它效率低下。

一种更好的方法是像这样更新计算的itens函数:

itens() {
  return this.data.filter((item) => {
    if (this.selected === '1'){
      return item.concluded === false
    } else if (this.selected === '2'){
      // filter another way
    } else if (... // so on and so forth
  });
},

另外,在将它们发送到组件之前,我会过滤排除的项目。如果您不使用它,请不要发送。

删除onChange&lt; select&gt;和关联方法上的事件,因为它们现在未使用。

Based on how you have structured your app, our only concern should be with the List.vue file.

Your goal is to filter the results based on the selection (selected property). However, your issue is that you are not even using that anywhere.

I know you are hard coding the filter on the onChange method but that is, first of all wrong because you aren't really changing anything (you are returning an array), and secondly it's inefficient.

A better way to do it is to update the computed itens function like so:

itens() {
  return this.data.filter((item) => {
    if (this.selected === '1'){
      return item.concluded === false
    } else if (this.selected === '2'){
      // filter another way
    } else if (... // so on and so forth
  });
},

Also, I would filter out the excluded items before sending them to the component. If you aren't going to use it, don't send it.

Remove the onChange event on the <select> and the associated method since they are now unused.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文