Commit cbcdf24c7bf83a419566ce719a4ebf52ab545f0b

Authored by zhangdaihao
1 parent d81244b5

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

ant-design-vue-jeecg/src/components/jeecg/JEditableTable.vue
1 <!-- JEditableTable --> 1 <!-- JEditableTable -->
2 -<!-- @version 1.4.1 --> 2 +<!-- @version 1.4.2 -->
3 <!-- @author sjlei --> 3 <!-- @author sjlei -->
4 <template> 4 <template>
5 <a-spin :spinning="loading"> 5 <a-spin :spinning="loading">
@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 <!-- 操作按钮 --> 7 <!-- 操作按钮 -->
8 <div v-if="actionButton" class="action-button"> 8 <div v-if="actionButton" class="action-button">
9 <a-button type="primary" icon="plus" @click="handleClickAdd">新增</a-button> 9 <a-button type="primary" icon="plus" @click="handleClickAdd">新增</a-button>
10 - <span class="gap"/> 10 + <span class="gap"></span>
11 <template v-if="selectedRowIds.length>0"> 11 <template v-if="selectedRowIds.length>0">
12 <a-popconfirm 12 <a-popconfirm
13 :title="`确定要删除这 ${selectedRowIds.length} 项吗?`" 13 :title="`确定要删除这 ${selectedRowIds.length} 项吗?`"
@@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
15 <a-button type="primary" icon="minus">删除</a-button> 15 <a-button type="primary" icon="minus">删除</a-button>
16 </a-popconfirm> 16 </a-popconfirm>
17 <template v-if="showClearSelectButton"> 17 <template v-if="showClearSelectButton">
18 - <span class="gap"/> 18 + <span class="gap"></span>
19 <a-button icon="delete" @click="handleClickClearSelect">清空选择</a-button> 19 <a-button icon="delete" @click="handleClickClearSelect">清空选择</a-button>
20 </template> 20 </template>
21 </template> 21 </template>
@@ -282,6 +282,7 @@ @@ -282,6 +282,7 @@
282 import { FormTypes, VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil' 282 import { FormTypes, VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
283 import { cloneObject, randomString } from '@/utils/util' 283 import { cloneObject, randomString } from '@/utils/util'
284 import JDate from '@/components/jeecg/JDate' 284 import JDate from '@/components/jeecg/JDate'
  285 + import { initDictOptions } from '@/components/dict/JDictSelectUtil'
285 286
286 // 行高,需要在实例加载完成前用到 287 // 行高,需要在实例加载完成前用到
287 let rowHeight = 61 288 let rowHeight = 61
@@ -454,7 +455,7 @@ @@ -454,7 +455,7 @@
454 }, 455 },
455 // 侦听器 456 // 侦听器
456 watch: { 457 watch: {
457 - dataSource: function(newValue) { 458 + dataSource: function (newValue) {
458 this.initialize() 459 this.initialize()
459 460
460 let rows = [] 461 let rows = []
@@ -556,6 +557,9 @@ @@ -556,6 +557,9 @@
556 return {} 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,15 +579,15 @@
575 579
576 let vm = this 580 let vm = this
577 /** 监听滚动条事件 */ 581 /** 监听滚动条事件 */
578 - this.el.inputTable.onscroll = function(event) { 582 + this.el.inputTable.onscroll = function (event) {
579 vm.syncScrollBar(event.target.scrollLeft) 583 vm.syncScrollBar(event.target.scrollLeft)
580 } 584 }
581 - this.el.tbody.onscroll = function(event) { 585 + this.el.tbody.onscroll = function (event) {
582 // vm.recalcTrHiddenItem(event.target.scrollTop) 586 // vm.recalcTrHiddenItem(event.target.scrollTop)
583 } 587 }
584 588
585 let { thead, scrollView } = this.$refs 589 let { thead, scrollView } = this.$refs
586 - scrollView.onscroll = function(event) { 590 + scrollView.onscroll = function (event) {
587 591
588 // console.log(event.target.scrollTop, ' - ', event.target.scrollLeft) 592 // console.log(event.target.scrollTop, ' - ', event.target.scrollLeft)
589 593
@@ -1065,7 +1069,32 @@ @@ -1065,7 +1069,32 @@
1065 } else // 使用 else-if 是为了防止一个 rule 中出现两个规则 1069 } else // 使用 else-if 是为了防止一个 rule 中出现两个规则
1066 // 验证规则:正则表达式 1070 // 验证规则:正则表达式
1067 if (!!rule.pattern && !isNull) { 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 if (!passed) { 1100 if (!passed) {
@@ -1227,7 +1256,7 @@ @@ -1227,7 +1256,7 @@
1227 change = false 1256 change = false
1228 value = replace 1257 value = replace
1229 target.value = replace 1258 target.value = replace
1230 - if (typeof selectionStart === 'number') { 1259 + if (typeof selectionStart === 'number') {
1231 target.selectionStart = selectionStart - 1 1260 target.selectionStart = selectionStart - 1
1232 target.selectionEnd = selectionStart - 1 1261 target.selectionEnd = selectionStart - 1
1233 } 1262 }
@@ -1322,6 +1351,19 @@ @@ -1322,6 +1351,19 @@
1322 this.uploadValues[id] = null 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 /* --- common function end --- */ 1367 /* --- common function end --- */
1326 1368
1327 /* --- 以下是辅助方法,多用于动态构造页面中的数据 --- */ 1369 /* --- 以下是辅助方法,多用于动态构造页面中的数据 --- */
@@ -1483,6 +1525,7 @@ @@ -1483,6 +1525,7 @@
1483 border-bottom: @border; 1525 border-bottom: @border;
1484 1526
1485 /** 隐藏thead的滑块 */ 1527 /** 隐藏thead的滑块 */
  1528 +
1486 &::-webkit-scrollbar-thumb { 1529 &::-webkit-scrollbar-thumb {
1487 box-shadow: none !important; 1530 box-shadow: none !important;
1488 background-color: transparent !important; 1531 background-color: transparent !important;
@@ -1588,15 +1631,19 @@ @@ -1588,15 +1631,19 @@
1588 } 1631 }
1589 1632
1590 /* 设置placeholder的颜色 */ 1633 /* 设置placeholder的颜色 */
  1634 +
1591 &::-webkit-input-placeholder { /* WebKit browsers */ 1635 &::-webkit-input-placeholder { /* WebKit browsers */
1592 color: #ccc; 1636 color: #ccc;
1593 } 1637 }
  1638 +
1594 &:-moz-placeholder { /* Mozilla Firefox 4 to 18 */ 1639 &:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
1595 color: #ccc; 1640 color: #ccc;
1596 } 1641 }
  1642 +
1597 &::-moz-placeholder { /* Mozilla Firefox 19+ */ 1643 &::-moz-placeholder { /* Mozilla Firefox 19+ */
1598 color: #ccc; 1644 color: #ccc;
1599 } 1645 }
  1646 +
1600 &:-ms-input-placeholder { /* Internet Explorer 10+ */ 1647 &:-ms-input-placeholder { /* Internet Explorer 10+ */
1601 color: #ccc; 1648 color: #ccc;
1602 } 1649 }
@@ -1615,16 +1662,21 @@ @@ -1615,16 +1662,21 @@
1615 .thead, .thead .tr, .scroll-view { 1662 .thead, .thead .tr, .scroll-view {
1616 @scrollBarSize: 6px; 1663 @scrollBarSize: 6px;
1617 /* 定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/ 1664 /* 定义滚动条高宽及背景 高宽分别对应横竖滚动条的尺寸*/
  1665 +
1618 &::-webkit-scrollbar { 1666 &::-webkit-scrollbar {
1619 width: @scrollBarSize; 1667 width: @scrollBarSize;
1620 height: @scrollBarSize; 1668 height: @scrollBarSize;
1621 background-color: transparent; 1669 background-color: transparent;
1622 } 1670 }
  1671 +
1623 /* 定义滚动条轨道 */ 1672 /* 定义滚动条轨道 */
  1673 +
1624 &::-webkit-scrollbar-track { 1674 &::-webkit-scrollbar-track {
1625 background-color: #f0f0f0; 1675 background-color: #f0f0f0;
1626 } 1676 }
  1677 +
1627 /* 定义滑块 */ 1678 /* 定义滑块 */
  1679 +
1628 &::-webkit-scrollbar-thumb { 1680 &::-webkit-scrollbar-thumb {
1629 background-color: #eee; 1681 background-color: #eee;
1630 box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3); 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,10 +36,11 @@
36 36
37 #### 当 type=select 时所需的参数 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 ##### options 所需参数 45 ##### options 所需参数
45 46
ant-design-vue-jeecg/src/views/jeecg/JeecgEditableTableExample.vue
@@ -110,6 +110,16 @@ @@ -110,6 +110,16 @@
110 validateRules: [{ required: true, message: '请选择${title}' }] 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 title: '多选测试', 123 title: '多选测试',
114 key: 'multipleSelect', 124 key: 'multipleSelect',
115 // width: '18%', 125 // width: '18%',