## 常用的工具类方法
常用的工具类方法定义在
```html
```
## 方法一览表
|方法|说明|方法|说明
|:---|:---|:---|:---|
|getName()|获取随机名称|getWriteable(permission)|判断控件是否可编辑|
|mergeValidate(rules, appendRules)|融合校验规则|reduceValidate(rules, reduceRules)|移除校验规则|
|validateString2Object(rule)|字符串校验规则转对象|stringSplit(str, sep)|字符串分割为数组|
|addRequiredOrNot(permission, validate)|判断控件是否要添加必填校验|formatDate(value)|格式化日期时间|
|formatDateYear(value)|格式化日期|dateIsBefore(dateOne, dateTwo, canEquals)|判断日期1是否早于日期2|
|dateCalc(startDate, endDate, op)|计算开始时间和结束时间的间隔|tile2nest(array, key, pKey, childrenKey)|将平铺结构转为嵌套结构|
|getOnlineFormInstance(instance)|获取表单的VueComponent实例|getSubScopeElAndIndex(element)|获取子表的VueComponent实例及当前行索引|
|getSubInputScopeByModelExpression(subScopeEl, expression)|根据子表VueComponent实例获取表达式所对应的VueComponent实例|validateForm(instance, scopeName)|校验指定作用域的表单|
|getParentElementByAttribute(element, attribute)|获取上级元素中有指定属性的元素|getSomeAttributeFromParentElement(element, attribute)|获取上级元素中指定属性的值|
|getWholePathOfSub(subPath, mainPath, index)|将子表属性路径转换为完整的属性路径|getValueByPath(instance, path, subIndex)|获取路径所对应的值|
|setValueByPath(instance, path, value, subIndex)|根据路径设置值|parseToJson(jsonStr, type)|字符串解析为JSON对象|
|convertCurrency(currencyDigits)|数字转人民币大写|getUrlKey(name)|获取URL地址中指定参数的值|
|arrayMove(ary, part, direct)|将数组中的一个或多个元素调整顺序|getValueByConfigKey(instance, config, key)|通过配置来获取对应的值|
|setValueByConfigKey(instance, config, key, value)|通过配置来设置对应的值|objectEquals(obj1, obj2, props)|深度对比两个对象是否相等|
|arrayEquals(ary1, ary2)|深度对比两个数组是否相等|trimEachLine(text)|对大文本进行缩进处理|
|hashCode(text)|获取大文本的哈希编码|Array.prototype.remove(item)|数组中移除指定项|
|Array.prototype.unique(arg)|数组去重复|Array.prototype.trim()|数组去除空白项|
|Array.prototype.groupByKey(key)|数组按指定属性分组|-|-|
|Array.prototype.extractByKey(key)|数组抽取指定属性的值成为一个新的数组|nest2tile(arr,childrenKey,obtainChildren)|将嵌套结构转换为平铺结构|
## getName()
返回值`string`
获得一个随机产生的8位长度的字母数字组成的名称。
## getWriteable(permission)
返回值`boolean`
判断`permission`权限是否对应可编辑状态。
## mergeValidate(rules, appendRules)
返回值`string/object`
在已有规则`rules`中追加新的规则`appendRules`,如果`rules`和`appendRules`均为字符串格式,合并后仍为字符串格式;如果其中一个为Object格式,合并后为Object格式。
`rules`和`appendRules`均支持多个校验规则的情况下完成合并,而且在旧规则和追加规则中均有某一个规则时,会以`appendRules`中的规则覆盖掉`rules`中的规则。
## reduceValidate(rules, reduceRules)
返回值`string/object`
从已有规则`rules`中移除`reduceRules`对应的规则,返回的规则与`rules`的格式一样。
## validateString2Object(rule)
返回值`object`
将字符串格式的规则`rule`转换为对象格式。
## stringSplit(str, sep)
返回值`array`
字符串str按照sep分割为数组,并清理掉数组结果中的空白项。
## addRequiredOrNot(permission, validate)
返回值`string/object`
根据您`permission`权限信息来判断,当前`validate`中是否需要添加校验规则。是则返回添加了必填规则的校验规则表达式,否则返回原校验规则表达式。
## formatDate(value)
返回值`string`
按照`yyyy-MM-dd HH:mm:ss`的格式对`value`进行日期时间格式化,`value`的类型支持`Date`、时间戳、字符串等多种格式。
## formatDateYear(value)
返回值`string`
按照`yyyy-MM-dd`的格式对`value`进行日期格式化,`value`的类型支持字符串格式的日期类型。
## dateIsBefore(dateOne, dateTwo, canEquals)
返回值`boolean`
日期`dateOne`是否早于日期`dateTwo`,是则返回`true`,否则返回`false`。
另外参数`canEquals`表示两个日期能否不相等,该参数默认为`false`,表示不能相等,即日期`dateOne`等于`dateTwo`时返回`false`;反之如果该参数传入`true`时,日期`dateOne`等于`dateTwo`时返回`true`。
## dateCalc(startDate, endDate, op)
返回值`number`
计算`startDate`和`endDate`之间相差几个`op`单位,`op`的默认值为`day`,`op`的选项有:
+ year
- month
- day
- hour
- minute
- second
## tile2nest(array, key, pKey, childrenKey)
返回值`array`
将平铺结构的数组转换为嵌套结构的数组。参数说明:
+ `array`为待转换的平铺结构数组
- `key`为数据的主键,默认为`id`
- `pKey`为数据中存放父级数据主键的key,默认为`parentId`
- `childrenKey`为当数据转为嵌套结构后,子数据以什么key来存放,默认为`children`
如下所示,转换前后的结构:
```javascript
// 平铺结构(其中通过parentId标记出其父级数据的id)
const tile = [
{ id: "1", parentId: "-1", name: "1" },
{ id: "2", parentId: "1", name: "1-2" },
{ id: "3", parentId: "2", name: "1-2-3" },
{ id: "4", parentId: "2", name: "1-2-4" },
{ id: "5", parentId: "1", name: "1-5" },
{ id: "6", parentId: "5", name: "1-5-6" }
];
// 转换为嵌套结构后
const nest = [
{ id: "1", name: "1", children: [
{ id: "2", name: "1-2", children: [{ id: "3", name: "1-2-3" }, { id: "4", name: "1-2-4" }] },
{ id: "5", name: "1-5", children: [{ id: "6", name: "1-5-6" }] }
]}
];
```
## getOnlineFormInstance(instance)
返回值`object`
通过控件的`vueComponent`实例获取控件所在的表单的`vueComponent`实例。
## getSubScopeElAndIndex(element)
返回值`{ vueComponent, integer }`
根据子表对象中的某个Dom元素获取子表所对应的`vueComponent`对象及当前索引`index`。
特别注意:
因为是通过`data-subname`来确定是否为子表,以及通过`data-index`来记录索引,所以只当子表按照以下格式时,该方法才能返回正确的结果。
```html