当前位置: 移动技术网 > IT编程>脚本编程>vue.js > Element输入框带历史查询记录的实现示例

Element输入框带历史查询记录的实现示例

2019年05月29日  | 移动技术网IT编程  | 我要评论

聂元梓,米莱恩,碧岭生态村

需求描述

页面的查询框增加一下显示历史查找记录

实现及踩坑记录

使用element带输入建议的输入框来实现此需求。用法详见

1. 坑1:不能直接在querysearch里返回数组,一定要调用回调函数cb来处理数据

看了一下例子,建议列表应该是个数组,然后我就在querysearch里直接返回了一个数组:

 querysearch(querystring, cb) {
  return json.parse(localstorage.getitem('srcordernoarr'))
 },

但是回到网页上却发现列表数据加载不出来

正确姿势:

 /**
 * 建议列表
 */
querysearch(querystring, cb) {
 // 调用 callback 返回建议列表的数据
 cb(json.parse(localstorage.getitem('srcordernoarr')))
}

2. 坑2:数组内数据格式有要求,格式一定要是[{value: '', xxx: ''}, {value: '', xxx: ''}, ...]

这个建议列表是根据数组内的value属性值来渲染的,所以数组内的对象一定要有value键值对。比如说是这样的:

data () {
  return {
    srcordernoarr: [{
      value: '', // 这个必须要有
      type: ''
    },
    {
      value: '',
      type: ''
    },
    {
      value: '',
      type: ''
    }]
  }
}
methods: {
  /**
   * 把搜索记录存入localstorage
   */
  setintostorage (type) {
   let self = this
   let noarr = [], // 订单号历史记录数组
    text = '', value = ''
   switch (type) {
    case 'srcorderno':
     text = 'srcordernoarr'
     value = self.srcorderno
     break
    case 'joborderno':
     text = 'jobordernoarr'
     value = self.joborderno
     break
    case 'cntno':
     text = 'cntnoarr'
     value = self.cntno
     break
    default:
     break
   }
   noarr.push({value: value, type: type})
   if(json.parse(localstorage.getitem(text))) { // 判断是否已有xxx查询记录的localstorage
    if(localstorage.getitem(text).indexof(value) > -1 ) { // 判断是否已有此条查询记录,若已存在,则不需再存储
     return
    }
    if(json.parse(localstorage.getitem(text)).length >= 5) {
     let arr = json.parse(localstorage.getitem(text))
     arr.pop()
     localstorage.setitem(text, json.stringify(arr))
    }
    localstorage.setitem(text, json.stringify(noarr.concat(json.parse(localstorage.getitem(text)))))
   }
   else { // 首次创建
    localstorage.setitem(text, json.stringify(noarr))
   }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网