想实现带复选框可拖拽伸缩的表格,
<template>
<div>
<div style="margin-bottom: 16px">
<a-button type="primary" :disabled="!hasSelected" :loading="loading" @click="start"> Reload </a-button>
<span style="margin-left: 8px">
<template v-if="hasSelected">
{{ `Selected ${selectedRowKeys.length} items` }}
</template>
</span>
</div>
<a-table
bordered
:columns="columns"
:components="components"
:data-source="data"
:row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
>
<template v-slot:action>
<a href="javascript:;">Delete</a>
</template>
</a-table>
</div>
</template>
<script>
import Vue from "vue";
import VueDraggableResizable from "vue-draggable-resizable";
Vue.component("vue-draggable-resizable", VueDraggableResizable);
const columns = [
{
title: "Date",
dataIndex: "date",
width: 200,
},
{
title: "Amount",
dataIndex: "amount",
width: 100,
},
{
title: "Type",
dataIndex: "type",
width: 100,
},
{
title: "Note",
dataIndex: "note",
width: 200,
},
{
title: "Action",
key: "action",
width: 200,
// fixed: 'right',
scopedSlots: { customRender: "action" },
},
];
const data = [
{
key: 0,
date: "2018-02-11",
amount: 120,
type: "income",
note: "transfer",
},
{
key: 1,
date: "2018-03-11",
amount: 243,
type: "income",
note: "transfer",
},
{
key: 2,
date: "2018-04-11",
amount: 98,
type: "income",
note: "transfer",
},
];
const draggingMap = {};
columns.forEach((col) => {
draggingMap[col.key] = col.width;
});
const draggingState = Vue.observable(draggingMap);
const resizeableTitle = (h, props, children) => {
let thDom = null;
const { key, ...restProps } = props;
const col = columns.find((col) => {
const k = col.dataIndex || col.key;
return k === key;
});
if (!col || !col.width) {
return <th {...restProps}>{children}</th>;
}
const onDrag = (x) => {
draggingState[key] = 0;
col.width = Math.max(x, 1);
};
const onDragstop = () => {
draggingState[key] = thDom.getBoundingClientRect().width;
};
return (
<th
{...restProps}
v-ant-ref={(r) => (thDom = r)}
width={col.width}
class="resize-table-th"
>
{children}
<vue-draggable-resizable
key={col.key}
class="table-draggable-handle"
w={10}
x={draggingState[key] || col.width}
z={1}
axis="x"
draggable={true}
resizable={false}
onDragging={onDrag}
onDragstop={onDragstop}
></vue-draggable-resizable>
</th>
);
};
export default {
name: "App",
computed: {
hasSelected() {
return this.selectedRowKeys.length > 0;
},
},
data() {
this.components = {
header: {
cell: resizeableTitle,
},
};
return {
data,
columns,
selectedRowKeys: [], // Check here to configure the default column
loading: false,
};
},
methods:{
start() {
this.loading = true;
// ajax request after empty completing
setTimeout(() => {
this.loading = false;
this.selectedRowKeys = [];
}, 1000);
},
onSelectChange(selectedRowKeys) {
console.log('selectedRowKeys changed: ', selectedRowKeys);
this.selectedRowKeys = selectedRowKeys;
},
}
};
</script>
<style lang="less">
.resize-table-th {
position: relative;
.table-draggable-handle {
transform: none !important;
position: absolute;
height: 100% !important;
bottom: 0;
left: auto !important;
right: -5px;
cursor: col-resize;
touch-action: none;
}
}
</style>
此代码只是解决简单的带复选框的可拖拽伸缩,但是当固定列时,就会有问题