欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue数组更新及过滤排序功能

程序员文章站 2023-02-23 22:14:40
前面的话   vue为了增加列表渲染的功能,增加了一组观察数组的方法,而且可以显示一个数组的过滤或排序的副本。本文将详细介绍vue数组更新及过滤排序 变异方法   v...

前面的话

  vue为了增加列表渲染的功能,增加了一组观察数组的方法,而且可以显示一个数组的过滤或排序的副本。本文将详细介绍vue数组更新及过滤排序

变异方法

  vue 包含一组观察数组的变异方法,它们将会触发视图更新,包含以下方法

push() 接收任意数量的参数,把它们逐个添加到数组末尾,并返回修改后数组的长度

pop() 从数组末尾移除最后一项,减少数组的length值,然后返回移除的项

shift() 移除数组中的第一个项并返回该项,同时数组的长度减1

unshift() 在数组前端添加任意个项并返回新数组长度

splice() 删除原数组的一部分成员,并可以在被删除的位置添加入新的数组成员

sort() 调用每个数组项的tostring()方法,然后比较得到的字符串排序,返回经过排序之后的数组

reverse() 用于反转数组的顺序,返回经过排序之后的数组

<div id="example">
 <div>
 <button @click='push'>push</button>
 <button @click='pop'>pop</button>
 <button @click='shift'>shift</button>
 <button @click='unshift'>unshift</button>
 <button @click='splice'>splice</button>
 <button @click='sort'>sort</button>
 <button @click='reverse'>reverse</button>
 </div>
 <ul>
 <li v-for="item in items" >
  {{ item.message }}
 </li>
 </ul> 
</div>

<script>
var example = new vue({
 el: '#example',
 data: {
 items: [
  {message: 'foo' },
  {message: 'bar' },
  {message: 'baz' }
 ],
 addvalue:{message:'match'}
 },
 methods:{
 push(){
  this.items.push(this.addvalue)
 },
 pop(){
  this.items.pop()
 },
 shift(){
  this.items.shift()
 },
 unshift(){
  this.items.unshift(this.addvalue)
 },
 splice(){
  this.items.splice(0,1)
 },
 sort(){
  this.items.sort()
 },
 reverse(){
  this.items.reverse()
 },
 }
})
</script>

非变异方法

  变异方法(mutation method),顾名思义,会改变被这些方法调用的原始数组。相比之下,也有非变异(non-mutating method)方法,例如: filter(), concat(), slice() 。这些不会改变原始数组,但总是返回一个新数组。当使用非变异方法时,可以用新数组替换旧数组

concat() 先创建当前数组一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回新构建的数组

slice() 基于当前数组中一个或多个项创建一个新数组,接受一个或两个参数,即要返回项的起始和结束位置,最后返回新数组

map() 对数组的每一项运行给定函数,返回每次函数调用的结果组成的数组

filter() 对数组中的每一项运行给定函数,该函数会返回true的项组成的数组

<div id="example">
 <div>
 <button @click='concat'>concat</button>
 <button @click='slice'>slice</button>
 <button @click='map'>map</button>
 <button @click='filter'>filter</button>
 </div>
 <ul>
 <li v-for="item in items" >
  {{ item }}
 </li>
 </ul> 
</div>
<script>
var example = new vue({
 el: '#example',
 data: {
 items: ['foo','bar','baz'],
 addvalue:'match'
 },
 methods:{
 concat(){
  this.items = this.items.concat(this.addvalue)
 },
 slice(){
  this.items = this.items.slice(1)
 },
 map(){
  this.items = this.items.map(function(item,index,arr){
  return index + item; 
  })
 },
 filter(){
  this.items = this.items.filter(function(item,index,arr){
  return (index > 0); 
  })
 }
 }
})
</script>

  以上操作并不会导致vue丢弃现有dom并重新渲染整个列表。vue实现了一些智能启发式方法来最大化dom元素重用,所以用一个含有相同元素的数组去替换原来的数组是非常高效的操作

无法检测

  由于js的限制, vue 不能检测以下变动的数组:

  1、利用索引直接设置一个项时,例如: vm.items[indexofitem] = newvalue

  2、修改数组的长度时,例如: vm.items.length = newlength

<div id="example">
 <div>
 <button @click='setval'>setval</button>
 <button @click='setlength'>setlength</button>
 <button @click='pop'>pop</button>
 </div>
 <ul>
 <li v-for="item in items" >{{ item }}</li>
 </ul> 
 <p>{{ message }}</p> 
</div>
<script>
var watchfunc = function(){
 example.message = '数据发生变化';
 settimeout(function(){
 example.message = '';
 },500); 
}
var example = new vue({
 el: '#example',
 data: {
 items: ['foo','bar','baz'],
 message:'',
 },
 watch:{
 items:watchfunc
 },
 methods:{
 pop(){
  this.items.pop()
 },
 setval(){
  this.items[0]= 'match';
 },
 setlength(){
  this.items.length = 2;
 }
 }
})
</script>

  以上代码中,直接设置值和长度使用watch不能检测到变化

  以下两种方式都可以实现和vm.items[indexofitem]=newvalue相同的效果, 同时也将触发状态更新

// vue.set
vue.set(example1.items, indexofitem, newvalue)
// array.prototype.splice
example1.items.splice(indexofitem, 1, newvalue)

   为了解决第二类问题,可以使用 splice

example1.items.splice(newlength)
<div id="example">
 <div>
 <button @click='setval1'>setval1</button>
 <button @click='setval2'>setval2</button>
 <button @click='setlength'>setlength</button>
 </div>
 <ul>
 <li v-for="item in items" >{{ item }}</li>
 </ul> 
 <p>{{ message }}</p> 
</div>
<script>
var watchfunc = function(){
 example.message = '数据发生变化';
 settimeout(function(){
 example.message = '';
 },500); 
}
var example = new vue({
 el: '#example',
 data: {
 items: ['foo','bar','baz'],
 message:'',
 },
 watch:{
 items:watchfunc
 },
 methods:{
 setval1(){
  vue.set(this.items, 0, 'match')
 },
 setval2(){
  this.items.splice(1, 1, 'xiaohuochai')
 }, 
 setlength(){
  this.items.splice(2)
 }
 }
})
</script>

过滤排序

  有时,要显示一个数组的过滤或排序副本,而不实际改变或重置原始数据。在这种情况下,可以创建返回过滤或排序数组的计算属性

【computed】

<div id="example">
 <ul>
 <li v-for="n in evennumbers">{{ n }}</li>
 </ul> 
</div>
<script>
var example = new vue({
 el: '#example',
 data: {
 numbers: [ 1, 2, 3, 4, 5 ],
 },
 computed: {
 evennumbers: function () {
  return this.numbers.filter(function (number) {
  return number % 2 === 0
  })
 }
 }
})
</script>

【methods】

  在计算属性不适用的情况下 (例如,在嵌套 v-for 循环中) 可以使用一个 method 方法

<div id="example">
 <ul>
 <li v-for="n in even(numbers)">{{ n }}</li>
 </ul> 
</div>
<script>
var example = new vue({
 el: '#example',
 data: {
 numbers: [ 1, 2, 3, 4, 5 ],
 },
 methods: {
 even: function (numbers) {
  return numbers.filter(function (number) {
  return number % 2 === 0
  })
 }
 }
})
</script>

Vue数组更新及过滤排序功能

总结

以上所述是小编给大家介绍的vue数组更新及过滤排序功能,希望对大家有所帮助