7c5ed8bfc4efb9db985815468941a4c5c4f612db.svn-base 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <template>
  2. <j-vxe-table
  3. ref="vTable"
  4. toolbar
  5. row-number
  6. row-selection
  7. drag-sort
  8. keep-source
  9. :height="580"
  10. :loading="loading"
  11. :dataSource="dataSource"
  12. :columns="columns"
  13. style="margin-top: 8px;"
  14. @valueChange="handleValueChange"
  15. >
  16. <template v-slot:toolbarSuffix>
  17. <a-button @click="handleTableCheck">表单验证</a-button>
  18. <a-tooltip placement="top" title="获取值,忽略表单验证" :autoAdjustOverflow="true">
  19. <a-button @click="handleTableGet">获取值</a-button>
  20. </a-tooltip>
  21. <a-tooltip placement="top" title="模拟加载1000条数据" :autoAdjustOverflow="true">
  22. <a-button @click="handleTableSet">设置值</a-button>
  23. </a-tooltip>
  24. </template>
  25. <template v-slot:action="props">
  26. <a @click="handleCK(props)">查看</a>
  27. <a-divider type="vertical"/>
  28. <a-popconfirm title="确定删除吗?" @confirm="handleDL(props)">
  29. <a>删除</a>
  30. </a-popconfirm>
  31. </template>
  32. </j-vxe-table>
  33. </template>
  34. <script>
  35. import moment from 'moment'
  36. import { pushIfNotExist, randomNumber, randomUUID } from '@/utils/util'
  37. import { JVXETypes } from '@/components/jeecg/JVxeTable'
  38. export default {
  39. name: 'JVxeDemo1',
  40. data() {
  41. return {
  42. loading: false,
  43. columns: [
  44. {
  45. title: '不可编辑',
  46. key: 'normal',
  47. type: JVXETypes.normal,
  48. width: '180px',
  49. fixed: 'left',
  50. defaultValue: 'normal-new',
  51. },
  52. {
  53. title: '单行文本',
  54. key: 'input',
  55. type: JVXETypes.input,
  56. width: '180px',
  57. defaultValue: '',
  58. placeholder: '请输入${title}',
  59. validateRules: [
  60. {
  61. required: true, // 必填
  62. message: '请输入${title}' // 显示的文本
  63. },
  64. {
  65. pattern: /^[a-z|A-Z][a-z|A-Z\d_-]*$/, // 正则
  66. message: '${title}必须以字母开头,可包含数字、下划线、横杠'
  67. },
  68. {
  69. unique: true,
  70. message: '${title}不能重复'
  71. },
  72. {
  73. handler({cellValue, row, column}, callback, target) {
  74. // cellValue 当前校验的值
  75. // callback(flag, message) 方法必须执行且只能执行一次
  76. // flag = 是否通过了校验,不填写或者填写 null 代表不进行任何操作
  77. // message = 提示的类型,默认使用配置的 message
  78. // target 行编辑的实例对象
  79. if (cellValue === 'abc') {
  80. callback(false, '${title}不能是abc') // false = 未通过校验
  81. } else {
  82. callback(true) // true = 通过验证
  83. }
  84. },
  85. message: '${title}默认提示'
  86. }
  87. ]
  88. },
  89. {
  90. title: '多行文本',
  91. key: 'textarea',
  92. type: JVXETypes.textarea,
  93. width: '200px',
  94. },
  95. {
  96. title: '数字',
  97. key: 'number',
  98. type: JVXETypes.inputNumber,
  99. width: '80px',
  100. defaultValue: 32,
  101. // 【统计列】sum = 求和、average = 平均值
  102. statistics: ['sum', 'average'],
  103. },
  104. {
  105. title: '下拉框',
  106. key: 'select',
  107. type: JVXETypes.select,
  108. width: '180px',
  109. // 下拉选项
  110. options: [
  111. {title: 'String', value: 'string'},
  112. {title: 'Integer', value: 'int'},
  113. {title: 'Double', value: 'double'},
  114. {title: 'Boolean', value: 'boolean'}
  115. ],
  116. allowInput: true,
  117. placeholder: '请选择'
  118. },
  119. {
  120. title: '下拉框_字典',
  121. key: 'select_dict',
  122. type: JVXETypes.select,
  123. width: '180px',
  124. options: [],
  125. dictCode: 'sex',
  126. placeholder: '请选择',
  127. },
  128. {
  129. title: '下拉框_多选',
  130. key: 'select_multiple',
  131. type: JVXETypes.selectMultiple,
  132. width: '205px',
  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. },
  143. {
  144. title: '下拉框_搜索',
  145. key: 'select_search',
  146. type: JVXETypes.selectSearch,
  147. width: '180px',
  148. options: [
  149. {title: 'String', value: 'string'},
  150. {title: 'Integer', value: 'int'},
  151. {title: 'Double', value: 'double'},
  152. {title: 'Boolean', value: 'boolean'}
  153. ],
  154. },
  155. {
  156. title: '日期时间',
  157. key: 'datetime',
  158. type: JVXETypes.datetime,
  159. width: '200px',
  160. defaultValue: '2019-4-30 14:52:22',
  161. placeholder: '请选择',
  162. },
  163. {
  164. title: '复选框',
  165. key: 'checkbox',
  166. type: JVXETypes.checkbox,
  167. width: '100px',
  168. customValue: ['Y', 'N'], // true ,false
  169. defaultChecked: false,
  170. },
  171. {
  172. title: '操作',
  173. key: 'action',
  174. type: JVXETypes.slot,
  175. fixed: 'right',
  176. minWidth: '100px',
  177. align: 'center',
  178. slotName: 'action',
  179. }
  180. ],
  181. dataSource: [],
  182. }
  183. },
  184. created() {
  185. this.randomPage(0, 20, true)
  186. },
  187. methods: {
  188. handleCK(props) {
  189. this.$message.success('请在控制台查看输出')
  190. // 参数介绍:
  191. // props.value 当前单元格的值
  192. // props.row 当前行的数据
  193. // props.rowId 当前行ID
  194. // props.rowIndex 当前行下标
  195. // props.column 当前列的配置
  196. // props.columnIndex 当前列下标
  197. // props.$table vxe实例,可以调用vxe内置方法
  198. // props.target JVXE实例,可以调用JVXE内置方法
  199. // props.caseId JVXE实例唯一ID
  200. // props.scrolling 是否正在滚动
  201. // props.triggerChange 触发change事件,用于更改slot的值
  202. console.log('查看: ', {props})
  203. },
  204. handleDL(props) {
  205. // 调用删除方法
  206. props.target.removeRows(props.row)
  207. },
  208. handleValueChange(event) {
  209. console.log('handleValueChange.event: ', event)
  210. },
  211. /** 表单验证 */
  212. handleTableCheck() {
  213. this.$refs.vTable.validateTable().then(errMap => {
  214. if (errMap) {
  215. console.log('表单验证未通过:', {errMap})
  216. this.$message.error('验证未通过,请在控制台查看详细')
  217. } else {
  218. this.$message.success('验证通过')
  219. }
  220. })
  221. },
  222. /** 获取值,忽略表单验证 */
  223. handleTableGet() {
  224. const values = this.$refs.vTable.getTableData()
  225. console.log('获取值:', {values})
  226. this.$message.success('获取值成功,请看控制台输出')
  227. },
  228. /** 模拟加载1000条数据 */
  229. handleTableSet() {
  230. this.randomPage(1, 1000, true)
  231. },
  232. /* 随机生成数据 */
  233. randomPage(current, pageSize, loading = false) {
  234. if (loading) {
  235. this.loading = true
  236. }
  237. let randomDatetime = () => {
  238. let time = randomNumber(1000, 9999999999999)
  239. return moment(new Date(time)).format('YYYY-MM-DD HH:mm:ss')
  240. }
  241. let limit = (current - 1) * pageSize
  242. let options = ['string', 'int', 'double', 'boolean']
  243. let begin = Date.now()
  244. let values = []
  245. for (let i = 0; i < pageSize; i++) {
  246. values.push({
  247. id: randomUUID(),
  248. normal: `normal-${(limit + i) + 1}`,
  249. input: `text-${(limit + i) + 1}`,
  250. textarea: `textarea-${(limit + i) + 1}`,
  251. number: randomNumber(0, 233),
  252. select: options[randomNumber(0, 3)],
  253. select_dict: randomNumber(1, 2).toString(),
  254. select_multiple: (() => {
  255. let length = randomNumber(1, 4)
  256. let arr = []
  257. for (let j = 0; j < length; j++) {
  258. pushIfNotExist(arr, options[randomNumber(0, 3)])
  259. }
  260. return arr
  261. })(),
  262. select_search: options[randomNumber(0, 3)],
  263. datetime: randomDatetime(),
  264. checkbox: ['Y', 'N'][randomNumber(0, 1)]
  265. })
  266. }
  267. this.dataSource = values
  268. let end = Date.now()
  269. let diff = end - begin
  270. if (loading && diff < pageSize) {
  271. setTimeout(() => {
  272. this.loading = false
  273. }, pageSize - diff)
  274. }
  275. }
  276. }
  277. }
  278. </script>
  279. <style scoped>
  280. </style>