table.mdc 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117
  1. ---
  2. description: cl-table 组件示例
  3. globs: *.tsx, *.ts, *.vue
  4. ---
  5. ## 起步 示例
  6. ```vue
  7. <template>
  8. <div class="scope">
  9. <div class="h">
  10. <el-tag size="small" effect="dark" disable-transitions>base</el-tag>
  11. <span>起步</span>
  12. </div>
  13. <div class="c">
  14. <el-button @click="open">预览</el-button>
  15. <demo-code :files="['table/base.vue']" />
  16. <!-- 自定义表格组件 -->
  17. <cl-dialog v-model="visible" title="起步" width="80%">
  18. <!--【很重要】需要包含在 cl-crud 组件内 -->
  19. <cl-crud ref="Crud">
  20. <cl-row>
  21. <!-- 参数文档查看:https://element-plus.org/zh-CN/component/table.html#table-%E5%B1%9E%E6%80%A7 -->
  22. <cl-table ref="Table" stripe></cl-table>
  23. </cl-row>
  24. <cl-row>
  25. <cl-flex1 />
  26. <cl-pagination />
  27. </cl-row>
  28. </cl-crud>
  29. </cl-dialog>
  30. </div>
  31. <div class="f">
  32. <span class="date">2024-01-01</span>
  33. </div>
  34. </div>
  35. </template>
  36. <script setup lang="ts">
  37. import { useCrud, useTable } from '@cool-vue/crud';
  38. import { ref } from 'vue';
  39. import { useDict } from '/$/dict';
  40. const { dict } = useDict();
  41. // cl-crud 配置
  42. const Crud = useCrud(
  43. {
  44. // 测试数据,移步到 cl-crud 例子查看
  45. service: 'test'
  46. },
  47. app => {
  48. app.refresh();
  49. }
  50. );
  51. // cl-table 配置
  52. const Table = useTable({
  53. // 是否自动计算表格高度,表格高度等于减去上区域和下区域的高度
  54. //【很重要】在弹窗或者上级不确定高度中,设置 autoHeight: false,避免显示异常。也可以手动设置最大高度 maxHeight: 500
  55. autoHeight: false,
  56. // 右键菜单,移步到右键菜单示例中查看
  57. contextMenu: ['refresh'],
  58. // 列配置,点击 columns 查看描述
  59. // 更多配置查看 el-table-column 文档,https://element-plus.org/zh-CN/component/table.html#table-column-%E5%B1%9E%E6%80%A7
  60. columns: [
  61. {
  62. // 是否为多选框操作列
  63. type: 'selection'
  64. // 是否为序号列
  65. // type: "index"
  66. },
  67. {
  68. // 表头标题
  69. label: '姓名',
  70. // 绑定值
  71. prop: 'name',
  72. // 最小宽度
  73. minWidth: 140
  74. },
  75. {
  76. label: '手机号',
  77. prop: 'phone',
  78. minWidth: 140
  79. },
  80. {
  81. label: '工作',
  82. prop: 'occupation',
  83. // 字典匹配,移步到字典示例中查看
  84. dict: dict.get('occupation'),
  85. minWidth: 140
  86. },
  87. {
  88. label: '创建时间',
  89. prop: 'createTime',
  90. minWidth: 170,
  91. // 是否排序,desc, asc
  92. sortable: 'desc'
  93. }
  94. ]
  95. });
  96. const visible = ref(false);
  97. function open() {
  98. visible.value = true;
  99. }
  100. </script>
  101. ```
  102. ## 多级表头 示例
  103. ```vue
  104. <template>
  105. <div class="scope">
  106. <div class="h">
  107. <el-tag size="small" effect="dark" disable-transitions>children</el-tag>
  108. <span>多级表头</span>
  109. </div>
  110. <div class="c">
  111. <el-button @click="open">预览</el-button>
  112. <demo-code :files="['table/children.vue']" />
  113. <!-- 自定义表格组件 -->
  114. <cl-dialog v-model="visible" title="多级表头" width="80%">
  115. <cl-crud ref="Crud">
  116. <cl-row>
  117. <cl-table ref="Table" />
  118. </cl-row>
  119. <cl-row>
  120. <cl-flex1 />
  121. <cl-pagination />
  122. </cl-row>
  123. </cl-crud>
  124. </cl-dialog>
  125. </div>
  126. <div class="f">
  127. <span class="date">2024-01-01</span>
  128. </div>
  129. </div>
  130. </template>
  131. <script setup lang="ts">
  132. import { useCrud, useTable } from '@cool-vue/crud';
  133. import { ref } from 'vue';
  134. import { useDict } from '/$/dict';
  135. const { dict } = useDict();
  136. // cl-crud 配置
  137. const Crud = useCrud(
  138. {
  139. service: 'test'
  140. },
  141. app => {
  142. app.refresh();
  143. }
  144. );
  145. // cl-table 配置
  146. const Table = useTable({
  147. autoHeight: false,
  148. contextMenu: ['refresh'],
  149. columns: [
  150. {
  151. label: '用户信息',
  152. prop: 'baseInfo',
  153. minWidth: 250,
  154. // 配置 children 参数
  155. children: [
  156. {
  157. label: '姓名',
  158. prop: 'name',
  159. minWidth: 140
  160. },
  161. {
  162. label: '手机号',
  163. prop: 'phone',
  164. minWidth: 140
  165. }
  166. ]
  167. },
  168. {
  169. label: '工作',
  170. prop: 'occupation',
  171. dict: dict.get('occupation'),
  172. minWidth: 140
  173. },
  174. {
  175. label: '创建时间',
  176. prop: 'createTime',
  177. minWidth: 170,
  178. sortable: 'desc'
  179. }
  180. ]
  181. });
  182. const visible = ref(false);
  183. function open() {
  184. visible.value = true;
  185. }
  186. </script>
  187. ```
  188. ## 自定义列展示 示例
  189. ```vue
  190. <template>
  191. <div class="scope">
  192. <div class="h">
  193. <el-tag size="small" effect="dark" disable-transitions>column-custom</el-tag>
  194. <span>自定义列展示</span>
  195. </div>
  196. <div class="c">
  197. <el-button @click="open">预览</el-button>
  198. <demo-code :files="['table/column-custom.vue']" />
  199. <!-- 自定义表格组件 -->
  200. <cl-dialog v-model="visible" title="自定义列展示" width="80%">
  201. <cl-crud ref="Crud">
  202. <cl-row>
  203. <!--【很重要】组件配置,设置为 Table 的 columns,也可以自定义 -->
  204. <cl-column-custom :columns="Table?.columns" />
  205. </cl-row>
  206. <cl-row>
  207. <cl-table ref="Table"></cl-table>
  208. </cl-row>
  209. <cl-row>
  210. <cl-flex1 />
  211. <cl-pagination />
  212. </cl-row>
  213. </cl-crud>
  214. </cl-dialog>
  215. </div>
  216. <div class="f">
  217. <span class="date">2024-01-01</span>
  218. </div>
  219. </div>
  220. </template>
  221. <script setup lang="ts">
  222. import { useCrud, useTable } from '@cool-vue/crud';
  223. import { ref } from 'vue';
  224. import { useDict } from '/$/dict';
  225. const { dict } = useDict();
  226. // cl-crud 配置
  227. const Crud = useCrud(
  228. {
  229. service: 'test'
  230. },
  231. app => {
  232. app.refresh();
  233. }
  234. );
  235. // cl-table 配置
  236. const Table = useTable({
  237. autoHeight: false,
  238. contextMenu: ['refresh'],
  239. columns: [
  240. {
  241. label: '姓名',
  242. prop: 'name',
  243. minWidth: 140
  244. },
  245. {
  246. label: '手机号',
  247. prop: 'phone',
  248. minWidth: 140
  249. },
  250. {
  251. label: '工作',
  252. prop: 'occupation',
  253. dict: dict.get('occupation'),
  254. minWidth: 140
  255. },
  256. {
  257. label: '状态',
  258. prop: 'status',
  259. dict: [
  260. {
  261. label: '启用',
  262. value: 1,
  263. type: 'success'
  264. },
  265. {
  266. label: '禁用',
  267. value: 0,
  268. type: 'danger'
  269. }
  270. ],
  271. minWidth: 140
  272. },
  273. {
  274. label: '创建时间',
  275. prop: 'createTime',
  276. minWidth: 170,
  277. sortable: 'desc'
  278. }
  279. ]
  280. });
  281. const visible = ref(false);
  282. function open() {
  283. visible.value = true;
  284. }
  285. </script>
  286. ```
  287. ## 组件渲染 示例
  288. ```vue
  289. <template>
  290. <div class="scope">
  291. <div class="h">
  292. <el-tag size="small" effect="dark" disable-transitions>component</el-tag>
  293. <span>组件渲染</span>
  294. </div>
  295. <div class="c">
  296. <el-button @click="open">预览</el-button>
  297. <demo-code :files="['table/component/index.vue']" />
  298. <!-- 自定义表格组件 -->
  299. <cl-dialog v-model="visible" title="组件渲染" width="80%">
  300. <cl-crud ref="Crud">
  301. <cl-row>
  302. <cl-table ref="Table"></cl-table>
  303. </cl-row>
  304. <cl-row>
  305. <cl-flex1 />
  306. <cl-pagination />
  307. </cl-row>
  308. </cl-crud>
  309. </cl-dialog>
  310. </div>
  311. <div class="f">
  312. <span class="date">2024-01-01</span>
  313. </div>
  314. </div>
  315. </template>
  316. <script setup lang="ts">
  317. import { useCrud, useTable } from '@cool-vue/crud';
  318. import { ref } from 'vue';
  319. import { useDict } from '/$/dict';
  320. import { ElMessage } from 'element-plus';
  321. import UserInfo from './user-info.vue';
  322. const { dict } = useDict();
  323. // cl-crud 配置
  324. const Crud = useCrud(
  325. {
  326. service: 'test'
  327. },
  328. app => {
  329. app.refresh();
  330. }
  331. );
  332. // cl-table 配置
  333. const Table = useTable({
  334. autoHeight: false,
  335. contextMenu: ['refresh'],
  336. columns: [
  337. {
  338. type: 'selection'
  339. },
  340. {
  341. label: '姓名',
  342. prop: 'name',
  343. minWidth: 140,
  344. //【很重要】组件实例方式渲染
  345. component: {
  346. vm: UserInfo
  347. }
  348. },
  349. {
  350. label: '手机号',
  351. prop: 'phone',
  352. minWidth: 140,
  353. //【很重要】组件名方式渲染
  354. component: {
  355. // 组件名,组件必须全局注册了
  356. name: 'el-input',
  357. // 传入参数
  358. props: {
  359. onChange(val) {
  360. ElMessage.info(val);
  361. }
  362. }
  363. }
  364. },
  365. {
  366. label: '工作',
  367. prop: 'occupation',
  368. dict: dict.get('occupation'),
  369. minWidth: 140
  370. },
  371. {
  372. label: '创建时间',
  373. prop: 'createTime',
  374. minWidth: 170,
  375. sortable: 'desc'
  376. }
  377. ]
  378. });
  379. const visible = ref(false);
  380. function open() {
  381. visible.value = true;
  382. }
  383. </script>
  384. ```
  385. ## user-info 示例
  386. ```vue
  387. <template>
  388. <div class="user-info">
  389. <cl-avatar :size="36" />
  390. <div class="det">
  391. <p>{{ scope?.name }}</p>
  392. </div>
  393. </div>
  394. </template>
  395. <!-- name 必须填写且唯一 -->
  396. <script setup lang="ts">
  397. defineOptions({
  398. name: 'user-info'
  399. });
  400. const props = defineProps({
  401. prop: String, // 列配置的 prop
  402. scope: null // 列数据
  403. });
  404. </script>
  405. <style lang="scss" scoped>
  406. .user-info {
  407. display: flex;
  408. align-items: center;
  409. .det {
  410. flex: 1;
  411. margin-left: 10px;
  412. text-align: left;
  413. }
  414. }
  415. </style>
  416. ```
  417. ## 右键菜单 示例
  418. ```vue
  419. <template>
  420. <div class="scope">
  421. <div class="h">
  422. <el-tag size="small" effect="dark" disable-transitions>context-menu</el-tag>
  423. <span>右键菜单</span>
  424. </div>
  425. <div class="c">
  426. <el-button @click="open">预览</el-button>
  427. <demo-code :files="['table/context-menu.vue']" />
  428. <!-- 自定义表格组件 -->
  429. <cl-dialog v-model="visible" title="右键菜单">
  430. <cl-crud ref="Crud">
  431. <cl-row>
  432. <cl-table ref="Table"></cl-table>
  433. </cl-row>
  434. <cl-row>
  435. <cl-flex1 />
  436. <cl-pagination />
  437. </cl-row>
  438. <!-- 新增、编辑 -->
  439. <cl-upsert ref="Upsert" />
  440. </cl-crud>
  441. </cl-dialog>
  442. </div>
  443. <div class="f">
  444. <span class="date">2024-01-01</span>
  445. </div>
  446. </div>
  447. </template>
  448. <script setup lang="ts">
  449. import { useCrud, useTable, useUpsert } from '@cool-vue/crud';
  450. import { ref } from 'vue';
  451. import { useDict } from '/$/dict';
  452. import { ElMessage } from 'element-plus';
  453. import { EditPen, MoreFilled } from '@element-plus/icons-vue';
  454. const { dict } = useDict();
  455. // cl-crud 配置
  456. const Crud = useCrud(
  457. {
  458. service: 'test'
  459. },
  460. app => {
  461. app.refresh();
  462. }
  463. );
  464. // cl-table 配置
  465. const Table = useTable({
  466. autoHeight: false,
  467. // 右键菜单配置,为 [] 时则不显示内容
  468. contextMenu: [
  469. 'refresh', // 刷新
  470. 'check', // 选择行
  471. 'edit', // 弹出编辑框
  472. 'delete', // 弹出删除提示
  473. 'info', // 弹出详情
  474. 'order-desc', // 使列倒序
  475. 'order-asc', // 使列升序
  476. {
  477. label: '禁用状态',
  478. disabled: true
  479. },
  480. {
  481. label: '带图标',
  482. prefixIcon: EditPen,
  483. suffixIcon: MoreFilled
  484. },
  485. {
  486. label: '超出隐藏,看我有很多字非常多',
  487. ellipsis: true
  488. },
  489. {
  490. label: '多层级',
  491. children: [
  492. {
  493. label: 'A',
  494. children: [
  495. {
  496. label: 'A-1',
  497. callback(done) {
  498. ElMessage.success('点击了A-1');
  499. done();
  500. }
  501. }
  502. ]
  503. },
  504. {
  505. label: 'B'
  506. },
  507. {
  508. label: 'C'
  509. }
  510. ]
  511. },
  512. // row 行数据
  513. // column 列属性
  514. // event 事件对象
  515. (row, column, event) => {
  516. // 必须返回一个对象
  517. return {
  518. label: '自定义2',
  519. callback(done) {
  520. ElMessage.info('获取中');
  521. setTimeout(() => {
  522. ElMessage.success('Ta 是' + row.name);
  523. // 关闭右键菜单,只有在用到 callback 方法时才需要
  524. done();
  525. }, 500);
  526. }
  527. };
  528. }
  529. ],
  530. columns: [
  531. {
  532. type: 'selection'
  533. },
  534. {
  535. label: '姓名',
  536. prop: 'name',
  537. minWidth: 140
  538. },
  539. {
  540. label: '手机号',
  541. prop: 'phone',
  542. minWidth: 140
  543. },
  544. {
  545. label: '工作',
  546. prop: 'occupation',
  547. dict: dict.get('occupation'),
  548. minWidth: 140
  549. },
  550. {
  551. label: '创建时间',
  552. prop: 'createTime',
  553. minWidth: 170,
  554. sortable: 'desc'
  555. }
  556. ]
  557. });
  558. // cl-upsert 配置,详细移步到 cl-upsert 示例查看
  559. const Upsert = useUpsert({
  560. items: [
  561. {
  562. label: '姓名',
  563. prop: 'name',
  564. component: {
  565. name: 'el-input'
  566. }
  567. },
  568. {
  569. label: '手机号',
  570. prop: 'phone',
  571. component: {
  572. name: 'el-input'
  573. }
  574. },
  575. {
  576. label: '工作',
  577. prop: 'occupation',
  578. component: {
  579. name: 'cl-select',
  580. props: {
  581. tree: true,
  582. checkStrictly: true,
  583. options: dict.get('occupation')
  584. }
  585. }
  586. }
  587. ]
  588. });
  589. const visible = ref(false);
  590. function open() {
  591. visible.value = true;
  592. }
  593. </script>
  594. ```
  595. ## 字典匹配 示例
  596. ```vue
  597. <template>
  598. <div class="scope">
  599. <div class="h">
  600. <el-tag size="small" effect="dark" disable-transitions>dict</el-tag>
  601. <span>字典匹配</span>
  602. </div>
  603. <div class="c">
  604. <el-button @click="open">预览</el-button>
  605. <demo-code :files="['table/dict.vue']" />
  606. <!-- 自定义表格组件 -->
  607. <cl-dialog v-model="visible" title="字典匹配" width="80%">
  608. <cl-crud ref="Crud">
  609. <cl-row>
  610. <cl-table ref="Table" />
  611. </cl-row>
  612. <cl-row>
  613. <cl-flex1 />
  614. <cl-pagination />
  615. </cl-row>
  616. </cl-crud>
  617. </cl-dialog>
  618. </div>
  619. <div class="f">
  620. <span class="date">2024-01-01</span>
  621. </div>
  622. </div>
  623. </template>
  624. <script setup lang="ts">
  625. import { useCrud, useTable } from '@cool-vue/crud';
  626. import { computed, reactive, ref } from 'vue';
  627. import { useDict } from '/$/dict';
  628. const { dict } = useDict();
  629. // cl-crud 配置
  630. const Crud = useCrud(
  631. {
  632. service: 'test'
  633. },
  634. app => {
  635. app.refresh();
  636. }
  637. );
  638. const options = reactive({
  639. occupation: [] as { label: string; value: any }[]
  640. });
  641. // cl-table 配置
  642. const Table = useTable({
  643. autoHeight: false,
  644. contextMenu: ['refresh'],
  645. columns: [
  646. {
  647. label: '姓名',
  648. prop: 'name',
  649. minWidth: 140
  650. },
  651. {
  652. label: '手机号',
  653. prop: 'phone',
  654. minWidth: 140
  655. },
  656. {
  657. label: '工作',
  658. prop: 'occupation',
  659. //【很重要】字典匹配
  660. // 使用字典模块的 get 方法绑定,菜单地址 /dict/list
  661. dict: dict.get('occupation'),
  662. // 是否使用不同颜色区分
  663. dictColor: true,
  664. minWidth: 140
  665. },
  666. {
  667. label: '等级',
  668. prop: 'occupation',
  669. //【很重要】动态匹配列表的情况,使用 computed
  670. dict: computed(() => options.occupation),
  671. minWidth: 140
  672. },
  673. {
  674. label: '状态',
  675. prop: 'status',
  676. // 自定义匹配列表
  677. dict: [
  678. {
  679. label: '启用',
  680. value: 1,
  681. type: 'success'
  682. },
  683. {
  684. label: '禁用',
  685. value: 0,
  686. type: 'danger'
  687. }
  688. ],
  689. minWidth: 140
  690. },
  691. {
  692. label: '创建时间',
  693. prop: 'createTime',
  694. minWidth: 170,
  695. sortable: 'desc'
  696. }
  697. ]
  698. });
  699. const visible = ref(false);
  700. function open() {
  701. visible.value = true;
  702. // 模拟接口获取数据
  703. setTimeout(() => {
  704. options.occupation = [
  705. {
  706. label: 'A',
  707. value: 0
  708. },
  709. {
  710. label: 'B',
  711. value: 1
  712. },
  713. {
  714. label: 'C',
  715. value: 2
  716. },
  717. {
  718. label: 'D',
  719. value: 3
  720. },
  721. {
  722. label: 'E',
  723. value: 4
  724. },
  725. {
  726. label: 'F',
  727. value: 5
  728. }
  729. ];
  730. }, 1500);
  731. }
  732. </script>
  733. ```
  734. ## 数据格式化 示例
  735. ```vue
  736. <template>
  737. <div class="scope">
  738. <div class="h">
  739. <el-tag size="small" effect="dark" disable-transitions>formatter</el-tag>
  740. <span>数据格式化</span>
  741. </div>
  742. <div class="c">
  743. <el-button @click="open">预览</el-button>
  744. <demo-code :files="['table/formatter.vue']" />
  745. <!-- 自定义表格组件 -->
  746. <cl-dialog v-model="visible" title="数据格式化" width="80%">
  747. <cl-crud ref="Crud">
  748. <cl-row>
  749. <cl-table ref="Table" />
  750. </cl-row>
  751. <cl-row>
  752. <cl-flex1 />
  753. <cl-pagination />
  754. </cl-row>
  755. </cl-crud>
  756. </cl-dialog>
  757. </div>
  758. <div class="f">
  759. <span class="date">2024-09-26</span>
  760. </div>
  761. </div>
  762. </template>
  763. <script setup lang="tsx">
  764. import { useCrud, useTable } from '@cool-vue/crud';
  765. import { ref } from 'vue';
  766. // cl-crud 配置
  767. const Crud = useCrud(
  768. {
  769. service: 'test'
  770. },
  771. app => {
  772. app.refresh();
  773. }
  774. );
  775. // cl-table 配置
  776. const Table = useTable({
  777. autoHeight: false,
  778. contextMenu: ['refresh'],
  779. columns: [
  780. {
  781. label: '姓名',
  782. prop: 'name',
  783. minWidth: 140
  784. },
  785. {
  786. label: '手机号',
  787. prop: 'phone',
  788. minWidth: 140,
  789. formatter(row) {
  790. return '📱' + row.phone;
  791. }
  792. },
  793. {
  794. label: '用户信息',
  795. minWidth: 200,
  796. // tsx 方式渲染
  797. // 【很重要】使用 tsx 语法时,script 的 lang 一定要设置为 tsx
  798. formatter(row) {
  799. // row 为当前行数据
  800. return (
  801. <el-row>
  802. <cl-avatar size={30} />
  803. <el-text style={{ marginLeft: '10px' }}>{row.name}</el-text>
  804. </el-row>
  805. );
  806. }
  807. },
  808. {
  809. label: '创建时间',
  810. prop: 'createTime',
  811. minWidth: 170,
  812. sortable: 'desc'
  813. }
  814. ]
  815. });
  816. const visible = ref(false);
  817. function open() {
  818. visible.value = true;
  819. }
  820. </script>
  821. ```
  822. ## 隐藏/显示 示例
  823. ```vue
  824. <template>
  825. <div class="scope">
  826. <div class="h">
  827. <el-tag size="small" effect="dark" disable-transitions>hidden</el-tag>
  828. <span>隐藏/显示</span>
  829. </div>
  830. <div class="c">
  831. <el-button @click="open">预览</el-button>
  832. <demo-code :files="['table/hidden.vue']" />
  833. <!-- 自定义表格组件 -->
  834. <cl-dialog v-model="visible" title="隐藏/显示" width="80%">
  835. <cl-crud ref="Crud">
  836. <!--配置一个 tab -->
  837. <el-tabs v-model="active">
  838. <el-tab-pane label="员工" name="user"></el-tab-pane>
  839. <el-tab-pane label="企业" name="company"></el-tab-pane>
  840. </el-tabs>
  841. <cl-row>
  842. <!-- 使用方法 showColumn 显示 -->
  843. <el-button @click="showColumn('account')">显示账号</el-button>
  844. <!-- 使用方法 hideColumn 隐藏 -->
  845. <el-button @click="hideColumn('account')">隐藏账号</el-button>
  846. </cl-row>
  847. <cl-row>
  848. <cl-table ref="Table"></cl-table>
  849. </cl-row>
  850. <cl-row>
  851. <cl-flex1 />
  852. <cl-pagination />
  853. </cl-row>
  854. </cl-crud>
  855. </cl-dialog>
  856. </div>
  857. <div class="f">
  858. <span class="date">2024-01-01</span>
  859. </div>
  860. </div>
  861. </template>
  862. <script setup lang="ts">
  863. import { useCrud, useTable } from '@cool-vue/crud';
  864. import { computed, ref } from 'vue';
  865. import { useDict } from '/$/dict';
  866. const { dict } = useDict();
  867. // cl-crud 配置
  868. const Crud = useCrud(
  869. {
  870. // 测试数据,移步到 cl-crud 例子查看
  871. service: 'test'
  872. },
  873. app => {
  874. app.refresh();
  875. }
  876. );
  877. const active = ref('user');
  878. // cl-table 配置
  879. const Table = useTable({
  880. autoHeight: false,
  881. contextMenu: ['refresh'],
  882. columns: [
  883. {
  884. label: 'ID',
  885. prop: 'id',
  886. minWidth: 140,
  887. //【很重要】配置 hidden 参数,格式为 boolean 或者 Vue.ComputedRef<boolean>
  888. hidden: computed(() => {
  889. return active.value != 'company';
  890. })
  891. },
  892. {
  893. label: '账号',
  894. prop: 'account',
  895. minWidth: 140,
  896. hidden: true // 默认 false
  897. },
  898. {
  899. label: '姓名',
  900. prop: 'name',
  901. minWidth: 140
  902. },
  903. {
  904. label: '手机号',
  905. prop: 'phone',
  906. minWidth: 140
  907. },
  908. {
  909. label: '工作',
  910. prop: 'occupation',
  911. dict: dict.get('occupation'),
  912. minWidth: 140
  913. },
  914. {
  915. label: '创建时间',
  916. prop: 'createTime',
  917. minWidth: 170,
  918. sortable: 'desc'
  919. }
  920. ]
  921. });
  922. const visible = ref(false);
  923. function open() {
  924. visible.value = true;
  925. }
  926. function showColumn(prop: string) {
  927. Table.value?.showColumn(prop);
  928. }
  929. function hideColumn(prop: string) {
  930. Table.value?.hideColumn(prop);
  931. }
  932. </script>
  933. ```
  934. ## 操作栏 示例
  935. ```vue
  936. <template>
  937. <div class="scope">
  938. <div class="h">
  939. <el-tag size="small" effect="dark" disable-transitions>op</el-tag>
  940. <span>操作栏</span>
  941. </div>
  942. <div class="c">
  943. <el-button @click="open">预览</el-button>
  944. <demo-code :files="['table/op.vue']" />
  945. <!-- 自定义表格组件 -->
  946. <cl-dialog v-model="visible" title="操作栏" width="80%">
  947. <cl-crud ref="Crud">
  948. <cl-row>
  949. <cl-table ref="Table">
  950. <!-- 插槽的渲染方式 #[component.name] -->
  951. <template #slot-btns="{ scope }">
  952. <el-button
  953. @click="
  954. () => {
  955. ElMessage.info(scope.row.name);
  956. }
  957. "
  958. >插槽按钮</el-button
  959. >
  960. </template>
  961. </cl-table>
  962. </cl-row>
  963. <cl-row>
  964. <cl-flex1 />
  965. <cl-pagination />
  966. </cl-row>
  967. <!-- 新增、编辑 -->
  968. <cl-upsert ref="Upsert" />
  969. </cl-crud>
  970. </cl-dialog>
  971. </div>
  972. <div class="f">
  973. <span class="date">2024-01-01</span>
  974. </div>
  975. </div>
  976. </template>
  977. <script setup lang="ts">
  978. import { useCrud, useTable, useUpsert } from '@cool-vue/crud';
  979. import { ref } from 'vue';
  980. import { useDict } from '/$/dict';
  981. import { ElMessage } from 'element-plus';
  982. const { dict } = useDict();
  983. // cl-crud 配置
  984. const Crud = useCrud(
  985. {
  986. service: 'test'
  987. },
  988. app => {
  989. app.refresh();
  990. }
  991. );
  992. // cl-table 配置
  993. const Table = useTable({
  994. autoHeight: false,
  995. contextMenu: ['refresh'],
  996. columns: [
  997. {
  998. label: '姓名',
  999. prop: 'name',
  1000. minWidth: 140
  1001. },
  1002. {
  1003. label: '手机号',
  1004. prop: 'phone',
  1005. minWidth: 140
  1006. },
  1007. {
  1008. label: '工作',
  1009. prop: 'occupation',
  1010. dict: dict.get('occupation'),
  1011. minWidth: 140
  1012. },
  1013. {
  1014. label: '创建时间',
  1015. prop: 'createTime',
  1016. minWidth: 170,
  1017. sortable: 'desc'
  1018. },
  1019. {
  1020. //【很重要】type 必须是 op
  1021. type: 'op',
  1022. width: 410, // 宽度
  1023. //【很重要】操作按钮配置,edit 和 info 必须搭配 cl-upsert 实现
  1024. // edit 编辑,预先获取 service 的 info 接口数据,并带入 cl-upsert 的表单值中
  1025. // info 详情,cl-upsert 内的组件全部传入 disabled 参数
  1026. // delete 删除,调用 service 的 delete 接口删除行数据
  1027. buttons: [
  1028. {
  1029. label: '编辑',
  1030. type: 'primary',
  1031. onClick({ scope }) {
  1032. ElMessage.info(scope.row.name);
  1033. }
  1034. },
  1035. {
  1036. label: '删除',
  1037. type: 'danger',
  1038. onClick({ scope }) {
  1039. ElMessage.info(scope.row.name);
  1040. }
  1041. },
  1042. {
  1043. label: '更多',
  1044. type: 'success',
  1045. children: [
  1046. {
  1047. label: '查看',
  1048. onClick({ scope }) {
  1049. ElMessage.info(scope.row.name);
  1050. }
  1051. },
  1052. {
  1053. label: '禁用',
  1054. onClick({ scope }) {
  1055. ElMessage.info(scope.row.name);
  1056. }
  1057. }
  1058. ]
  1059. },
  1060. {
  1061. name: 'slot-btns'
  1062. }
  1063. ]
  1064. }
  1065. ]
  1066. });
  1067. // cl-upsert 配置,详细移步到 cl-upsert 示例查看
  1068. const Upsert = useUpsert({
  1069. items: [
  1070. {
  1071. label: '姓名',
  1072. prop: 'name',
  1073. component: {
  1074. name: 'el-input'
  1075. }
  1076. },
  1077. {
  1078. label: '手机号',
  1079. prop: 'phone',
  1080. component: {
  1081. name: 'el-input'
  1082. }
  1083. },
  1084. {
  1085. label: '工作',
  1086. prop: 'occupation',
  1087. component: {
  1088. name: 'cl-select',
  1089. props: {
  1090. tree: true, // 树形方式选择
  1091. checkStrictly: true, // 任意层级都能点
  1092. options: dict.get('occupation') // 使用字典数据
  1093. }
  1094. }
  1095. }
  1096. ]
  1097. });
  1098. const visible = ref(false);
  1099. function open() {
  1100. visible.value = true;
  1101. }
  1102. </script>
  1103. ```
  1104. ## 插件的使用 示例
  1105. ```vue
  1106. <template>
  1107. <div class="scope">
  1108. <div class="h">
  1109. <el-tag size="small" effect="dark" disable-transitions>plugin</el-tag>
  1110. <span>插件的使用</span>
  1111. </div>
  1112. <div class="c">
  1113. <el-button @click="open">预览</el-button>
  1114. <demo-code :files="['table/plugin/base.vue']" />
  1115. <!-- 自定义表格组件 -->
  1116. <cl-dialog v-model="visible" title="插件的使用" width="80%">
  1117. <cl-crud ref="Crud">
  1118. <cl-row>
  1119. <cl-table ref="Table" />
  1120. </cl-row>
  1121. <cl-row>
  1122. <cl-flex1 />
  1123. <cl-pagination />
  1124. </cl-row>
  1125. </cl-crud>
  1126. </cl-dialog>
  1127. </div>
  1128. <div class="f">
  1129. <span class="date">2024-01-01</span>
  1130. </div>
  1131. </div>
  1132. </template>
  1133. <script setup lang="tsx">
  1134. import { useCrud, useTable } from '@cool-vue/crud';
  1135. import { ref } from 'vue';
  1136. import { useDict } from '/$/dict';
  1137. import { merge } from 'lodash-es';
  1138. import { defineComponent } from 'vue';
  1139. // 插件:列标签匹配,方便多个列表公用同一个组件
  1140. function setColumn(): ClTable.Plugin {
  1141. const columns = {
  1142. UserInfo: {
  1143. label: '用户信息',
  1144. minWidth: 200,
  1145. component: {
  1146. vm: defineComponent({
  1147. name: 'user-info',
  1148. props: {
  1149. scope: null
  1150. },
  1151. setup(props) {
  1152. return () => {
  1153. return (
  1154. <div>
  1155. <p>{props.scope.name}</p>
  1156. <p>{props.scope.phone}</p>
  1157. </div>
  1158. );
  1159. };
  1160. }
  1161. })
  1162. }
  1163. }
  1164. } as { [key: string]: DeepPartial<ClTable.Column> };
  1165. return ({ exposed }) => {
  1166. function deep(arr: ClTable.Column[]) {
  1167. arr.forEach(e => {
  1168. if (e.tag) {
  1169. merge(e, columns[e.tag]);
  1170. }
  1171. deep(e.children || []);
  1172. });
  1173. }
  1174. deep(exposed.columns);
  1175. };
  1176. }
  1177. const { dict } = useDict();
  1178. // cl-crud 配置
  1179. const Crud = useCrud(
  1180. {
  1181. service: 'test'
  1182. },
  1183. app => {
  1184. app.refresh();
  1185. }
  1186. );
  1187. // cl-table 配置
  1188. const Table = useTable({
  1189. autoHeight: false,
  1190. contextMenu: ['refresh'],
  1191. columns: [
  1192. {
  1193. type: 'selection'
  1194. },
  1195. {
  1196. tag: 'UserInfo'
  1197. },
  1198. {
  1199. label: '工作',
  1200. prop: 'occupation',
  1201. dict: dict.get('occupation'),
  1202. minWidth: 140
  1203. },
  1204. {
  1205. label: '创建时间',
  1206. prop: 'createTime',
  1207. minWidth: 170,
  1208. sortable: 'desc'
  1209. }
  1210. ],
  1211. //【很重要】配置插件
  1212. plugins: [setColumn()]
  1213. });
  1214. const visible = ref(false);
  1215. function open() {
  1216. visible.value = true;
  1217. }
  1218. </script>
  1219. ```
  1220. ## 行编辑 示例
  1221. ```vue
  1222. <template>
  1223. <div class="scope">
  1224. <div class="h">
  1225. <el-tag size="small" effect="dark" disable-transitions>row-edit</el-tag>
  1226. <span>行编辑</span>
  1227. </div>
  1228. <div class="c">
  1229. <el-button @click="open">预览</el-button>
  1230. <demo-code :files="['table/plugin/row-edit.vue']" />
  1231. <!-- 自定义表格组件 -->
  1232. <cl-dialog v-model="visible" title="行编辑" width="80%">
  1233. <cl-crud ref="Crud">
  1234. <el-text class="mb-4" tag="p">点击姓名、手机号可以进行编辑</el-text>
  1235. <cl-row>
  1236. <cl-table ref="Table" />
  1237. </cl-row>
  1238. <cl-row>
  1239. <cl-flex1 />
  1240. <cl-pagination />
  1241. </cl-row>
  1242. </cl-crud>
  1243. </cl-dialog>
  1244. </div>
  1245. <div class="f">
  1246. <span class="date">2024-01-01</span>
  1247. </div>
  1248. </div>
  1249. </template>
  1250. <script setup lang="ts">
  1251. import { useCrud, useTable } from '@cool-vue/crud';
  1252. import { ref } from 'vue';
  1253. import { useDict } from '/$/dict';
  1254. import { Plugins } from '/#/crud';
  1255. const { dict } = useDict();
  1256. // cl-crud 配置
  1257. const Crud = useCrud(
  1258. {
  1259. service: 'test'
  1260. },
  1261. app => {
  1262. app.refresh();
  1263. }
  1264. );
  1265. // cl-table 配置
  1266. const Table = useTable({
  1267. autoHeight: false,
  1268. contextMenu: ['refresh'],
  1269. columns: [
  1270. {
  1271. label: '姓名',
  1272. prop: 'name',
  1273. minWidth: 140,
  1274. // 【很重要】行编辑,默认 el-input
  1275. edit: true
  1276. },
  1277. {
  1278. label: '手机号',
  1279. prop: 'phone',
  1280. minWidth: 140,
  1281. // 【很重要】行编辑,开启、关闭
  1282. edit: {
  1283. enable: true
  1284. }
  1285. },
  1286. {
  1287. label: '工作',
  1288. prop: 'occupation',
  1289. dict: dict.get('occupation'),
  1290. minWidth: 140,
  1291. edit: {
  1292. enable: true,
  1293. // 【很重要】行编辑,组件配置
  1294. component: {
  1295. name: 'cl-select',
  1296. props: {
  1297. options: dict.get('occupation'),
  1298. tree: true
  1299. }
  1300. }
  1301. }
  1302. },
  1303. {
  1304. label: '创建时间',
  1305. prop: 'createTime',
  1306. minWidth: 170,
  1307. sortable: 'desc',
  1308. // 【很重要】行编辑,组件配置
  1309. edit: {
  1310. enable: true,
  1311. component: {
  1312. name: 'el-date-picker',
  1313. props: {
  1314. type: 'date',
  1315. valueFormat: 'YYYY-MM-DD'
  1316. }
  1317. }
  1318. }
  1319. },
  1320. {
  1321. type: 'op',
  1322. buttons: ['delete']
  1323. }
  1324. ],
  1325. //【很重要】行编辑插件
  1326. plugins: [Plugins.Table.rowEdit()]
  1327. });
  1328. const visible = ref(false);
  1329. function open() {
  1330. visible.value = true;
  1331. }
  1332. </script>
  1333. ```
  1334. ## 表头搜索 示例
  1335. ```vue
  1336. <template>
  1337. <div class="scope">
  1338. <div class="h">
  1339. <el-tag size="small" effect="dark" disable-transitions>search</el-tag>
  1340. <span>表头搜索</span>
  1341. </div>
  1342. <div class="c">
  1343. <el-button @click="open">预览</el-button>
  1344. <demo-code :files="['table/search.vue']" />
  1345. <!-- 自定义表格组件 -->
  1346. <cl-dialog v-model="visible" title="表头搜索" width="80%">
  1347. <cl-crud ref="Crud">
  1348. <cl-row>
  1349. <cl-table ref="Table" />
  1350. </cl-row>
  1351. <cl-row>
  1352. <cl-flex1 />
  1353. <cl-pagination />
  1354. </cl-row>
  1355. </cl-crud>
  1356. </cl-dialog>
  1357. </div>
  1358. <div class="f">
  1359. <span class="date">2024-01-01</span>
  1360. </div>
  1361. </div>
  1362. </template>
  1363. <script setup lang="tsx">
  1364. import { useCrud, useTable } from '@cool-vue/crud';
  1365. import { ref } from 'vue';
  1366. import { useDict } from '/$/dict';
  1367. import { Plus } from '@element-plus/icons-vue';
  1368. const { dict } = useDict();
  1369. // cl-crud 配置
  1370. const Crud = useCrud(
  1371. {
  1372. service: 'test'
  1373. },
  1374. app => {
  1375. app.refresh();
  1376. }
  1377. );
  1378. // cl-table 配置
  1379. const Table = useTable({
  1380. autoHeight: false,
  1381. contextMenu: ['refresh'],
  1382. columns: [
  1383. {
  1384. label: '姓名',
  1385. prop: 'name',
  1386. minWidth: 140,
  1387. //【很重要】搜索参数配置
  1388. search: {
  1389. isInput: false, // 默认false,是否输入框模式
  1390. value: '', // 默认值
  1391. refreshOnChange: true, // 默认false,搜索时刷新数据,service 的 page 接口请求参数为 { page: 1, [绑定的prop]: 输入值 }
  1392. // 自定义渲染组件
  1393. component: {
  1394. name: 'el-input',
  1395. props: {
  1396. placeholder: '搜索姓名'
  1397. }
  1398. }
  1399. }
  1400. },
  1401. {
  1402. label: '手机号',
  1403. prop: 'phone',
  1404. minWidth: 140,
  1405. //【很重要】搜索参数配置
  1406. search: {
  1407. // 是否显示搜索图标
  1408. icon: () => <Plus />,
  1409. // 自定义渲染组件
  1410. component: {
  1411. name: 'el-input',
  1412. props: {
  1413. placeholder: '搜索手机号',
  1414. // 自定义 change 事件
  1415. onChange(val) {
  1416. Crud.value?.refresh({
  1417. page: 1,
  1418. phone: val
  1419. });
  1420. }
  1421. }
  1422. }
  1423. }
  1424. },
  1425. {
  1426. label: '工作',
  1427. prop: 'occupation',
  1428. dict: dict.get('occupation'),
  1429. minWidth: 140,
  1430. //【很重要】搜索参数配置
  1431. search: {
  1432. // 是否显示搜索图标
  1433. icon: () => <cl-svg name="icon-app" size={13} />,
  1434. // 自定义渲染组件
  1435. component: {
  1436. name: 'cl-select',
  1437. props: {
  1438. placeholder: '搜索工作',
  1439. options: dict.get('occupation')
  1440. }
  1441. }
  1442. }
  1443. },
  1444. {
  1445. label: '创建时间',
  1446. prop: 'createTime',
  1447. minWidth: 170,
  1448. sortable: 'desc'
  1449. }
  1450. ]
  1451. });
  1452. const visible = ref(false);
  1453. function open() {
  1454. visible.value = true;
  1455. }
  1456. </script>
  1457. ```
  1458. ## 多选框数据 示例
  1459. ```vue
  1460. <template>
  1461. <div class="scope">
  1462. <div class="h">
  1463. <el-tag size="small" effect="dark" disable-transitions>selection</el-tag>
  1464. <span>多选框数据</span>
  1465. </div>
  1466. <div class="c">
  1467. <el-button @click="open">预览</el-button>
  1468. <demo-code :files="['table/selection.vue']" />
  1469. <!-- 自定义表格组件 -->
  1470. <cl-dialog v-model="visible" title="多选框数据" width="80%">
  1471. <cl-crud ref="Crud">
  1472. <cl-row>
  1473. <el-button @click="selectRow">选中2行</el-button>
  1474. <el-button :disabled="Table?.selection.length == 0" @click="clear">
  1475. 取消选择
  1476. </el-button>
  1477. </cl-row>
  1478. <cl-row>
  1479. <cl-table ref="Table" />
  1480. </cl-row>
  1481. <cl-row>
  1482. <el-text>已选 {{ Table?.selection.length }} 人</el-text>
  1483. <cl-flex1 />
  1484. <cl-pagination />
  1485. </cl-row>
  1486. </cl-crud>
  1487. </cl-dialog>
  1488. </div>
  1489. <div class="f">
  1490. <span class="date">2024-01-01</span>
  1491. </div>
  1492. </div>
  1493. </template>
  1494. <script setup lang="ts">
  1495. import { useCrud, useTable } from '@cool-vue/crud';
  1496. import { ref } from 'vue';
  1497. import { useDict } from '/$/dict';
  1498. const { dict } = useDict();
  1499. // cl-crud 配置
  1500. const Crud = useCrud(
  1501. {
  1502. service: 'test'
  1503. },
  1504. app => {
  1505. app.refresh();
  1506. }
  1507. );
  1508. // cl-table 配置
  1509. const Table = useTable({
  1510. autoHeight: false,
  1511. contextMenu: ['refresh', 'check'],
  1512. columns: [
  1513. {
  1514. type: 'selection'
  1515. },
  1516. {
  1517. label: '姓名',
  1518. prop: 'name',
  1519. minWidth: 140
  1520. },
  1521. {
  1522. label: '手机号',
  1523. prop: 'phone',
  1524. minWidth: 140
  1525. },
  1526. {
  1527. label: '工作',
  1528. prop: 'occupation',
  1529. dict: dict.get('occupation'),
  1530. minWidth: 140
  1531. },
  1532. {
  1533. label: '创建时间',
  1534. prop: 'createTime',
  1535. minWidth: 170,
  1536. sortable: 'desc'
  1537. }
  1538. ]
  1539. });
  1540. function selectRow() {
  1541. const [a, b] = Table.value?.data || [];
  1542. // 选中2个
  1543. Table.value?.toggleRowSelection(a);
  1544. Table.value?.toggleRowSelection(b);
  1545. }
  1546. function clear() {
  1547. Table.value?.clearSelection();
  1548. }
  1549. const visible = ref(false);
  1550. function open() {
  1551. visible.value = true;
  1552. }
  1553. </script>
  1554. ```
  1555. ## 插槽的使用 示例
  1556. ```vue
  1557. <template>
  1558. <div class="scope">
  1559. <div class="h">
  1560. <el-tag size="small" effect="dark" disable-transitions>slot</el-tag>
  1561. <span>插槽的使用</span>
  1562. </div>
  1563. <div class="c">
  1564. <el-button @click="open">预览</el-button>
  1565. <demo-code :files="['table/slot.vue']" />
  1566. <!-- 自定义表格组件 -->
  1567. <cl-dialog v-model="visible" title="插槽的使用" width="80%">
  1568. <cl-crud ref="Crud">
  1569. <cl-row>
  1570. <cl-table ref="Table">
  1571. <!--【很重要】必须与 prop 名保持一致,格式:column-[prop] -->
  1572. <template #column-name="{ scope }">
  1573. <cl-row type="flex" align="middle">
  1574. <cl-avatar :size="36" :style="{ marginRight: '10px' }" />
  1575. <el-text>{{ scope.row.name }}</el-text>
  1576. </cl-row>
  1577. </template>
  1578. <template #column-phone="{ scope }"> 📱{{ scope.row.phone }} </template>
  1579. </cl-table>
  1580. </cl-row>
  1581. <cl-row>
  1582. <cl-flex1 />
  1583. <cl-pagination />
  1584. </cl-row>
  1585. </cl-crud>
  1586. </cl-dialog>
  1587. </div>
  1588. <div class="f">
  1589. <span class="date">2024-01-01</span>
  1590. </div>
  1591. </div>
  1592. </template>
  1593. <script setup lang="ts">
  1594. import { useCrud, useTable } from '@cool-vue/crud';
  1595. import { ref } from 'vue';
  1596. import { useDict } from '/$/dict';
  1597. const { dict } = useDict();
  1598. // cl-crud 配置
  1599. const Crud = useCrud(
  1600. {
  1601. service: 'test'
  1602. },
  1603. app => {
  1604. app.refresh();
  1605. }
  1606. );
  1607. // cl-table 配置
  1608. const Table = useTable({
  1609. autoHeight: false,
  1610. contextMenu: ['refresh'],
  1611. columns: [
  1612. {
  1613. headerAlign: 'left',
  1614. label: '姓名',
  1615. prop: 'name',
  1616. minWidth: 140
  1617. },
  1618. {
  1619. label: '手机号',
  1620. prop: 'phone',
  1621. minWidth: 140
  1622. },
  1623. {
  1624. label: '工作',
  1625. prop: 'occupation',
  1626. dict: dict.get('occupation'),
  1627. minWidth: 140
  1628. },
  1629. {
  1630. label: '创建时间',
  1631. prop: 'createTime',
  1632. minWidth: 170,
  1633. sortable: 'desc'
  1634. }
  1635. ]
  1636. });
  1637. const visible = ref(false);
  1638. function open() {
  1639. visible.value = true;
  1640. }
  1641. </script>
  1642. ```
  1643. ## 合并行或列 示例
  1644. ```vue
  1645. <template>
  1646. <div class="scope">
  1647. <div class="h">
  1648. <el-tag size="small" effect="dark" disable-transitions>span-method</el-tag>
  1649. <span>合并行或列</span>
  1650. </div>
  1651. <div class="c">
  1652. <el-button @click="open">预览</el-button>
  1653. <demo-code :files="['table/span-method.vue']" />
  1654. <!-- 自定义表格组件 -->
  1655. <cl-dialog v-model="visible" title="合并行或列" width="80%">
  1656. <cl-crud ref="Crud">
  1657. <cl-row>
  1658. <cl-table ref="Table" :span-method="onSpanMethod" />
  1659. </cl-row>
  1660. <cl-row>
  1661. <cl-flex1 />
  1662. <cl-pagination />
  1663. </cl-row>
  1664. </cl-crud>
  1665. </cl-dialog>
  1666. </div>
  1667. <div class="f">
  1668. <span class="date">2024-01-01</span>
  1669. </div>
  1670. </div>
  1671. </template>
  1672. <script setup lang="ts">
  1673. import { useCrud, useTable } from '@cool-vue/crud';
  1674. import { ref } from 'vue';
  1675. import { useDict } from '/$/dict';
  1676. const { dict } = useDict();
  1677. // cl-crud 配置
  1678. const Crud = useCrud(
  1679. {
  1680. service: 'test'
  1681. },
  1682. app => {
  1683. app.refresh();
  1684. }
  1685. );
  1686. // cl-table 配置
  1687. const Table = useTable({
  1688. autoHeight: false,
  1689. contextMenu: ['refresh'],
  1690. columns: [
  1691. {
  1692. label: '姓名',
  1693. prop: 'name',
  1694. minWidth: 140
  1695. },
  1696. {
  1697. label: '存款',
  1698. prop: 'wages',
  1699. minWidth: 140
  1700. },
  1701. {
  1702. label: '手机号',
  1703. prop: 'phone',
  1704. minWidth: 140
  1705. },
  1706. {
  1707. label: '工作',
  1708. prop: 'occupation',
  1709. dict: dict.get('occupation'),
  1710. minWidth: 140
  1711. },
  1712. {
  1713. label: '创建时间',
  1714. prop: 'createTime',
  1715. minWidth: 170,
  1716. sortable: 'desc'
  1717. }
  1718. ]
  1719. });
  1720. interface SpanMethodProps {
  1721. row: any;
  1722. column: any;
  1723. rowIndex: number;
  1724. columnIndex: number;
  1725. }
  1726. function onSpanMethod({ row, column, rowIndex, columnIndex }: SpanMethodProps) {
  1727. // 根据实际业务需求调整返回值 { rowspan, colspan }
  1728. if (columnIndex === 0) {
  1729. if (rowIndex % 2 === 0) {
  1730. return {
  1731. rowspan: 2,
  1732. colspan: 1
  1733. };
  1734. } else {
  1735. return {
  1736. rowspan: 0,
  1737. colspan: 0
  1738. };
  1739. }
  1740. }
  1741. }
  1742. const visible = ref(false);
  1743. function open() {
  1744. visible.value = true;
  1745. }
  1746. </script>
  1747. ```
  1748. ## 表尾合计行 示例
  1749. ```vue
  1750. <template>
  1751. <div class="scope">
  1752. <div class="h">
  1753. <el-tag size="small" effect="dark" disable-transitions>summary</el-tag>
  1754. <span>表尾合计行</span>
  1755. </div>
  1756. <div class="c">
  1757. <el-button @click="open">预览</el-button>
  1758. <demo-code :files="['table/summary.vue']" />
  1759. <!-- 自定义表格组件 -->
  1760. <cl-dialog v-model="visible" title="表尾合计行" width="80%">
  1761. <cl-crud ref="Crud">
  1762. <cl-row>
  1763. <cl-table ref="Table" show-summary :summary-method="getSummaries" />
  1764. </cl-row>
  1765. <cl-row>
  1766. <cl-flex1 />
  1767. <cl-pagination />
  1768. </cl-row>
  1769. </cl-crud>
  1770. </cl-dialog>
  1771. </div>
  1772. <div class="f">
  1773. <span class="date">2024-01-01</span>
  1774. </div>
  1775. </div>
  1776. </template>
  1777. <script setup lang="ts">
  1778. import { useCrud, useTable } from '@cool-vue/crud';
  1779. import { ref } from 'vue';
  1780. import { useDict } from '/$/dict';
  1781. const { dict } = useDict();
  1782. // cl-crud 配置
  1783. const Crud = useCrud(
  1784. {
  1785. service: 'test'
  1786. },
  1787. app => {
  1788. app.refresh();
  1789. }
  1790. );
  1791. // cl-table 配置
  1792. const Table = useTable({
  1793. autoHeight: false,
  1794. contextMenu: ['refresh'],
  1795. columns: [
  1796. {
  1797. label: '姓名',
  1798. prop: 'name',
  1799. minWidth: 140
  1800. },
  1801. {
  1802. label: '存款',
  1803. prop: 'wages',
  1804. minWidth: 140
  1805. },
  1806. {
  1807. label: '手机号',
  1808. prop: 'phone',
  1809. minWidth: 140
  1810. },
  1811. {
  1812. label: '工作',
  1813. prop: 'occupation',
  1814. dict: dict.get('occupation'),
  1815. minWidth: 140
  1816. },
  1817. {
  1818. label: '创建时间',
  1819. prop: 'createTime',
  1820. minWidth: 170,
  1821. sortable: 'desc'
  1822. }
  1823. ]
  1824. });
  1825. function getSummaries() {
  1826. return ['合计', '$' + Table.value?.data.reduce((a, b) => a + b.wages, 0)];
  1827. }
  1828. const visible = ref(false);
  1829. function open() {
  1830. visible.value = true;
  1831. }
  1832. </script>
  1833. ```