javascript - 为什么当值为 false(它是 bool 值)时我无法传递过滤后的数据

标签 javascript node.js vue.js vuetify.js computed-properties

我尝试使用 vuetify 过滤数据表,当我尝试使用 v-select value true( bool 数据类型)过滤时,它完成了:隐藏/过滤 false 值。

但是当我尝试过滤错误值时,真实值仍然存在。所以表值显示这两个数据都包含done: true 和done: false

这是我的代码:

    <template>
<div>
  <v-card md12>
    <v-card-actions class="justify-center">
    </v-card-actions>
    <v-card-title>
      Nutrition
      <v-spacer></v-spacer>
      <v-select 
      label="Status" 
      :items="[false,true]"
      v-model='dessertsStatus'
      ></v-select>
       <!-- <v-checkbox 
          v-model="dessertsStatus" 
          class="mx-2" 
          label="Status">
      </v-checkbox> -->
      <v-spacer></v-spacer>
      <v-text-field
        v-model="search"
        append-icon="search"
        label="Search"
        single-line
        hide-details
      ></v-text-field>
    </v-card-title>
    <v-data-table
      :headers="headers"
      :items="filteredItems"
      :search="search"
      show-expand
      item-key="name"
    >
    <template #expanded-item="{headers,item}">
        <td :colspan="headers.length">
        {{item.name}}
        {{item.calories}}
        </td>
    </template>
    <template v-slot:item.action="{ item }">
      <v-icon
        color="green"
        medium
        @click="deleteItem(item)"
      >
        check_circle
      </v-icon>
    </template>
    </v-data-table>
  </v-card>
</div>
</template>

<script>
  export default {
    logout() 
        {
            this.$router.push("/login")
        },
    data () {
      return {
        search: '',
        headers: [
          {
            text: 'Dessert (100g serving)',
            align: 'left',
            sortable: false,
            value: 'name',
          },
          { text: 'Calories', value: 'calories' },
          { text: 'Fat (g)', value: 'fat' },
          { text: 'Carbs (g)', value: 'carbs' },
          { text: 'Protein (g)', value: 'protein' },
          { text: 'Iron (%)', value: 'iron' },
          { text: 'Actions', value: 'action', sortable: false },
          // { text: 'Status Done', value: 'done' },
        ],
        desserts: [
          {
            name: 'Frozen Yogurt',
            calories: 159,
            fat: 6.0,
            carbs: 24,
            protein: 4.0,
            iron: '1%',
            done: true,
          },
          {
            name: 'Ice cream sandwich',
            calories: 237,
            fat: 9.0,
            carbs: 37,
            protein: 4.3,
            iron: '1%',
            done: false,
          },
          {
            name: 'Eclair',
            calories: 262,
            fat: 16.0,
            carbs: 23,
            protein: 6.0,
            iron: '7%',
            done: false,
          },
          {
            name: 'Cupcake',
            calories: 305,
            fat: 3.7,
            carbs: 67,
            protein: 4.3,
            iron: '8%',
            done: true,
          },
          {
            name: 'Gingerbread',
            calories: 356,
            fat: 16.0,
            carbs: 49,
            protein: 3.9,
            iron: '16%',
            done: false,
          },
          {
            name: 'Jelly bean',
            calories: 375,
            fat: 0.0,
            carbs: 94,
            protein: 0.0,
            iron: '0%',
            done: true,
          },
          {
            name: 'Lollipop',
            calories: 392,
            fat: 0.2,
            carbs: 98,
            protein: 0,
            iron: '2%',
            done: false,
          },
          {
            name: 'Honeycomb',
            calories: 408,
            fat: 3.2,
            carbs: 87,
            protein: 6.5,
            iron: '45%',
            done: false,
          },
          {
            name: 'Donut',
            calories: 452,
            fat: 25.0,
            carbs: 51,
            protein: 4.9,
            iron: '22%',
            done: false,
          },
          {
            name: 'KitKat',
            calories: 518,
            fat: 26.0,
            carbs: 65,
            protein: 7,
            iron: '6%',
            done: true,
          },
        ],
        dessertsStatus: '',
      }
    },
    computed: {
      filteredItems() {
        return this.desserts.filter((i) => 
          {
            return !this.dessertsStatus || (i.done == this.dessertsStatus);
          })
        }
      },
    };
</script>

我的计算代码有问题吗?或者它与 javascript 上的虚假值有关吗?

最佳答案

我认为你的问题在这里:

return !this.dessertsStatus || (i.done == this.dessertsStatus)

this.dessertsStatusfalse 时,它不会到达第二部分,因为第一部分,它将返回 true

相反,您想要这样的东西,第一个条件更明确地针对空值:

return this.dessertsStatus === '' || (i.done === this.dessertsStatus)

我还将 == 更改为 ===。在这里这应该不重要,但允许强制是自找麻烦。

就我个人而言,我会使用 undefinednull 而不是空字符串作为 '' 的默认值,但我在上面的代码中将其保留为 '' 以匹配您的 data 中的内容。

关于javascript - 为什么当值为 false(它是 bool 值)时我无法传递过滤后的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59954705/

相关文章:

Vue.js 一些错误/警告很难调试

vue.js - 始终首先过滤在 Quasar Select 上选择的内容

javascript - 如何在 Javascript 中读取 JSON(服务器响应)?

javascript - 揭示模块模式和 jquery 失败

javascript - 对回调函数进行编码以接收访问 token 需要什么?

javascript - 为什么元素没有以 Angular 动态添加

javascript - 从 url 调用 Nodejs 函数

node.js - 用 CoffeeScript jsx 开 Jest ?

javascript - 如何测量 JavaScript 引擎中的 CPU 缓存和预取缺失?

javascript - 页面刷新后 Vue.js eventBus 不发出属性