layui/src/module/table/index.vue

251 lines
7.1 KiB
Vue
Raw Normal View History

2021-10-09 02:41:52 +00:00
<template>
2021-10-13 16:49:27 +00:00
<div id="lay-table">
<table class="layui-hide" lay-filter="test" />
2021-10-09 02:41:52 +00:00
<div
class="layui-form layui-border-box layui-table-view layui-table-view-1"
>
<div v-if="defaultToolbar || slot.toolbar" class="layui-table-tool">
2021-10-15 01:46:45 +00:00
<div v-if="slot.toolbar" class="layui-table-tool-temp">
<slot name="toolbar"></slot>
</div>
<div v-if="defaultToolbar" class="layui-table-tool-self">
2021-11-07 07:55:08 +00:00
<lay-dropdown>
<div class="layui-inline" title="筛选列" lay-event="LAYTABLE_COLS">
<i class="layui-icon layui-icon-cols"></i>
</div>
<template #content>
<div style="padding:10px">
<table-item-checkbox
skin="primary"
v-for="column in columns"
v-model="tableColumns"
:label="column"
:key="column"
>{{ column.title }}</table-item-checkbox
>
</div>
</template>
</lay-dropdown>
<div
class="layui-inline"
title="打印"
lay-event="LAYTABLE_PRINT"
@click="print()"
>
<i class="layui-icon layui-icon-print" />
2021-10-09 02:41:52 +00:00
</div>
</div>
</div>
2021-10-09 02:41:52 +00:00
<div class="layui-table-box">
<!-- 表格头部 -->
2021-10-09 02:41:52 +00:00
<div class="layui-table-header">
<table class="layui-table" :lay-size="size">
2021-10-09 02:41:52 +00:00
<thead>
<tr>
<th v-if="checkbox" class="layui-table-col-special">
2021-10-09 21:48:45 +00:00
<div class="layui-table-cell laytable-cell-checkbox">
2021-10-11 07:46:44 +00:00
<lay-checkbox
v-model:checked="allChecked"
skin="primary"
2021-10-11 07:46:44 +00:00
label="all"
@change="changeAll"
/>
2021-10-09 02:41:52 +00:00
</div>
</th>
2021-11-07 07:55:08 +00:00
<template v-for="column in columns" :key="column">
<th v-if="tableColumns.includes(column)">
<div
class="layui-table-cell"
:style="{ width: column.width }"
>
<span>{{ column.title }}</span>
</div>
</th>
</template>
2021-10-09 02:41:52 +00:00
</tr>
</thead>
</table>
</div>
<!-- 表格数据 -->
2021-10-09 02:41:52 +00:00
<div class="layui-table-body layui-table-main">
<table class="layui-table" :lay-size="size">
2021-10-09 02:41:52 +00:00
<tbody>
2021-10-09 21:48:45 +00:00
<template v-for="data in dataSource" :key="data">
2021-11-04 17:26:20 +00:00
<tr
@click.stop="rowClick(data)"
@dblclick.stop="rowDoubleClick(data)"
>
<td v-if="checkbox" class="layui-table-col-special">
2021-10-09 21:48:45 +00:00
<div class="layui-table-cell laytable-cell-checkbox">
<table-item-checkbox
v-model="tableSelectedKeys"
skin="primary"
2021-10-11 07:46:44 +00:00
:label="data[id]"
>
</table-item-checkbox>
2021-10-09 02:41:52 +00:00
</div>
2021-10-09 21:48:45 +00:00
</td>
2021-11-07 07:55:08 +00:00
<template v-for="column in columns" :key="column">
2021-11-07 07:55:08 +00:00
<template v-if="tableColumns.includes(column)">
<template v-if="column.customSlot">
<td class="layui-table-cell">
<div :style="{ width: column.width }">
<slot :name="column.customSlot" :data="data" />
</div>
</td>
</template>
2021-11-07 07:55:08 +00:00
<template
v-else
v-for="(value, key) in data"
:key="value"
>
<td v-if="column.key == key" class="layui-table-cell">
<div :style="{ width: column.width }">
<span v-if="column.slot">
<slot :name="column.slot" :data="data" />
</span>
<span v-else> {{ value }} </span>
</div>
</td>
</template>
2021-10-09 21:48:45 +00:00
</template>
</template>
</tr>
</template>
2021-10-09 02:41:52 +00:00
</tbody>
</table>
</div>
</div>
<div v-if="page" class="layui-table-page">
<lay-page
:total="page.total"
:limit="page.limit"
show-page
show-limit
show-skip
@jump="change"
>
2021-11-04 17:26:20 +00:00
<template #prev
><lay-icon type="layui-icon-left"></lay-icon
></template>
2021-11-04 17:26:20 +00:00
<template #next
><lay-icon type="layui-icon-right"></lay-icon
></template>
</lay-page>
2021-10-09 02:41:52 +00:00
</div>
</div>
</div>
</template>
<script lang="ts">
export default {
name: 'LayTable',
}
</script>
<script setup lang="ts">
import tableItemCheckbox from './component/checkbox.vue'
2021-10-11 07:46:44 +00:00
import {
defineProps,
ref,
useSlots,
watch,
withDefaults,
2021-11-07 07:55:08 +00:00
defineEmits
2021-10-11 07:46:44 +00:00
} from 'vue'
import { Recordable } from '/@src/module/type'
2021-10-09 17:07:37 +00:00
const props = withDefaults(
2021-10-09 17:07:37 +00:00
defineProps<{
2021-10-11 07:46:44 +00:00
id?: string
2021-10-09 21:48:45 +00:00
skin?: string
size?: string
page?: Recordable
checkbox?: boolean
columns?: Recordable[]
dataSource: Recordable[]
defaultToolbar?: boolean
2021-10-15 01:46:45 +00:00
selectedKeys?: Array<string>
}>(),
{
2021-10-11 07:46:44 +00:00
id: 'id',
dataSource: function () {
return []
},
selectedKeys: function () {
return []
},
size: 'md',
}
)
2021-11-04 17:26:20 +00:00
const emit = defineEmits(['change', 'row', 'row-double', 'update:selectedKeys'])
2021-10-09 17:07:37 +00:00
const slot = useSlots()
const slots = slot.default && slot.default()
const allChecked = ref(false)
const tableSelectedKeys = ref([...props.selectedKeys])
2021-11-07 07:55:08 +00:00
const tableColumns = ref([...props.columns])
const changeAll = function ({ checked, value }: any) {
const ids = props.dataSource.map((item: any) => {
return item[props.id]
})
tableSelectedKeys.value.splice(0, ids.length)
if (checked) {
ids.forEach((id) => {
tableSelectedKeys.value.push(id)
2021-10-11 07:46:44 +00:00
})
}
2021-10-13 16:49:27 +00:00
emit('update:selectedKeys', tableSelectedKeys.value)
}
watch(
tableSelectedKeys,
function () {
if (tableSelectedKeys.value.length === props.dataSource.length) {
allChecked.value = true
} else {
allChecked.value = false
}
2021-10-13 16:49:27 +00:00
emit('update:selectedKeys', tableSelectedKeys.value)
2021-10-11 07:46:44 +00:00
},
{ deep: true }
)
const change = function (page: any) {
emit('change', page)
}
2021-10-13 16:49:27 +00:00
2021-11-04 17:26:20 +00:00
const rowClick = function (data: any) {
emit('row', data)
}
2021-11-04 17:26:20 +00:00
const rowDoubleClick = function (data: any) {
emit('row-double', data)
}
2021-10-13 16:49:27 +00:00
const print = function () {
let subOutputRankPrint = document.getElementById('lay-table') as HTMLElement
let newContent = subOutputRankPrint.innerHTML
let oldContent = document.body.innerHTML
document.body.innerHTML = newContent
window.print()
window.location.reload()
document.body.innerHTML = oldContent
}
</script>
2021-11-07 07:55:08 +00:00
<style scoped>
.laytable-cell-checkbox {
width: 34px;
}
.layui-table-col-special {
width: 34px;
}
</style>