使用vue2的filter过滤列表

本文介绍了在Vue2中如何创建并使用filter过滤列表数据,通过自定义filter实现对组件中数据的筛选和处理,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

组件部分

<template>
  <div class="todo-contain">
    <h1>TODOS</h1>
    <div class="todolist-contain">
      <header>
        <nav>
          <a v-for="(item,index) in tabListMap" :key="index" :class="currentTabKey==index ? 'active':''" @click="changeTab(index)">
            {{item}}
          </a>
        </nav>
        <div class="todo-add">
          <input type="text" ref="addInput" v-model="addTodoText" placeholder="请输入待办事项" />
          <button @click="handleAddTodo()">添加</button>
        </div>
        <span class="todo-count">{{totalTodoCount}}个待办</span>
      </header>
      <div class="todo-list">
        <ul>
          <li v-for="(item ,index) in filterTodoList" :key="index" :class=" (item.checked && currentTabKey!='done') ?'done':''">
            <label>
              <template v-if="item.status!='done' && currentTabKey!='done'">
                <input type="checkbox" :value="item.id" v-model="item.checked" />
              </template>
              {{item.title}}
            </label>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'TodoList2',
  props: {
    todoListData: {
      default: []
    }
  },
  data() {
    return {
      todoListData_: {},
      tabListMap: {
        all: '全部',
        todo: '待办',
        doing: '进行中',
        done: '已完成'
      },
      currentTabKey: 'all',
      addTodoText: ''
    }
  },
  created() {
    this.todoListData_ = [...this.todoListData] //深拷贝
  },
  computed: {
    filterTodoList: function() {
      var key = this.currentTabKey
      var todoListData_ = this.todoListData_
      return todoListData_.filter(function(item) {
        if (key === 'all') {
          return item
        } else if (key === 'done') {
          return item.status == key || item.checked == true
        } else {
          return item.status == key
        }
      })
    },
    totalTodoCount() {
      var count = 0
      this.todoListData_.forEach(function(item, index) {
        if (item.status === 'todo' && item.checked !== true) {
          count++
        }
      })
      return count
    }
  },
  methods: {
    changeTab(selectedTab) {
      this.currentTabKey = selectedTab
    },
    handleAddTodo() {
      var newData = {
        title: this.addTodoText,
        status: 'todo'
      }
      this.todoListData_.unshift(newData) //从头加入
      this.addTodoText = '' //清空添加输入框
    }
  }
}
</script>

<style>
html,
body {
  background: #f5f5f5;
  margin: 0;
  padding: 0;
}
ul,
li {
  margin: 0;
  padding: 0;
}
a {
  cursor: pointer;
}
.todo-contain {
  width: 500px;
  margin: 0 auto;
}
.todo-contain h1 {
  font-size: 60px;
  font-family: Arial, Helvetica, sans-serif;
  text-align: center;
  color: #ecd7d7;
  line-height: 60px;
  margin-bottom: 10px;
}
.todolist-contain {
  border: 1px solid #ccc;
  background: #fff;
}
.todolist-contain header {
  position: relative;
}
.todolist-contain nav {
  text-align: center;
  padding: 5px 0;
  border-bottom: 1px solid #e5e5e5;
}
.todolist-contain nav a {
  display: inline-block;
  padding: 3px 10px;
  font-size: 14px;
  color: #94786e;
  text-decoration: none;
}
.todolist-contain nav a.active {
  border: 1px solid #ecd7d7;
  border-radius: 5px;
}
.todo-count {
  position: absolute;
  left: 10px;
  top: 8px;
  font-size: 14px;
}
.todo-add {
  padding: 5px 0;
  border-bottom: 1px solid #e5e5e5;
}
.todo-add input {
  border: 0 none;
  font-size: 24px;
  padding: 5px 15px;
  width: 400px;
}
.todo-add button {
  border: 0 none;
  background: #2695ee;
  color: #fff;
  border-radius: 5px;
  font-size: 16px;
  padding: 5px 10px;
  cursor: pointer;
}
.todo-list li {
  list-style: none;
  border-bottom: 1px solid #e5e5e5;
  font-size: 24px;
  padding: 10px 10px 10px 50px;
  position: relative;
  color: #534d4e;
}
.todo-list li input {
  width: 40px;
  height: 40px;
  position: absolute;
  left: 0;
  top: 3px;
  background: none;
  -webkit-appearance: none;
  appearance: none;
}
.todo-list li input:focus {
  outline: none;
}
.todo-list li input:after {
  content: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40" viewBox="-10 -18 100 135"><circle cx="50" cy="50" r="50" fill="none" stroke="#ededed" stroke-width="3"/></svg>');
  display: block;
}
.todo-list li input:checked:after {
  content: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40" viewBox="-10 -18 100 135"><circle cx="50" cy="50" r="50" fill="none" stroke="#bddad5" stroke-width="3"/><path fill="#5dc2af" d="M72 25L42 71 27 56l-4 4 20 20 34-52z"/></svg>');
}
.todo-list li.done {
  font-style: italic;
  color: #dadbdb;
  text-decoration: line-through;
}
.todo-list li:last-child {
  border-bottom: 0;
}
</style>

app.vue

<template>
  <div id="app">
    <TodoList :todoListData="todoListData"></TodoList>
  </div>
</template>

<script>
import TodoList from './components/todoList/TodoList'
export default {
  name: 'App',
  components: {
    TodoList
  },
  data() {
    return {
      todoListData: {
        todo: [
          {
            id: 1,
            title: 'todo 1'
          },
          {
            id: 2,
            title: 'todo 2'
          },
          {
            id: 3,
            title: 'todo 3'
          },
          {
            id: 4,
            title: 'todo 4'
          }
        ],
        doing: [
          {
            id: 1,
            title: 'doing 1'
          },
          {
            id: 2,
            title: 'doing 2'
          },
          {
            id: 3,
            title: 'doing 3'
          },
          {
            id: 4,
            title: 'doing 4'
          }
        ],
        done: [
          {
            id: 1,
            title: 'done 1'
          },
          {
            id: 2,
            title: 'done 2'
          },
          {
            id: 3,
            title: 'done 3'
          },
          {
            id: 4,
            title: 'done 4'
          }
        ]
      },
      todoListData2: [
        {
          id: 1,
          title: 'todo 1',
          status: 'todo'
        },
        {
          id: 2,
          title: 'todo 2',
          status: 'todo'
        },
        {
          id: 3,
          title: 'todo 3',
          status: 'todo'
        },
        {
          id: 4,
          title: 'todo 4',
          status: 'todo'
        },
        {
          id: 1,
          title: 'doing 1',
          status: 'doing'
        },
        {
          id: 2,
          title: 'doing 2',
          status: 'doing'
        },
        {
          id: 3,
          title: 'doing 3',
          status: 'doing'
        },
        {
          id: 4,
          title: 'doing 4',
          status: 'doing'
        },
        {
          id: 1,
          title: 'done 1',
          status: 'done'
        },
        {
          id: 2,
          title: 'done 2',
          status: 'done'
        },
        {
          id: 3,
          title: 'done 3',
          status: 'done'
        },
        {
          id: 4,
          title: 'done 4',
          status: 'done'
        }
      ]
    }
  }
}
</script>

<style lang="less" scoped>
@label-width: 200px;
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  margin-top: 60px;
}
.logo {
  text-align: center;
}
.row {
  margin-bottom: 24px;
  vertical-align: top;
  zoom: 1;

  &__label,
  &__content {
    font-size: 14px;
  }
  &__label {
    width: @label-width;
    text-align: right;
    vertical-align: middle;
    float: left;
    color: #515a6e;
    line-height: 1;
    padding: 10px 12px 10px 0;
    box-sizing: border-box;
  }
  &__content {
    margin-left: @label-width;
    position: relative;
    line-height: 32px;
  }
}
</style>

Vue2中的过滤器(filter)是用于对文本进行格式化的功能。过滤器可以在插值表达式和v-bind属性绑定中使用。在Vue中,过滤器可以分为全局过滤器和私有过滤器(本地过滤器)两种类型。 全局过滤器是在Vue实例之间共享的过滤器。可以使用Vue.filter方法来定义全局过滤器,将其添加到Vue过滤列表中。全局过滤器可以在任何Vue实例的模板中使用。 私有过滤器是在当前Vue实例所控制的区域内使用过滤器。私有过滤器是在Vue实例的filters节点下定义的。私有过滤器只能在当前Vue实例的模板中使用使用过滤器的基本语法是在文本表达式或属性绑定的末尾添加管道符(|),然后跟上过滤器的名称。例如,可以使用全局过滤器来格式化时间。 具体的使用方法和示例可以参考引用\[1\]、\[2\]和\[3\]中的内容。这些引用提供了关于过滤器的基本用法、全局过滤器的定义和示例,以及过滤器在Vue中的作用和注意事项。 #### 引用[.reference_title] - *1* *2* [Vue2.0开发之——Vue基础用法-过滤器(26)](https://blog.youkuaiyun.com/Calvin_zhou/article/details/127993274)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Vue2过滤器的用法详解](https://blog.youkuaiyun.com/sunyctf/article/details/127851578)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值