树形数据,检索功能,并保持原数据结构

检索title字段,是否包含检索关键字

recursiveFn(data, val) {
      let { hasProp } = this
      let arr = []
      data.map(item => {
        if (item.children) {
          let children = item.children
          item.children = this.recursiveFn(children, val)
          if (hasProp(item.title, val) || (item.children && item.children.length > 0)) {
            arr.push(item)
          }
        } else {
          if (hasProp(item.title, val)) {
            arr.push(item)
          }
        }
      })
      return arr
    },
    hasProp(string, prop) {
      return string.indexOf(prop) > -1
    },

猜你喜欢

转载自www.cnblogs.com/hill-foryou/p/12644016.html