e8f29b515c494883cb138b9c494e72e60812b0e2.svn-base 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <div>
  3. <a-button @click="handleTableCheck" type="primary">表单验证</a-button>
  4. <span style="padding-left:8px;"></span>
  5. <a-tooltip placement="top" title="获取值,忽略表单验证" :autoAdjustOverflow="true">
  6. <a-button @click="handleTableGet" type="primary">获取值</a-button>
  7. </a-tooltip>
  8. <span style="padding-left:8px;"></span>
  9. <a-tooltip placement="top" title="模拟加载1000条数据" :autoAdjustOverflow="true">
  10. <a-button @click="handleTableSet" type="primary">设置值</a-button>
  11. </a-tooltip>
  12. <j-editable-table
  13. ref="editableTable"
  14. :loading="loading"
  15. :columns="columns"
  16. :dataSource="dataSource"
  17. :rowNumber="true"
  18. :rowSelection="true"
  19. :actionButton="true"
  20. :dragSort="true"
  21. style="margin-top: 8px;"
  22. @selectRowChange="handleSelectRowChange">
  23. <template v-slot:action="props">
  24. <a @click="handleDelete(props)">删除</a>
  25. </template>
  26. </j-editable-table>
  27. </div>
  28. </template>
  29. <script>
  30. import moment from 'moment'
  31. import { FormTypes } from '@/utils/JEditableTableUtil'
  32. import { randomUUID, randomNumber } from '@/utils/util'
  33. import JEditableTable from '@/components/jeecg/JEditableTable'
  34. export default {
  35. name: 'DefaultTable',
  36. components: { JEditableTable },
  37. data() {
  38. return {
  39. loading: false,
  40. columns: [
  41. {
  42. title: '字段名称',
  43. key: 'dbFieldName',
  44. // width: '19%',
  45. width: '300px',
  46. type: FormTypes.input,
  47. defaultValue: '',
  48. placeholder: '请输入${title}',
  49. validateRules: [
  50. {
  51. required: true, // 必填
  52. message: '请输入${title}' // 显示的文本
  53. },
  54. {
  55. pattern: /^[a-z|A-Z][a-z|A-Z\d_-]{0,}$/, // 正则
  56. message: '${title}必须以字母开头,可包含数字、下划线、横杠'
  57. },
  58. {
  59. unique: true,
  60. message: '${title}不能重复'
  61. },
  62. {
  63. handler(type, value, row, column, callback, target) {
  64. // type 触发校验的类型(input、change、blur)
  65. // value 当前校验的值
  66. // callback(flag, message) 方法必须执行且只能执行一次
  67. // flag = 是否通过了校验,不填写或者填写 null 代表不进行任何操作
  68. // message = 提示的类型,默认使用配置的 message
  69. // target 行编辑的实例对象
  70. if (type === 'blur') {
  71. if (value === 'abc') {
  72. callback(false, '${title}不能是abc') // false = 未通过校验
  73. } else {
  74. callback(true) // true = 通过验证
  75. }
  76. } else {
  77. callback(true) // 不填写或者填写 null 代表不进行任何操作
  78. }
  79. },
  80. message: '${title}默认提示'
  81. }
  82. ]
  83. },
  84. {
  85. title: '文件域',
  86. key: 'upload',
  87. type: FormTypes.upload,
  88. // width: '19%',
  89. width: '300px',
  90. placeholder: '点击上传',
  91. token: true,
  92. responseName: 'message',
  93. action: window._CONFIG['domianURL'] + '/sys/common/upload',
  94. data: {
  95. biz: 'temp',
  96. // 更多扩展参数
  97. },
  98. },
  99. {
  100. title: '字段类型',
  101. key: 'dbFieldType',
  102. // width: '18%',
  103. width: '300px',
  104. type: FormTypes.select,
  105. options: [ // 下拉选项
  106. { title: 'String', value: 'string' },
  107. { title: 'Integer', value: 'int' },
  108. { title: 'Double', value: 'double' },
  109. { title: 'Boolean', value: 'boolean' }
  110. ],
  111. allowInput: true,
  112. defaultValue: '',
  113. placeholder: '请选择${title}',
  114. validateRules: [{ required: true, message: '请选择${title}' }]
  115. },
  116. {
  117. title: '性别(字典)',
  118. key: 'sex_dict',
  119. width: '300px',
  120. type: FormTypes.select,
  121. options: [],
  122. dictCode: 'sex',
  123. placeholder: '请选择${title}',
  124. validateRules: [{ required: true, message: '请选择${title}' }]
  125. },
  126. {
  127. title: '多选测试',
  128. key: 'multipleSelect',
  129. // width: '18%',
  130. width: '300px',
  131. type: FormTypes.select,
  132. props: { 'mode': 'multiple' }, // 支持多选
  133. options: [
  134. { title: 'String', value: 'string' },
  135. { title: 'Integer', value: 'int' },
  136. { title: 'Double', value: 'double' },
  137. { title: 'Boolean', value: 'boolean' }
  138. ],
  139. defaultValue: ['int', 'boolean'], // 多个默认项
  140. // defaultValue: 'string,double,int', // 也可使用这种方式
  141. placeholder: '这里可以多选',
  142. validateRules: [{ required: true, message: '请选择${title}' }]
  143. },
  144. {
  145. title: '字段长度',
  146. key: 'dbLength',
  147. // width: '8%',
  148. width: '100px',
  149. type: FormTypes.inputNumber,
  150. defaultValue: 32,
  151. placeholder: '${title}',
  152. // 是否是统计列,只有 inputNumber 才能设置统计列
  153. statistics: true,
  154. validateRules: [{ required: true, message: '请输入${title}' }]
  155. },
  156. {
  157. title: '日期',
  158. key: 'datetime',
  159. // width: '22%',
  160. width: '320px',
  161. type: FormTypes.datetime,
  162. defaultValue: '2019-4-30 14:52:22',
  163. placeholder: '请选择${title}',
  164. validateRules: [{ required: true, message: '请选择${title}' }]
  165. },
  166. {
  167. title: '数字',
  168. key: 'money',
  169. width: '320px',
  170. type: FormTypes.inputNumber,
  171. defaultValue: '100.32',
  172. placeholder: '请选择${title}',
  173. validateRules: [{ required: true, message: '请选择${title}' }]
  174. },
  175. {
  176. title: '可以为空',
  177. key: 'isNull',
  178. // width: '8%',
  179. width: '100px',
  180. type: FormTypes.checkbox,
  181. customValue: ['Y', 'N'], // true ,false
  182. defaultChecked: false
  183. },
  184. {
  185. type: FormTypes.popup,
  186. key: 'popup',
  187. title: 'JPopup',
  188. width: '180px',
  189. popupCode: 'demo',
  190. field: 'name',
  191. orgFields: 'name',
  192. destFields: 'name'
  193. },
  194. {
  195. title: '操作',
  196. key: 'action',
  197. // width: '8%',
  198. width: '100px',
  199. type: FormTypes.slot,
  200. slotName: 'action',
  201. }
  202. ],
  203. dataSource: [],
  204. selectedRowIds: []
  205. }
  206. },
  207. mounted() {
  208. this.randomData(23, false)
  209. },
  210. methods: {
  211. /** 表单验证 */
  212. handleTableCheck() {
  213. this.$refs.editableTable.getValues((error) => {
  214. if (error === 0) {
  215. this.$message.success('验证通过')
  216. } else {
  217. this.$message.error('验证未通过')
  218. }
  219. })
  220. },
  221. /** 获取值,忽略表单验证 */
  222. handleTableGet() {
  223. this.$refs.editableTable.getValues((error, values) => {
  224. console.log('values:', values)
  225. }, false)
  226. console.log('deleteIds:', this.$refs.editableTable.getDeleteIds())
  227. this.$message.info('获取值成功,请看控制台输出')
  228. },
  229. /** 模拟加载1000条数据 */
  230. handleTableSet() {
  231. this.randomData(1000, true)
  232. },
  233. handleSelectRowChange(selectedRowIds) {
  234. this.selectedRowIds = selectedRowIds
  235. },
  236. /* 随机生成数据 */
  237. randomData(size, loading = false) {
  238. if (loading) {
  239. this.loading = true
  240. }
  241. let randomDatetime = () => {
  242. let time = parseInt(randomNumber(1000, 9999999999999))
  243. return moment(new Date(time)).format('YYYY-MM-DD HH:mm:ss')
  244. }
  245. let begin = Date.now()
  246. let values = []
  247. for (let i = 0; i < size; i++) {
  248. values.push({
  249. id: randomUUID(),
  250. dbFieldName: `name_${i + 1}`,
  251. // dbFieldTxt: randomString(10),
  252. multipleSelect: ['string', ['int', 'double', 'boolean'][randomNumber(0, 2)]],
  253. dbFieldType: ['string', 'int', 'double', 'boolean'][randomNumber(0, 3)],
  254. dbLength: randomNumber(0, 233),
  255. datetime: randomDatetime(),
  256. isNull: ['Y', 'N'][randomNumber(0, 1)]
  257. })
  258. }
  259. this.dataSource = values
  260. let end = Date.now()
  261. let diff = end - begin
  262. if (loading && diff < size) {
  263. setTimeout(() => {
  264. this.loading = false
  265. }, size - diff)
  266. }
  267. },
  268. handleDelete(props) {
  269. let { rowId, target } = props
  270. target.removeRows(rowId)
  271. }
  272. }
  273. }
  274. </script>
  275. <style scoped>
  276. </style>