12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117 |
- ---
- description: cl-table 组件示例
- globs: *.tsx, *.ts, *.vue
- ---
- ## 起步 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>base</el-tag>
- <span>起步</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/base.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="起步" width="80%">
- <!--【很重要】需要包含在 cl-crud 组件内 -->
- <cl-crud ref="Crud">
- <cl-row>
- <!-- 参数文档查看:https://element-plus.org/zh-CN/component/table.html#table-%E5%B1%9E%E6%80%A7 -->
- <cl-table ref="Table" stripe></cl-table>
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { useCrud, useTable } from '@cool-vue/crud';
- import { ref } from 'vue';
- import { useDict } from '/$/dict';
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- // 测试数据,移步到 cl-crud 例子查看
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- // cl-table 配置
- const Table = useTable({
- // 是否自动计算表格高度,表格高度等于减去上区域和下区域的高度
- //【很重要】在弹窗或者上级不确定高度中,设置 autoHeight: false,避免显示异常。也可以手动设置最大高度 maxHeight: 500
- autoHeight: false,
- // 右键菜单,移步到右键菜单示例中查看
- contextMenu: ['refresh'],
- // 列配置,点击 columns 查看描述
- // 更多配置查看 el-table-column 文档,https://element-plus.org/zh-CN/component/table.html#table-column-%E5%B1%9E%E6%80%A7
- columns: [
- {
- // 是否为多选框操作列
- type: 'selection'
- // 是否为序号列
- // type: "index"
- },
- {
- // 表头标题
- label: '姓名',
- // 绑定值
- prop: 'name',
- // 最小宽度
- minWidth: 140
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140
- },
- {
- label: '工作',
- prop: 'occupation',
- // 字典匹配,移步到字典示例中查看
- dict: dict.get('occupation'),
- minWidth: 140
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- // 是否排序,desc, asc
- sortable: 'desc'
- }
- ]
- });
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- </script>
- ```
- ## 多级表头 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>children</el-tag>
- <span>多级表头</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/children.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="多级表头" width="80%">
- <cl-crud ref="Crud">
- <cl-row>
- <cl-table ref="Table" />
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { useCrud, useTable } from '@cool-vue/crud';
- import { ref } from 'vue';
- import { useDict } from '/$/dict';
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- contextMenu: ['refresh'],
- columns: [
- {
- label: '用户信息',
- prop: 'baseInfo',
- minWidth: 250,
- // 配置 children 参数
- children: [
- {
- label: '姓名',
- prop: 'name',
- minWidth: 140
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140
- }
- ]
- },
- {
- label: '工作',
- prop: 'occupation',
- dict: dict.get('occupation'),
- minWidth: 140
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc'
- }
- ]
- });
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- </script>
- ```
- ## 自定义列展示 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>column-custom</el-tag>
- <span>自定义列展示</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/column-custom.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="自定义列展示" width="80%">
- <cl-crud ref="Crud">
- <cl-row>
- <!--【很重要】组件配置,设置为 Table 的 columns,也可以自定义 -->
- <cl-column-custom :columns="Table?.columns" />
- </cl-row>
- <cl-row>
- <cl-table ref="Table"></cl-table>
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { useCrud, useTable } from '@cool-vue/crud';
- import { ref } from 'vue';
- import { useDict } from '/$/dict';
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- contextMenu: ['refresh'],
- columns: [
- {
- label: '姓名',
- prop: 'name',
- minWidth: 140
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140
- },
- {
- label: '工作',
- prop: 'occupation',
- dict: dict.get('occupation'),
- minWidth: 140
- },
- {
- label: '状态',
- prop: 'status',
- dict: [
- {
- label: '启用',
- value: 1,
- type: 'success'
- },
- {
- label: '禁用',
- value: 0,
- type: 'danger'
- }
- ],
- minWidth: 140
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc'
- }
- ]
- });
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- </script>
- ```
- ## 组件渲染 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>component</el-tag>
- <span>组件渲染</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/component/index.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="组件渲染" width="80%">
- <cl-crud ref="Crud">
- <cl-row>
- <cl-table ref="Table"></cl-table>
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { useCrud, useTable } from '@cool-vue/crud';
- import { ref } from 'vue';
- import { useDict } from '/$/dict';
- import { ElMessage } from 'element-plus';
- import UserInfo from './user-info.vue';
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- contextMenu: ['refresh'],
- columns: [
- {
- type: 'selection'
- },
- {
- label: '姓名',
- prop: 'name',
- minWidth: 140,
- //【很重要】组件实例方式渲染
- component: {
- vm: UserInfo
- }
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140,
- //【很重要】组件名方式渲染
- component: {
- // 组件名,组件必须全局注册了
- name: 'el-input',
- // 传入参数
- props: {
- onChange(val) {
- ElMessage.info(val);
- }
- }
- }
- },
- {
- label: '工作',
- prop: 'occupation',
- dict: dict.get('occupation'),
- minWidth: 140
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc'
- }
- ]
- });
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- </script>
- ```
- ## user-info 示例
- ```vue
- <template>
- <div class="user-info">
- <cl-avatar :size="36" />
- <div class="det">
- <p>{{ scope?.name }}</p>
- </div>
- </div>
- </template>
- <!-- name 必须填写且唯一 -->
- <script setup lang="ts">
- defineOptions({
- name: 'user-info'
- });
- const props = defineProps({
- prop: String, // 列配置的 prop
- scope: null // 列数据
- });
- </script>
- <style lang="scss" scoped>
- .user-info {
- display: flex;
- align-items: center;
- .det {
- flex: 1;
- margin-left: 10px;
- text-align: left;
- }
- }
- </style>
- ```
- ## 右键菜单 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>context-menu</el-tag>
- <span>右键菜单</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/context-menu.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="右键菜单">
- <cl-crud ref="Crud">
- <cl-row>
- <cl-table ref="Table"></cl-table>
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- <!-- 新增、编辑 -->
- <cl-upsert ref="Upsert" />
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { useCrud, useTable, useUpsert } from '@cool-vue/crud';
- import { ref } from 'vue';
- import { useDict } from '/$/dict';
- import { ElMessage } from 'element-plus';
- import { EditPen, MoreFilled } from '@element-plus/icons-vue';
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- // 右键菜单配置,为 [] 时则不显示内容
- contextMenu: [
- 'refresh', // 刷新
- 'check', // 选择行
- 'edit', // 弹出编辑框
- 'delete', // 弹出删除提示
- 'info', // 弹出详情
- 'order-desc', // 使列倒序
- 'order-asc', // 使列升序
- {
- label: '禁用状态',
- disabled: true
- },
- {
- label: '带图标',
- prefixIcon: EditPen,
- suffixIcon: MoreFilled
- },
- {
- label: '超出隐藏,看我有很多字非常多',
- ellipsis: true
- },
- {
- label: '多层级',
- children: [
- {
- label: 'A',
- children: [
- {
- label: 'A-1',
- callback(done) {
- ElMessage.success('点击了A-1');
- done();
- }
- }
- ]
- },
- {
- label: 'B'
- },
- {
- label: 'C'
- }
- ]
- },
- // row 行数据
- // column 列属性
- // event 事件对象
- (row, column, event) => {
- // 必须返回一个对象
- return {
- label: '自定义2',
- callback(done) {
- ElMessage.info('获取中');
- setTimeout(() => {
- ElMessage.success('Ta 是' + row.name);
- // 关闭右键菜单,只有在用到 callback 方法时才需要
- done();
- }, 500);
- }
- };
- }
- ],
- columns: [
- {
- type: 'selection'
- },
- {
- label: '姓名',
- prop: 'name',
- minWidth: 140
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140
- },
- {
- label: '工作',
- prop: 'occupation',
- dict: dict.get('occupation'),
- minWidth: 140
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc'
- }
- ]
- });
- // cl-upsert 配置,详细移步到 cl-upsert 示例查看
- const Upsert = useUpsert({
- items: [
- {
- label: '姓名',
- prop: 'name',
- component: {
- name: 'el-input'
- }
- },
- {
- label: '手机号',
- prop: 'phone',
- component: {
- name: 'el-input'
- }
- },
- {
- label: '工作',
- prop: 'occupation',
- component: {
- name: 'cl-select',
- props: {
- tree: true,
- checkStrictly: true,
- options: dict.get('occupation')
- }
- }
- }
- ]
- });
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- </script>
- ```
- ## 字典匹配 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>dict</el-tag>
- <span>字典匹配</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/dict.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="字典匹配" width="80%">
- <cl-crud ref="Crud">
- <cl-row>
- <cl-table ref="Table" />
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { useCrud, useTable } from '@cool-vue/crud';
- import { computed, reactive, ref } from 'vue';
- import { useDict } from '/$/dict';
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- const options = reactive({
- occupation: [] as { label: string; value: any }[]
- });
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- contextMenu: ['refresh'],
- columns: [
- {
- label: '姓名',
- prop: 'name',
- minWidth: 140
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140
- },
- {
- label: '工作',
- prop: 'occupation',
- //【很重要】字典匹配
- // 使用字典模块的 get 方法绑定,菜单地址 /dict/list
- dict: dict.get('occupation'),
- // 是否使用不同颜色区分
- dictColor: true,
- minWidth: 140
- },
- {
- label: '等级',
- prop: 'occupation',
- //【很重要】动态匹配列表的情况,使用 computed
- dict: computed(() => options.occupation),
- minWidth: 140
- },
- {
- label: '状态',
- prop: 'status',
- // 自定义匹配列表
- dict: [
- {
- label: '启用',
- value: 1,
- type: 'success'
- },
- {
- label: '禁用',
- value: 0,
- type: 'danger'
- }
- ],
- minWidth: 140
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc'
- }
- ]
- });
- const visible = ref(false);
- function open() {
- visible.value = true;
- // 模拟接口获取数据
- setTimeout(() => {
- options.occupation = [
- {
- label: 'A',
- value: 0
- },
- {
- label: 'B',
- value: 1
- },
- {
- label: 'C',
- value: 2
- },
- {
- label: 'D',
- value: 3
- },
- {
- label: 'E',
- value: 4
- },
- {
- label: 'F',
- value: 5
- }
- ];
- }, 1500);
- }
- </script>
- ```
- ## 数据格式化 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>formatter</el-tag>
- <span>数据格式化</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/formatter.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="数据格式化" width="80%">
- <cl-crud ref="Crud">
- <cl-row>
- <cl-table ref="Table" />
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-09-26</span>
- </div>
- </div>
- </template>
- <script setup lang="tsx">
- import { useCrud, useTable } from '@cool-vue/crud';
- import { ref } from 'vue';
- // cl-crud 配置
- const Crud = useCrud(
- {
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- contextMenu: ['refresh'],
- columns: [
- {
- label: '姓名',
- prop: 'name',
- minWidth: 140
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140,
- formatter(row) {
- return '📱' + row.phone;
- }
- },
- {
- label: '用户信息',
- minWidth: 200,
- // tsx 方式渲染
- // 【很重要】使用 tsx 语法时,script 的 lang 一定要设置为 tsx
- formatter(row) {
- // row 为当前行数据
- return (
- <el-row>
- <cl-avatar size={30} />
- <el-text style={{ marginLeft: '10px' }}>{row.name}</el-text>
- </el-row>
- );
- }
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc'
- }
- ]
- });
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- </script>
- ```
- ## 隐藏/显示 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>hidden</el-tag>
- <span>隐藏/显示</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/hidden.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="隐藏/显示" width="80%">
- <cl-crud ref="Crud">
- <!--配置一个 tab -->
- <el-tabs v-model="active">
- <el-tab-pane label="员工" name="user"></el-tab-pane>
- <el-tab-pane label="企业" name="company"></el-tab-pane>
- </el-tabs>
- <cl-row>
- <!-- 使用方法 showColumn 显示 -->
- <el-button @click="showColumn('account')">显示账号</el-button>
- <!-- 使用方法 hideColumn 隐藏 -->
- <el-button @click="hideColumn('account')">隐藏账号</el-button>
- </cl-row>
- <cl-row>
- <cl-table ref="Table"></cl-table>
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { useCrud, useTable } from '@cool-vue/crud';
- import { computed, ref } from 'vue';
- import { useDict } from '/$/dict';
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- // 测试数据,移步到 cl-crud 例子查看
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- const active = ref('user');
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- contextMenu: ['refresh'],
- columns: [
- {
- label: 'ID',
- prop: 'id',
- minWidth: 140,
- //【很重要】配置 hidden 参数,格式为 boolean 或者 Vue.ComputedRef<boolean>
- hidden: computed(() => {
- return active.value != 'company';
- })
- },
- {
- label: '账号',
- prop: 'account',
- minWidth: 140,
- hidden: true // 默认 false
- },
- {
- label: '姓名',
- prop: 'name',
- minWidth: 140
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140
- },
- {
- label: '工作',
- prop: 'occupation',
- dict: dict.get('occupation'),
- minWidth: 140
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc'
- }
- ]
- });
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- function showColumn(prop: string) {
- Table.value?.showColumn(prop);
- }
- function hideColumn(prop: string) {
- Table.value?.hideColumn(prop);
- }
- </script>
- ```
- ## 操作栏 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>op</el-tag>
- <span>操作栏</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/op.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="操作栏" width="80%">
- <cl-crud ref="Crud">
- <cl-row>
- <cl-table ref="Table">
- <!-- 插槽的渲染方式 #[component.name] -->
- <template #slot-btns="{ scope }">
- <el-button
- @click="
- () => {
- ElMessage.info(scope.row.name);
- }
- "
- >插槽按钮</el-button
- >
- </template>
- </cl-table>
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- <!-- 新增、编辑 -->
- <cl-upsert ref="Upsert" />
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { useCrud, useTable, useUpsert } from '@cool-vue/crud';
- import { ref } from 'vue';
- import { useDict } from '/$/dict';
- import { ElMessage } from 'element-plus';
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- contextMenu: ['refresh'],
- columns: [
- {
- label: '姓名',
- prop: 'name',
- minWidth: 140
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140
- },
- {
- label: '工作',
- prop: 'occupation',
- dict: dict.get('occupation'),
- minWidth: 140
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc'
- },
- {
- //【很重要】type 必须是 op
- type: 'op',
- width: 410, // 宽度
- //【很重要】操作按钮配置,edit 和 info 必须搭配 cl-upsert 实现
- // edit 编辑,预先获取 service 的 info 接口数据,并带入 cl-upsert 的表单值中
- // info 详情,cl-upsert 内的组件全部传入 disabled 参数
- // delete 删除,调用 service 的 delete 接口删除行数据
- buttons: [
- {
- label: '编辑',
- type: 'primary',
- onClick({ scope }) {
- ElMessage.info(scope.row.name);
- }
- },
- {
- label: '删除',
- type: 'danger',
- onClick({ scope }) {
- ElMessage.info(scope.row.name);
- }
- },
- {
- label: '更多',
- type: 'success',
- children: [
- {
- label: '查看',
- onClick({ scope }) {
- ElMessage.info(scope.row.name);
- }
- },
- {
- label: '禁用',
- onClick({ scope }) {
- ElMessage.info(scope.row.name);
- }
- }
- ]
- },
- {
- name: 'slot-btns'
- }
- ]
- }
- ]
- });
- // cl-upsert 配置,详细移步到 cl-upsert 示例查看
- const Upsert = useUpsert({
- items: [
- {
- label: '姓名',
- prop: 'name',
- component: {
- name: 'el-input'
- }
- },
- {
- label: '手机号',
- prop: 'phone',
- component: {
- name: 'el-input'
- }
- },
- {
- label: '工作',
- prop: 'occupation',
- component: {
- name: 'cl-select',
- props: {
- tree: true, // 树形方式选择
- checkStrictly: true, // 任意层级都能点
- options: dict.get('occupation') // 使用字典数据
- }
- }
- }
- ]
- });
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- </script>
- ```
- ## 插件的使用 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>plugin</el-tag>
- <span>插件的使用</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/plugin/base.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="插件的使用" width="80%">
- <cl-crud ref="Crud">
- <cl-row>
- <cl-table ref="Table" />
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="tsx">
- import { useCrud, useTable } from '@cool-vue/crud';
- import { ref } from 'vue';
- import { useDict } from '/$/dict';
- import { merge } from 'lodash-es';
- import { defineComponent } from 'vue';
- // 插件:列标签匹配,方便多个列表公用同一个组件
- function setColumn(): ClTable.Plugin {
- const columns = {
- UserInfo: {
- label: '用户信息',
- minWidth: 200,
- component: {
- vm: defineComponent({
- name: 'user-info',
- props: {
- scope: null
- },
- setup(props) {
- return () => {
- return (
- <div>
- <p>{props.scope.name}</p>
- <p>{props.scope.phone}</p>
- </div>
- );
- };
- }
- })
- }
- }
- } as { [key: string]: DeepPartial<ClTable.Column> };
- return ({ exposed }) => {
- function deep(arr: ClTable.Column[]) {
- arr.forEach(e => {
- if (e.tag) {
- merge(e, columns[e.tag]);
- }
- deep(e.children || []);
- });
- }
- deep(exposed.columns);
- };
- }
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- contextMenu: ['refresh'],
- columns: [
- {
- type: 'selection'
- },
- {
- tag: 'UserInfo'
- },
- {
- label: '工作',
- prop: 'occupation',
- dict: dict.get('occupation'),
- minWidth: 140
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc'
- }
- ],
- //【很重要】配置插件
- plugins: [setColumn()]
- });
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- </script>
- ```
- ## 行编辑 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>row-edit</el-tag>
- <span>行编辑</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/plugin/row-edit.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="行编辑" width="80%">
- <cl-crud ref="Crud">
- <el-text class="mb-4" tag="p">点击姓名、手机号可以进行编辑</el-text>
- <cl-row>
- <cl-table ref="Table" />
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { useCrud, useTable } from '@cool-vue/crud';
- import { ref } from 'vue';
- import { useDict } from '/$/dict';
- import { Plugins } from '/#/crud';
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- contextMenu: ['refresh'],
- columns: [
- {
- label: '姓名',
- prop: 'name',
- minWidth: 140,
- // 【很重要】行编辑,默认 el-input
- edit: true
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140,
- // 【很重要】行编辑,开启、关闭
- edit: {
- enable: true
- }
- },
- {
- label: '工作',
- prop: 'occupation',
- dict: dict.get('occupation'),
- minWidth: 140,
- edit: {
- enable: true,
- // 【很重要】行编辑,组件配置
- component: {
- name: 'cl-select',
- props: {
- options: dict.get('occupation'),
- tree: true
- }
- }
- }
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc',
- // 【很重要】行编辑,组件配置
- edit: {
- enable: true,
- component: {
- name: 'el-date-picker',
- props: {
- type: 'date',
- valueFormat: 'YYYY-MM-DD'
- }
- }
- }
- },
- {
- type: 'op',
- buttons: ['delete']
- }
- ],
- //【很重要】行编辑插件
- plugins: [Plugins.Table.rowEdit()]
- });
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- </script>
- ```
- ## 表头搜索 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>search</el-tag>
- <span>表头搜索</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/search.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="表头搜索" width="80%">
- <cl-crud ref="Crud">
- <cl-row>
- <cl-table ref="Table" />
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="tsx">
- import { useCrud, useTable } from '@cool-vue/crud';
- import { ref } from 'vue';
- import { useDict } from '/$/dict';
- import { Plus } from '@element-plus/icons-vue';
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- contextMenu: ['refresh'],
- columns: [
- {
- label: '姓名',
- prop: 'name',
- minWidth: 140,
- //【很重要】搜索参数配置
- search: {
- isInput: false, // 默认false,是否输入框模式
- value: '', // 默认值
- refreshOnChange: true, // 默认false,搜索时刷新数据,service 的 page 接口请求参数为 { page: 1, [绑定的prop]: 输入值 }
- // 自定义渲染组件
- component: {
- name: 'el-input',
- props: {
- placeholder: '搜索姓名'
- }
- }
- }
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140,
- //【很重要】搜索参数配置
- search: {
- // 是否显示搜索图标
- icon: () => <Plus />,
- // 自定义渲染组件
- component: {
- name: 'el-input',
- props: {
- placeholder: '搜索手机号',
- // 自定义 change 事件
- onChange(val) {
- Crud.value?.refresh({
- page: 1,
- phone: val
- });
- }
- }
- }
- }
- },
- {
- label: '工作',
- prop: 'occupation',
- dict: dict.get('occupation'),
- minWidth: 140,
- //【很重要】搜索参数配置
- search: {
- // 是否显示搜索图标
- icon: () => <cl-svg name="icon-app" size={13} />,
- // 自定义渲染组件
- component: {
- name: 'cl-select',
- props: {
- placeholder: '搜索工作',
- options: dict.get('occupation')
- }
- }
- }
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc'
- }
- ]
- });
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- </script>
- ```
- ## 多选框数据 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>selection</el-tag>
- <span>多选框数据</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/selection.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="多选框数据" width="80%">
- <cl-crud ref="Crud">
- <cl-row>
- <el-button @click="selectRow">选中2行</el-button>
- <el-button :disabled="Table?.selection.length == 0" @click="clear">
- 取消选择
- </el-button>
- </cl-row>
- <cl-row>
- <cl-table ref="Table" />
- </cl-row>
- <cl-row>
- <el-text>已选 {{ Table?.selection.length }} 人</el-text>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { useCrud, useTable } from '@cool-vue/crud';
- import { ref } from 'vue';
- import { useDict } from '/$/dict';
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- contextMenu: ['refresh', 'check'],
- columns: [
- {
- type: 'selection'
- },
- {
- label: '姓名',
- prop: 'name',
- minWidth: 140
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140
- },
- {
- label: '工作',
- prop: 'occupation',
- dict: dict.get('occupation'),
- minWidth: 140
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc'
- }
- ]
- });
- function selectRow() {
- const [a, b] = Table.value?.data || [];
- // 选中2个
- Table.value?.toggleRowSelection(a);
- Table.value?.toggleRowSelection(b);
- }
- function clear() {
- Table.value?.clearSelection();
- }
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- </script>
- ```
- ## 插槽的使用 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>slot</el-tag>
- <span>插槽的使用</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/slot.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="插槽的使用" width="80%">
- <cl-crud ref="Crud">
- <cl-row>
- <cl-table ref="Table">
- <!--【很重要】必须与 prop 名保持一致,格式:column-[prop] -->
- <template #column-name="{ scope }">
- <cl-row type="flex" align="middle">
- <cl-avatar :size="36" :style="{ marginRight: '10px' }" />
- <el-text>{{ scope.row.name }}</el-text>
- </cl-row>
- </template>
- <template #column-phone="{ scope }"> 📱{{ scope.row.phone }} </template>
- </cl-table>
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { useCrud, useTable } from '@cool-vue/crud';
- import { ref } from 'vue';
- import { useDict } from '/$/dict';
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- contextMenu: ['refresh'],
- columns: [
- {
- headerAlign: 'left',
- label: '姓名',
- prop: 'name',
- minWidth: 140
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140
- },
- {
- label: '工作',
- prop: 'occupation',
- dict: dict.get('occupation'),
- minWidth: 140
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc'
- }
- ]
- });
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- </script>
- ```
- ## 合并行或列 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>span-method</el-tag>
- <span>合并行或列</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/span-method.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="合并行或列" width="80%">
- <cl-crud ref="Crud">
- <cl-row>
- <cl-table ref="Table" :span-method="onSpanMethod" />
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { useCrud, useTable } from '@cool-vue/crud';
- import { ref } from 'vue';
- import { useDict } from '/$/dict';
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- contextMenu: ['refresh'],
- columns: [
- {
- label: '姓名',
- prop: 'name',
- minWidth: 140
- },
- {
- label: '存款',
- prop: 'wages',
- minWidth: 140
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140
- },
- {
- label: '工作',
- prop: 'occupation',
- dict: dict.get('occupation'),
- minWidth: 140
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc'
- }
- ]
- });
- interface SpanMethodProps {
- row: any;
- column: any;
- rowIndex: number;
- columnIndex: number;
- }
- function onSpanMethod({ row, column, rowIndex, columnIndex }: SpanMethodProps) {
- // 根据实际业务需求调整返回值 { rowspan, colspan }
- if (columnIndex === 0) {
- if (rowIndex % 2 === 0) {
- return {
- rowspan: 2,
- colspan: 1
- };
- } else {
- return {
- rowspan: 0,
- colspan: 0
- };
- }
- }
- }
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- </script>
- ```
- ## 表尾合计行 示例
- ```vue
- <template>
- <div class="scope">
- <div class="h">
- <el-tag size="small" effect="dark" disable-transitions>summary</el-tag>
- <span>表尾合计行</span>
- </div>
- <div class="c">
- <el-button @click="open">预览</el-button>
- <demo-code :files="['table/summary.vue']" />
- <!-- 自定义表格组件 -->
- <cl-dialog v-model="visible" title="表尾合计行" width="80%">
- <cl-crud ref="Crud">
- <cl-row>
- <cl-table ref="Table" show-summary :summary-method="getSummaries" />
- </cl-row>
- <cl-row>
- <cl-flex1 />
- <cl-pagination />
- </cl-row>
- </cl-crud>
- </cl-dialog>
- </div>
- <div class="f">
- <span class="date">2024-01-01</span>
- </div>
- </div>
- </template>
- <script setup lang="ts">
- import { useCrud, useTable } from '@cool-vue/crud';
- import { ref } from 'vue';
- import { useDict } from '/$/dict';
- const { dict } = useDict();
- // cl-crud 配置
- const Crud = useCrud(
- {
- service: 'test'
- },
- app => {
- app.refresh();
- }
- );
- // cl-table 配置
- const Table = useTable({
- autoHeight: false,
- contextMenu: ['refresh'],
- columns: [
- {
- label: '姓名',
- prop: 'name',
- minWidth: 140
- },
- {
- label: '存款',
- prop: 'wages',
- minWidth: 140
- },
- {
- label: '手机号',
- prop: 'phone',
- minWidth: 140
- },
- {
- label: '工作',
- prop: 'occupation',
- dict: dict.get('occupation'),
- minWidth: 140
- },
- {
- label: '创建时间',
- prop: 'createTime',
- minWidth: 170,
- sortable: 'desc'
- }
- ]
- });
- function getSummaries() {
- return ['合计', '$' + Table.value?.data.reduce((a, b) => a + b.wages, 0)];
- }
- const visible = ref(false);
- function open() {
- visible.value = true;
- }
- </script>
- ```
|