153 lines
3.8 KiB
JavaScript
153 lines
3.8 KiB
JavaScript
import { updateColumnsApi } from "@/request/api";
|
|
import { ElMessage } from "element-plus";
|
|
import { formatToDate, formatToDateTime } from "./time";
|
|
|
|
export function updateEditTableColumns(tableRef) {
|
|
const $table = unref(tableRef);
|
|
|
|
if (!$table || typeof $table.getColumns !== "function") {
|
|
ElMessage.warning("表格尚未初始化完成");
|
|
return;
|
|
}
|
|
|
|
const columns = $table.getColumns();
|
|
if (!$table || $table.length === 0) {
|
|
ElMessage.warning("未获取到栏位信息");
|
|
return;
|
|
}
|
|
|
|
const newColumns = columns
|
|
.filter((item) => item.params.b_id !== undefined)
|
|
.map((item, index) => ({
|
|
b_id: item.params.b_id,
|
|
col_edit_width: item.renderWidth,
|
|
col_edit_position: index,
|
|
}));
|
|
|
|
updateColumnsApi(newColumns).then((res) => {
|
|
if (res.success) {
|
|
ElMessage.success("保存成功");
|
|
}
|
|
});
|
|
}
|
|
|
|
export function updateViewTableColumns(tableRef) {
|
|
const $table = unref(tableRef);
|
|
|
|
if (!$table || typeof $table.getColumns !== "function") {
|
|
ElMessage.warning("表格尚未初始化完成");
|
|
return;
|
|
}
|
|
|
|
const columns = $table.getColumns();
|
|
console.log(columns);
|
|
|
|
if (!$table || $table.length === 0) {
|
|
ElMessage.warning("未获取到栏位信息");
|
|
return;
|
|
}
|
|
|
|
const newColumns = columns
|
|
.filter((item) => item.type === undefined && item.params && item.params.b_id)
|
|
.map((item, index) => ({
|
|
b_id: item.params.b_id,
|
|
col_Width: item.renderWidth,
|
|
col_Position: index,
|
|
}));
|
|
|
|
console.log(newColumns);
|
|
|
|
updateColumnsApi(newColumns).then((res) => {
|
|
if (res.success) {
|
|
ElMessage.success("保存成功");
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 根据 format 判断列对齐方式
|
|
* @param {string} format
|
|
*/
|
|
export function getAlignByFormat(format) {
|
|
if (!format || typeof format !== "string") return "left";
|
|
|
|
const fmt = format.toLowerCase();
|
|
if (fmt.startsWith("money") || fmt.startsWith("number")) {
|
|
return "right";
|
|
}
|
|
|
|
if (fmt === "checkbox") {
|
|
return "center";
|
|
}
|
|
|
|
return "left";
|
|
}
|
|
|
|
/**
|
|
* 根据 format 格式化值
|
|
* @param {any} value 原始值
|
|
* @param {string} format 格式描述,如 money(2)、percent(1)
|
|
*/
|
|
export function formatValueByFormat(value, format) {
|
|
if (value === null || value === undefined || value === "") return "";
|
|
|
|
if (!format || typeof format !== "string") return value;
|
|
|
|
const fmt = format.trim().toLowerCase();
|
|
|
|
// money(n)
|
|
if (fmt.startsWith("money")) {
|
|
const match = fmt.match(/^money\((\d+)\)$/);
|
|
const decimals = match ? Number(match[1]) : 2;
|
|
|
|
const num = Number.parseFloat(value);
|
|
if (Number.isNaN(num)) return value;
|
|
|
|
// 使用Intl.NumberFormat来处理千分位
|
|
return new Intl.NumberFormat("en-US", {
|
|
minimumFractionDigits: decimals,
|
|
maximumFractionDigits: decimals,
|
|
}).format(num);
|
|
} else if (fmt === "date") {
|
|
return formatToDate(value);
|
|
} else if (fmt === "datetime") {
|
|
return formatToDateTime(value);
|
|
} else if (fmt === "number") {
|
|
return Number(value) || 0;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* 根据 format 格式化值
|
|
* @param {any} value 原始值
|
|
* @param {string} format 格式描述,如 money(2)、percent(1)
|
|
*/
|
|
export function formatValueByFullFormat(value, format) {
|
|
if (value === null || value === undefined || value === "") return "";
|
|
|
|
if (!format || typeof format !== "string") return value;
|
|
|
|
const fmt = format.trim().toLowerCase();
|
|
|
|
// money(n)
|
|
if (fmt.startsWith("money")) {
|
|
const match = fmt.match(/^money\((\d+)\)$/);
|
|
const decimals = match ? Number(match[1]) : 2;
|
|
|
|
const num = Number.parseFloat(value);
|
|
if (Number.isNaN(num)) return value;
|
|
|
|
return num.toFixed(decimals);
|
|
} else if (fmt === "date") {
|
|
return formatToDate(value);
|
|
} else if (fmt === "datetime") {
|
|
return formatToDateTime(value);
|
|
} else if (fmt === "number") {
|
|
return Number(value) || 0;
|
|
}
|
|
|
|
return value;
|
|
}
|