代码运行效果图
| <template> |
| <!-- 使用 customRow 方法设置行属性 --> |
| <a-table |
| :columns="staffColumnsSort" |
| :dataSource="dataSource" |
| :rowKey="record => record.staffId" |
| :customRow="isSort ? customRow : null" |
| > |
| |
| <template #sort> |
| <HolderOutlined /> |
| </template> |
| </a-table> |
| </template> |
| |
| <script> |
| import { ref } from 'vue' |
| import { staffColumnsSort} from '../../columns' |
| import { HolderOutlined } from '@ant-design/icons-vue' |
| export default{ |
| components: { |
| HolderOutlined |
| }, |
| setup(){ |
| const sourceObj = ref({}) |
| const targetObj = ref({}) |
| const dataSource = ref([]) |
| const sortInfo = ref([]) |
| const isSort = ref(true) |
| |
| const removeStyle = () => { |
| document.getElementsByClassName('ant-table-tbody').forEach(parent => { |
| parent.getElementsByTagName('tr').forEach(child => { |
| child.removeAttribute('style') |
| }) |
| }) |
| } |
| const customRow = (record, index) => { |
| return { |
| style: { |
| cursor: 'grab' |
| }, |
| |
| onMouseenter: event => { |
| |
| var ev = event || window.event |
| ev.target.draggable = true |
| }, |
| |
| onDragstart: event => { |
| |
| var ev = event || window.event |
| |
| ev.stopPropagation() |
| |
| sourceObj.value = record |
| }, |
| |
| onDragover: event => { |
| |
| var ev = event || window.event |
| |
| ev.preventDefault() |
| console.log('ant-table-tbody元素个数', document.getElementsByClassName('ant-table-tbody')) |
| console.log('拖动元素经过的元素索引', index) |
| console.log( |
| '表格子元素', |
| document.getElementsByClassName('ant-table-tbody')[0].getElementsByTagName('tr')[index] |
| ) |
| |
| |
| document.getElementsByClassName('ant-table-tbody').forEach(parent => { |
| parent.getElementsByTagName('tr').forEach((item, i) => { |
| if (i === index) { |
| item.style.borderBottom = '2px solid #1677ff' |
| } else { |
| item.removeAttribute('style') |
| } |
| }) |
| }) |
| }, |
| |
| onDrop: event => { |
| |
| var ev = event || window.event |
| |
| ev.stopPropagation() |
| |
| targetObj.value = record |
| console.log('源数据', sourceObj.value) |
| console.log('目标数据', targetObj.value) |
| |
| const newArr = dataSource.value |
| |
| const source = newArr.findIndex(item => item.staffId ==sourceObj.value.staffId) |
| |
| const target = newArr.findIndex(item => item.staffId ==targetObj.value.staffId) |
| |
| newArr.splice(source, 1) |
| newArr.splice(target, 0, sourceObj.value) |
| |
| sortInfo.value = [] |
| newArr.forEach((item, index) => { |
| sortInfo.value.push({ |
| staffId: item.staffId, |
| sortIndex: index |
| }) |
| }) |
| |
| removeStyle() |
| }, |
| |
| onMouseleave: event => { |
| |
| var ev = event || window.event |
| |
| ev.target.draggable = false |
| removeStyle() |
| |
| } |
| } |
| } |
| |
| return { |
| sourceObj, |
| targetObj, |
| dataSource, |
| sortInfo, |
| removeStyle, |
| customRow, |
| isSort |
| } |
| } |
| } |
| </script> |
复制
| |
| export const staffColumnsSort = [ |
| { |
| title: '', |
| dataIndex: 'sort', |
| key: 'sort', |
| align: 'left', |
| width: 92, |
| slots: { customRender: 'sort' } |
| }, |
| { |
| title: '序号', |
| dataIndex: 'id1', |
| key: 'id1', |
| align: 'left', |
| width: 117, |
| slots: { customRender: 'id1' } |
| }, |
| { |
| title: '姓名', |
| dataIndex: 'staffName', |
| key: 'staffName', |
| align: 'left', |
| slots: { customRender: 'staffName' }, |
| ellipsis: true, |
| width: 225 |
| }, |
| { |
| title: '账号', |
| dataIndex: 'mobile', |
| key: 'mobile', |
| align: 'left', |
| ellipsis: true, |
| slots: { customRender: 'mobile' }, |
| width: 174 |
| }, |
| { |
| title: '所属组织', |
| dataIndex: 'departmentName', |
| key: 'departmentName', |
| align: 'left', |
| ellipsis: true, |
| slots: { customRender: 'departmentName' }, |
| width: 172 |
| }, |
| { |
| title: '指纹录入', |
| dataIndex: 'fingerprintEntry', |
| key: 'fingerprintEntry', |
| align: 'left', |
| width: 144, |
| ellipsis: true, |
| slots: { customRender: 'fingerprintEntry' } |
| }, |
| { |
| title: '操作', |
| dataIndex: 'action', |
| key: 'action', |
| align: 'left', |
| slots: { customRender: 'action' }, |
| fixed: 'right', |
| width: 124 |
| } |
| ] |
复制