import type {
LoadingConfig,
AdaptiveConfig,
PaginationProps
} from "@pureadmin/table";
import { ref, reactive } from "vue";
import { delObjectProperty } from "@pureadmin/utils";
import { clone, delay } from "@pureadmin/utils";
import Anchor from "@/model/anchor.js";
export function useColumns() {
const loading = ref(true);
const editMap = ref({});
const dataList = ref([]);
const options = ref([]);
const columns: TableColumnList = [
{
label: "ID",
prop: "id",
cellRenderer: ({ row, index }) => (
<>
{editMap.value[index]?.editable ? (
) : (
{row.id}
)}
>
)
},
{
label: "IMEI",
prop: "imei",
cellRenderer: ({ row, index }) => (
<>
{editMap.value[index]?.editable ? (
) : (
{row.imei}
)}
>
)
},
{
label: "ICCID",
prop: "iccid",
cellRenderer: ({ row, index }) => (
<>
{editMap.value[index]?.editable ? (
) : (
{row.iccid}
)}
>
)
},
{
label: "项目ID",
prop: "projId",
cellRenderer: ({ row, index }) => (
<>
{editMap.value[index]?.editable ? (
{options.value.map(item => {
return (
);
})}
) : (
{options.value.filter(opt => opt.value == row.projId)[0]?.label}
)}
>
)
},
{
label: "电池",
prop: "battery",
cellRenderer: ({ row, index }) => (
{row.battery}
)
},
{
label: "状态",
prop: "status",
cellRenderer: ({ row, index }) => (
{row.status}
)
},
{
label: "操作",
fixed: "right",
slot: "operation"
}
];
/** 分页配置 */
const pagination = reactive({
pageSize: 20,
currentPage: 1,
pageSizes: [20, 40, 60],
total: 0,
align: "right",
background: true,
size: "default"
});
/** 加载动画配置 */
const loadingConfig = reactive({
text: "正在加载第一页...",
viewBox: "-10, -10, 50, 50",
spinner: `
`
// svg: "",
// background: rgba()
});
/** 撑满内容区自适应高度相关配置 */
const adaptiveConfig: AdaptiveConfig = {
/** 表格距离页面底部的偏移量,默认值为 `96` */
offsetBottom: 110
/** 是否固定表头,默认值为 `true`(如果不想固定表头,fixHeader设置为false并且表格要设置table-layout="auto") */
// fixHeader: true
/** 页面 `resize` 时的防抖时间,默认值为 `60` ms */
// timeout: 60
/** 表头的 `z-index`,默认值为 `100` */
// zIndex: 100
};
function onSizeChange(val) {
console.log("onSizeChange", val);
}
function onCurrentChange(val) {
loadingConfig.text = `正在加载第${val}页...`;
loading.value = true;
delay(600).then(() => {
loading.value = false;
});
}
function onEdit(row, index) {
editMap.value[index] = Object.assign({ ...row, editable: true });
}
function onSave(index) {
editMap.value[index].editable = false;
}
function onCancel(index) {
editMap.value[index].editable = false;
dataList.value[index] = delObjectProperty(editMap.value[index], "editable");
}
function onAdd() {
const anchor = new Anchor();
anchor.id = dataList.value.length + 1;
dataList.value.push(anchor);
}
function onDel(row) {
const index = dataList.value.indexOf(row);
if (index !== -1) dataList.value.splice(index, 1);
}
return {
loading,
editMap,
columns,
dataList,
options,
pagination,
loadingConfig,
adaptiveConfig,
onAdd,
onDel,
onEdit,
onSave,
onCancel,
onSizeChange,
onCurrentChange
};
}