tools-common

1.0.21 • Public • Published

常用方法工具

common.js

1.1 时间相关

1.1.1 sortByTime

 sortByTime ({ data, key, order }) // 对数组按时间排序
 data:数据数组
 key:数组对象时间参数对应的键名
 order:默认顺序,false:倒序

1.1.2 randomNumber

 randomNumber (length) // 生成指定长度随机数,默认长度为10
 length:随机数长度

1.1.3 returnNowTime

returnNowTime () // 返回当前时间点,格式为YYYY-MM-DD HH-MM-SS

1.2 下载、防抖相关

1.2.1 downloadResult

 downloadResult (data, docName) // 构造a标签下载后销毁
 data:返回的文件流
 docName:下载文件名称

1.2.2 debounce

 debounce (fn, time) // 防抖
 fn:防抖回调
 time:防抖周期时长
<script>
import toolsCommon from 'tools-common'
export default {
  created () {
     // 初始化防抖方法,fn为防抖回调
    this.inputDebounce = toolsCommon.debounce(this.fn, 1000)
  },
  watch: {
     searchText () { // searchText为input对应value
          this.inputDebounce('test') //'test'为回调函数需要携带的参数
    }
  },
  methods: {
     fn(params){ // 防抖回调函数,params为 inputDebounce调用传参
          console.log('防抖回调' params) // --->  防抖回调test
     }
  }
}
</script>

1.3 操作tree相关

1.3.1 getTreeNodeByKey

 getTreeNodeByKey ({data, key, value}) // 通过指定tree内键及其值找到对应node
 data:tree数组
 key:数组对象value对应的键名
 value: 过滤参数

1.3.2 getNodeParentIds

 getNodeParentIds (data, key, value, parentIds) // 通过指定唯一标识找到tree内所有父级唯一标识
 data:tree数组
 key:数组对象value对应的键名
 value: 过滤参数
 parentIds:返回所有唯一标识的数组,外部传入接收

1.3.3 filterNode

 filterNode (key, idKey, value, tree, filterNodes) // 关键字过滤目录树
 key:节点名称对应键名
 idKey:唯一标识对应键名
 value:搜索关键字
 tree:待过滤树
 filterNodes:接收过滤后树节点,外部传入
const data = [
  {
    title: 'parent 1',
    key: '0-0',
    children: [
      {
        title: 'parent 1-0',
        key: '0-0-0',
        disabled: true,
        children: [
          { title: 'leaf', key: '0-0-0-0', disableCheckbox: true },
          { title: 'leaf', key: '0-0-0-1' },
        ],
      },
      {
        title: 'parent 1-1',
        key: '0-0-1',
        children: [{ key: '0-0-1-0', title: 'sss' }],
      },
    ],
  },
];
const filterNodes = []
filterNode ('title', 'key', 'parent 1-0', data, filterNodes)
console.log('filterNodes', filterNodes)

未完待续

Readme

Keywords

Package Sidebar

Install

npm i tools-common

Weekly Downloads

1

Version

1.0.21

License

ISC

Unpacked Size

5.96 kB

Total Files

3

Last publish

Collaborators

  • tongzhuang