import { tableDataEdit } from "../data"; import { message } from "@/utils/message"; import { ref, computed, Transition } from "vue"; import { clone, delay } from "@pureadmin/utils"; import EditPen from "@iconify-icons/ep/edit-pen"; import Check from "@iconify-icons/ep/check"; // 温馨提示:修改整行方法雷同,将cellRenderer后面渲染的组件抽出来做对应处理即可 export function useColumns() { // 修改值(可多个) const inputValMap = ref({}); // 是否正处于修改状态(可多个) const editStatus = ref({}); // 当前激活的单元格(唯一) const activeIndex = ref(-1); const dataList = ref(clone(tableDataEdit, true)); const comVal = computed(() => { return index => { return inputValMap.value[index]?.value; }; }); const editing = computed(() => { return index => { return editStatus.value[index]?.editing; }; }); const iconClass = computed(() => { return (index, other = false) => { return [ "cursor-pointer", "ml-2", "transition", "delay-100", other ? ["hover:scale-110", "hover:text-red-500"] : editing.value(index) && ["scale-150", "text-red-500"] ]; }; }); const columns: TableColumnList = [ { label: "ID(可修改)", prop: "id", // class="flex-bc" flex-bc 代表 flex justify-between items-center 具体看 src/style/tailwind.css 文件 cellRenderer: ({ row, index }) => (
(activeIndex.value = index)} onMouseleave={() => onMouseleave(index)} >

{row.id}

onChange(value, index)} /> onSure(index)} /> onEdit(row, index)} />
) }, { label: "日期", prop: "date" }, { label: "姓名", prop: "name" }, { label: "地址", prop: "address" } ]; function onEdit({ id }, index) { inputValMap.value[index] = Object.assign({}, inputValMap.value[index], { value: id }); // 处于修改状态 editStatus.value[index] = Object.assign({}, editStatus.value[index], { editing: true }); } function onMouseleave(index) { inputValMap.value[index]?.value ? (activeIndex.value = index) : (activeIndex.value = -1); } function onChange(value, index) { inputValMap.value[index].value = value; } function onSure(index) { dataList.value[index].id = inputValMap.value[index].value; message( `您修改了第 ${index + 1} 行,修改后数据为:${JSON.stringify( dataList.value[index] )}`, { type: "success" } ); // 修改状态关闭 editStatus.value[index] = Object.assign({}, editStatus.value[index], { editing: false }); delay().then(() => (inputValMap.value[index].value = null)); } return { columns, dataList }; }