Commit cbcdf24c7bf83a419566ce719a4ebf52ab545f0b

Authored by zhangdaihao
1 parent d81244b5

j-editable-table列表中数据字典列如何配合DictSelectUtil.js函数,实现自动显示字典文本 #253

ant-design-vue-jeecg/src/components/jeecg/JEditableTable.vue
1 1 <!-- JEditableTable -->
2   -<!-- @version 1.4.1 -->
  2 +<!-- @version 1.4.2 -->
3 3 <!-- @author sjlei -->
4 4 <template>
5 5 <a-spin :spinning="loading">
... ... @@ -7,7 +7,7 @@
7 7 <!-- 操作按钮 -->
8 8 <div v-if="actionButton" class="action-button">
9 9 <a-button type="primary" icon="plus" @click="handleClickAdd">新增</a-button>
10   - <span class="gap"/>
  10 + <span class="gap"></span>
11 11 <template v-if="selectedRowIds.length>0">
12 12 <a-popconfirm
13 13 :title="`确定要删除这 ${selectedRowIds.length} 项吗?`"
... ... @@ -15,7 +15,7 @@
15 15 <a-button type="primary" icon="minus">删除</a-button>
16 16 </a-popconfirm>
17 17 <template v-if="showClearSelectButton">
18   - <span class="gap"/>
  18 + <span class="gap"></span>
19 19 <a-button icon="delete" @click="handleClickClearSelect">清空选择</a-button>
20 20 </template>
21 21 </template>
... ... @@ -282,6 +282,7 @@
282 282 import { FormTypes, VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
283 283 import { cloneObject, randomString } from '@/utils/util'
284 284 import JDate from '@/components/jeecg/JDate'
  285 + import { initDictOptions } from '@/components/dict/JDictSelectUtil'
285 286  
286 287 // 行高,需要在实例加载完成前用到
287 288 let rowHeight = 61
... ... @@ -454,7 +455,7 @@
454 455 },
455 456 // 侦听器
456 457 watch: {
457   - dataSource: function(newValue) {
  458 + dataSource: function (newValue) {
458 459 this.initialize()
459 460  
460 461 let rows = []
... ... @@ -556,6 +557,9 @@
556 557 return {}
557 558 })
558 559 }
  560 + if (column.dictCode) {
  561 + this._loadDictConcatToOptions(column)
  562 + }
559 563 }
560 564 })
561 565 }
... ... @@ -575,15 +579,15 @@
575 579  
576 580 let vm = this
577 581 /** 监听滚动条事件 */
578   - this.el.inputTable.onscroll = function(event) {
  582 + this.el.inputTable.onscroll = function (event) {
579 583 vm.syncScrollBar(event.target.scrollLeft)
580 584 }
581   - this.el.tbody.onscroll = function(event) {
  585 + this.el.tbody.onscroll = function (event) {
582 586 // vm.recalcTrHiddenItem(event.target.scrollTop)
583 587 }
584 588  
585 589 let { thead, scrollView } = this.$refs
586   - scrollView.onscroll = function(event) {
  590 + scrollView.onscroll = function (event) {
587 591  
588 592 // console.log(event.target.scrollTop, ' - ', event.target.scrollLeft)
589 593  
... ... @@ -1065,7 +1069,32 @@
1065 1069 } else // 使用 else-if 是为了防止一个 rule 中出现两个规则
1066 1070 // 验证规则:正则表达式
1067 1071 if (!!rule.pattern && !isNull) {
1068   - passed = new RegExp(rule.pattern).test(value)
  1072 +
  1073 + // 兼容 online 的规则
  1074 + let foo = [
  1075 + { title: '唯一校验', value: 'only', pattern: null },
  1076 + { title: '6到16位数字', value: 'n6-16', pattern: /\d{6,18}/ },
  1077 + { title: '6到16位任意字符', value: '*6-16', pattern: /^.{6,16}$/ },
  1078 + { title: '网址', value: 'url', pattern: /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/ },
  1079 + { title: '电子邮件', value: 'e', pattern: /^([\w]+\.*)([\w]+)@[\w]+\.\w{3}(\.\w{2}|)$/ },
  1080 + { title: '手机号码', value: 'm', pattern: /^1[3456789]\d{9}$/ },
  1081 + { title: '邮政编码', value: 'p', pattern: /^[1-9]\d{5}$/ },
  1082 + { title: '字母', value: 's', pattern: /^[A-Z|a-z]+$/ },
  1083 + { title: '数字', value: 'n', pattern: /^-?\d+\.?\d*$/ },
  1084 + { title: '整数', value: 'z', pattern: /^[1-9]\d*$/ },
  1085 + { title: '非空', value: '*', pattern: /^.+$/ },
  1086 + { title: '6到18位字符串', value: 's6-18', pattern: /^.{6,18}$/ },
  1087 + { title: '金额', value: 'money', pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/ },
  1088 + ]
  1089 + let flag = false
  1090 + for (let item of foo) {
  1091 + if (rule.pattern === item.value && item.pattern) {
  1092 + passed = new RegExp(item.pattern).test(value)
  1093 + flag = true
  1094 + break
  1095 + }
  1096 + }
  1097 + if (!flag) passed = new RegExp(rule.pattern).test(value)
1069 1098 }
1070 1099 // 如果没有通过验证,则跳出循环。如果通过了验证,则继续验证下一条规则
1071 1100 if (!passed) {
... ... @@ -1227,7 +1256,7 @@
1227 1256 change = false
1228 1257 value = replace
1229 1258 target.value = replace
1230   - if (typeof selectionStart === 'number') {
  1259 + if (typeof selectionStart === 'number') {
1231 1260 target.selectionStart = selectionStart - 1
1232 1261 target.selectionEnd = selectionStart - 1
1233 1262 }
... ... @@ -1322,6 +1351,19 @@
1322 1351 this.uploadValues[id] = null
1323 1352 },
1324 1353  
  1354 + /** 加载数据字典并合并到 options */
  1355 + _loadDictConcatToOptions(column) {
  1356 + initDictOptions(column.dictCode).then((res) => {
  1357 + if (res.success) {
  1358 + column.options = (column.options || []).concat(res.result)
  1359 + } else {
  1360 + console.group(`JEditableTable 查询字典(${column.dictCode})发生异常`)
  1361 + console.log(res.message)
  1362 + console.groupEnd()
  1363 + }
  1364 + })
  1365 + },
  1366 +
1325 1367 /* --- common function end --- */
1326 1368  
1327 1369 /* --- 以下是辅助方法,多用于动态构造页面中的数据 --- */
... ... @@ -1483,6 +1525,7 @@
1483 1525 border-bottom: @border;
1484 1526  
1485 1527 /** 隐藏thead的滑块 */
  1528 +
1486 1529 &::-webkit-scrollbar-thumb {
1487 1530 box-shadow: none !important;
1488 1531 background-color: transparent !important;
... ... @@ -1588,15 +1631,19 @@
1588 1631 }
1589 1632  
1590 1633 /* 设置placeholder的颜色 */
  1634 +
1591 1635 &::-webkit-input-placeholder { /* WebKit browsers */
1592 1636 color: #ccc;
1593 1637 }
  1638 +
1594 1639 &:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
1595 1640 color: #ccc;
1596 1641 }
  1642 +
1597 1643 &::-moz-placeholder { /* Mozilla Firefox 19+ */
1598 1644 color: #ccc;
1599 1645 }
  1646 +
1600 1647 &:-ms-input-placeholder { /* Internet Explorer 10+ */
1601 1648 color: #ccc;
1602 1649 }
... ... @@ -1615,16 +1662,21 @@
1615 1662 .thead, .thead .tr, .scroll-view {
1616 1663 @scrollBarSize: 6px;
1617 1664 /* 定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/
  1665 +
1618 1666 &::-webkit-scrollbar {
1619 1667 width: @scrollBarSize;
1620 1668 height: @scrollBarSize;
1621 1669 background-color: transparent;
1622 1670 }
  1671 +
1623 1672 /* 定义滚动条轨道 */
  1673 +
1624 1674 &::-webkit-scrollbar-track {
1625 1675 background-color: #f0f0f0;
1626 1676 }
  1677 +
1627 1678 /* 定义滑块 */
  1679 +
1628 1680 &::-webkit-scrollbar-thumb {
1629 1681 background-color: #eee;
1630 1682 box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
... ...
ant-design-vue-jeecg/src/components/jeecg/README_JEditableTable.md
... ... @@ -36,10 +36,11 @@
36 36  
37 37 #### 当 type=select 时所需的参数
38 38  
39   -| 参数 | 类型 | 必填 | 说明 |
40   -|------------|---------|------|--------------------------------------|
41   -| options | array | ✔️ | 下拉选项列表,详见下表 |
42   -| allowInput | boolean | | 是否允许用户输入内容,并创建新的内容 |
  39 +| 参数 | 类型 | 必填 | 说明 |
  40 +|------------|---------|------|----------------------------------------------------|
  41 +| options | array | ✔️ | 下拉选项列表,详见下表 |
  42 +| allowInput | boolean | | 是否允许用户输入内容,并创建新的内容 |
  43 +| dictCode | String | | 数据字典Code,若options也有值,则拼接再options后面 |
43 44  
44 45 ##### options 所需参数
45 46  
... ...
ant-design-vue-jeecg/src/views/jeecg/JeecgEditableTableExample.vue
... ... @@ -110,6 +110,16 @@
110 110 validateRules: [{ required: true, message: '请选择${title}' }]
111 111 },
112 112 {
  113 + title: '性别(字典)',
  114 + key: 'sex_dict',
  115 + width: '300px',
  116 + type: FormTypes.select,
  117 + options: [],
  118 + dictCode: 'sex',
  119 + placeholder: '请选择${title}',
  120 + validateRules: [{ required: true, message: '请选择${title}' }]
  121 + },
  122 + {
113 123 title: '多选测试',
114 124 key: 'multipleSelect',
115 125 // width: '18%',
... ...