♻️(table): 重构 checkbox 实现方式

This commit is contained in:
就眠儀式 2022-07-18 15:25:43 +08:00
parent f37d9c3f0c
commit 3b7e0ff326
3 changed files with 243 additions and 72 deletions

View File

@ -178,22 +178,11 @@ const renderFixedClassName = (column: any, columnIndex: number) => {
@dblclick.stop="rowDoubleClick(data, $event)" @dblclick.stop="rowDoubleClick(data, $event)"
@contextmenu.stop="contextmenu(data, $event)" @contextmenu.stop="contextmenu(data, $event)"
> >
<!-- 复选框 -->
<td v-if="checkbox" class="layui-table-col-special">
<div class="layui-table-cell laytable-cell-checkbox">
<lay-checkbox
v-model="tableSelectedKeys"
:value="data[id]"
skin="primary"
/>
</div>
</td>
<!-- 数据列 --> <!-- 数据列 -->
<template v-for="(column, columnIndex) in columns" :key="columnIndex"> <template v-for="(column, columnIndex) in columns" :key="columnIndex">
<!-- 展示否 --> <!-- 展示否 -->
<template v-if="tableColumnKeys.includes(column.key)"> <template v-if="tableColumnKeys.includes(column.key)">
<template v-if="column.type == 'number'"> <template v-if="column.type == 'checkbox'">
<td <td
class="layui-table-cell" class="layui-table-cell"
:style="[ :style="[
@ -205,6 +194,7 @@ const renderFixedClassName = (column: any, columnIndex: number) => {
renderCellStyle(data, column, index, columnIndex), renderCellStyle(data, column, index, columnIndex),
]" ]"
:class="[ :class="[
renderFixedClassName(column, columnIndex),
renderCellClassName(data, column, index, columnIndex), renderCellClassName(data, column, index, columnIndex),
column.fixed ? `layui-table-fixed-${column.fixed}` : '', column.fixed ? `layui-table-fixed-${column.fixed}` : '',
]" ]"
@ -235,13 +225,57 @@ const renderFixedClassName = (column: any, columnIndex: number) => {
@click="handleExpand" @click="handleExpand"
></lay-icon> ></lay-icon>
<lay-tooltip <lay-checkbox
v-if="column.ellipsisTooltip" v-model="tableSelectedKeys"
:content="data[column.key]" :value="data[id]"
:isAutoShow="true" skin="primary"
/>
</td>
</template>
<template v-if="column.type == 'number'">
<td
class="layui-table-cell"
:style="[
{
textAlign: column.align,
whiteSpace: column.ellipsisTooltip ? 'nowrap' : 'normal',
},
renderFixedStyle(column, columnIndex),
renderCellStyle(data, column, index, columnIndex),
]"
:class="[
renderFixedClassName(column, columnIndex),
renderCellClassName(data, column, index, columnIndex),
column.fixed ? `layui-table-fixed-${column.fixed}` : '',
]"
> >
{{ index + 1 }} <!-- 树表占位与缩进 -->
</lay-tooltip> <span
v-if="expandSpace && columnIndex === expandIndex"
:style="{ 'margin-right': currentIndentSize + 'px' }"
></span>
<span
v-if="
expandSpace &&
!data[childrenColumnName] &&
!slot.expand &&
columnIndex === expandIndex
"
class="layui-table-cell-expand-icon-spaced"
></span>
<lay-icon
v-if="
(slot.expand || data[childrenColumnName]) &&
columnIndex === expandIndex
"
class="layui-table-cell-expand-icon"
:type="expandIconType"
@click="handleExpand"
></lay-icon>
{{ index + 1 }} {{ index + 1 }}
</td> </td>
</template> </template>
@ -259,6 +293,7 @@ const renderFixedClassName = (column: any, columnIndex: number) => {
renderCellStyle(data, column, index, columnIndex), renderCellStyle(data, column, index, columnIndex),
]" ]"
:class="[ :class="[
renderFixedClassName(column, columnIndex),
renderCellClassName(data, column, index, columnIndex), renderCellClassName(data, column, index, columnIndex),
column.fixed ? `layui-table-fixed-${column.fixed}` : '', column.fixed ? `layui-table-fixed-${column.fixed}` : '',
]" ]"

View File

@ -6,7 +6,15 @@ export default {
<script setup lang="ts"> <script setup lang="ts">
import "./index.less"; import "./index.less";
import { ref, watch, useSlots, withDefaults, onMounted, onUpdated, StyleValue } from "vue"; import {
ref,
watch,
useSlots,
withDefaults,
onMounted,
onUpdated,
StyleValue,
} from "vue";
import { v4 as uuidv4 } from "../../utils/guidUtil"; import { v4 as uuidv4 } from "../../utils/guidUtil";
import { Recordable } from "../../types"; import { Recordable } from "../../types";
import { LayIcon } from "@layui/icons-vue"; import { LayIcon } from "@layui/icons-vue";
@ -20,8 +28,6 @@ export interface LayTableProps {
skin?: string; skin?: string;
size?: string; size?: string;
page?: Recordable; page?: Recordable;
checkbox?: boolean;
radio?: boolean;
columns: Recordable[]; columns: Recordable[];
dataSource: Recordable[]; dataSource: Recordable[];
defaultToolbar?: boolean; defaultToolbar?: boolean;
@ -69,7 +75,7 @@ const slots = slot.default && slot.default();
const allChecked = ref(false); const allChecked = ref(false);
const hasChecked = ref(false); const hasChecked = ref(false);
const tableDataSource = ref([...props.dataSource]); const tableDataSource = ref<any[]>([...props.dataSource]);
const tableSelectedKeys = ref<Recordable[]>([...props.selectedKeys]); const tableSelectedKeys = ref<Recordable[]>([...props.selectedKeys]);
const tableColumns = ref([...props.columns]); const tableColumns = ref([...props.columns]);
const tableColumnKeys = ref( const tableColumnKeys = ref(
@ -352,7 +358,6 @@ const renderFixedClassName = (column: any, columnIndex: number) => {
<div class="layui-table-header" ref="tableHeader"> <div class="layui-table-header" ref="tableHeader">
<table class="layui-table" :lay-size="size" :lay-skin="skin"> <table class="layui-table" :lay-size="size" :lay-skin="skin">
<colgroup> <colgroup>
<col v-if="checkbox" class="layui-table-col-special" />
<template v-for="column in columns" :key="column"> <template v-for="column in columns" :key="column">
<template v-if="tableColumnKeys.includes(column.key)"> <template v-if="tableColumnKeys.includes(column.key)">
<col <col
@ -367,17 +372,6 @@ const renderFixedClassName = (column: any, columnIndex: number) => {
</colgroup> </colgroup>
<thead> <thead>
<tr> <tr>
<th v-if="checkbox" class="layui-table-col-special">
<div class="layui-table-cell laytable-cell-checkbox">
<lay-checkbox
v-model="hasChecked"
:is-indeterminate="!allChecked"
skin="primary"
value="all"
@change="changeAll"
/>
</div>
</th>
<template <template
v-for="(column, columnIndex) in columns" v-for="(column, columnIndex) in columns"
:key="column" :key="column"
@ -396,6 +390,16 @@ const renderFixedClassName = (column: any, columnIndex: number) => {
renderFixedStyle(column, columnIndex), renderFixedStyle(column, columnIndex),
]" ]"
> >
<template v-if="column.type == 'checkbox'">
<lay-checkbox
v-model="hasChecked"
:is-indeterminate="!allChecked"
skin="primary"
value="all"
@change="changeAll"
/>
</template>
<template v-else>
<span> <span>
<template v-if="column.titleSlot"> <template v-if="column.titleSlot">
<slot :name="column.titleSlot"></slot> <slot :name="column.titleSlot"></slot>
@ -421,6 +425,7 @@ const renderFixedClassName = (column: any, columnIndex: number) => {
title="降序" title="降序"
></i> ></i>
</span> </span>
</template>
</th> </th>
</template> </template>
<th <th
@ -447,7 +452,6 @@ const renderFixedClassName = (column: any, columnIndex: number) => {
:lay-skin="skin" :lay-skin="skin"
> >
<colgroup> <colgroup>
<col v-if="checkbox" class="layui-table-col-special" />
<template v-for="column in columns" :key="column"> <template v-for="column in columns" :key="column">
<template v-if="tableColumnKeys.includes(column.key)"> <template v-if="tableColumnKeys.includes(column.key)">
<col <col
@ -467,7 +471,6 @@ const renderFixedClassName = (column: any, columnIndex: number) => {
:index="index" :index="index"
:data="data" :data="data"
:columns="columns" :columns="columns"
:checkbox="checkbox"
:indent-size="indentSize" :indent-size="indentSize"
:currentIndentSize="currentIndentSize" :currentIndentSize="currentIndentSize"
:tableColumnKeys="tableColumnKeys" :tableColumnKeys="tableColumnKeys"

View File

@ -613,13 +613,12 @@ export default {
::: :::
::: title 刷新数据 ::: title 开启序号
::: :::
::: demo 通过 `data-source` 的赋值,实现数据的更新 ::: demo 通过 `fixed` 属性实现列固定, 可选值为 `left``right`
<template> <template>
<lay-button @click="changeDataSource21">更新数据</lay-button>
<lay-table :columns="columns21" :dataSource="dataSource21"></lay-table> <lay-table :columns="columns21" :dataSource="dataSource21"></lay-table>
</template> </template>
@ -630,6 +629,12 @@ export default {
setup() { setup() {
const columns21 = [ const columns21 = [
{
title: "ID",
width: "30px",
type: "number",
align: "center"
},
{ {
title:"账户", title:"账户",
width:"200px", width:"200px",
@ -654,7 +659,135 @@ export default {
} }
] ]
const dataSource21 = ref([ const dataSource21 = [
{username:"root", password:"root",sex:"男", age:"18", remark: 'layui - vue谐音类 UI) '},
{username:"root", password:"root",sex:"男", age:"18", remark: 'layui - vue谐音类 UI) '},
{username:"woow", password:"woow",sex:"男", age:"20", remark: 'layui - vue谐音类 UI) '},
{username:"woow", password:"woow",sex:"男", age:"20", remark: 'layui - vue谐音类 UI) '},
{username:"woow", password:"woow",sex:"男", age:"20", remark: 'layui - vue谐音类 UI) '}
]
return {
columns21,
dataSource21,
}
}
}
</script>
:::
::: title 开启多选
:::
::: demo 通过 `fixed` 属性实现列固定, 可选值为 `left``right`
<template>
<lay-table :columns="columns23" :dataSource="dataSource23"></lay-table>
</template>
<script>
import { ref } from 'vue'
export default {
setup() {
const columns23 = [
{
title: "ID",
width: "30px",
type: "checkbox",
align: "center"
},
{
title:"标识",
width:"30px",
key:"id"
},
{
title:"账户",
width:"200px",
key:"username"
},{
title:"密码",
width: "300px",
key:"password"
},{
title:"性别",
width: "300px",
key:"sex"
},{
title:"年龄",
width: "300px",
key:"age"
},{
title:"备注",
width: "180px",
key:"remark",
ellipsisTooltip: true
}
]
const dataSource23 = [
{id:"1",username:"root", password:"root",sex:"男", age:"18", remark: 'layui - vue谐音类 UI) '},
{id:"2",username:"root", password:"root",sex:"男", age:"18", remark: 'layui - vue谐音类 UI) '},
{id:"3",username:"woow", password:"woow",sex:"男", age:"20", remark: 'layui - vue谐音类 UI) '},
{id:"4",username:"woow", password:"woow",sex:"男", age:"20", remark: 'layui - vue谐音类 UI) '},
{id:"5",username:"woow", password:"woow",sex:"男", age:"20", remark: 'layui - vue谐音类 UI) '}
]
return {
columns23,
dataSource23,
}
}
}
</script>
:::
::: title 刷新数据
:::
::: demo 通过 `data-source` 的赋值,实现数据的更新
<template>
<lay-button @click="changeDataSource22">更新数据</lay-button>
<lay-table :columns="columns22" :dataSource="dataSource22"></lay-table>
</template>
<script>
import { ref } from 'vue'
export default {
setup() {
const columns22 = [
{
title:"账户",
width:"200px",
key:"username"
},{
title:"密码",
width: "300px",
key:"password"
},{
title:"性别",
width: "300px",
key:"sex"
},{
title:"年龄",
width: "300px",
key:"age"
},{
title:"备注",
width: "180px",
key:"remark",
ellipsisTooltip: true
}
]
const dataSource22 = ref([
{username:"root", password:"root",sex:"男", age:"18", remark: 'layui - vue谐音类 UI) '}, {username:"root", password:"root",sex:"男", age:"18", remark: 'layui - vue谐音类 UI) '},
{username:"root", password:"root",sex:"男", age:"18", remark: 'layui - vue谐音类 UI) '}, {username:"root", password:"root",sex:"男", age:"18", remark: 'layui - vue谐音类 UI) '},
{username:"woow", password:"woow",sex:"男", age:"20", remark: 'layui - vue谐音类 UI) '}, {username:"woow", password:"woow",sex:"男", age:"20", remark: 'layui - vue谐音类 UI) '},
@ -662,14 +795,14 @@ export default {
{username:"woow", password:"woow",sex:"男", age:"20", remark: 'layui - vue谐音类 UI) '} {username:"woow", password:"woow",sex:"男", age:"20", remark: 'layui - vue谐音类 UI) '}
]) ])
const changeDataSource21 = () => { const changeDataSource22 = () => {
dataSource21.value = [{username:"update", password:"update",sex:"boy", age:"18", remark: '更新数据 '}] dataSource21.value = [{username:"update", password:"update",sex:"boy", age:"18", remark: '更新数据 '}]
} }
return { return {
columns21, columns22,
dataSource21, dataSource22,
changeDataSource21 changeDataSource22
} }
} }
} }