Files
test/jikimo_frontend/static/src/list/custom_width.js
2023-11-27 21:30:45 +08:00

96 lines
3.5 KiB
JavaScript

/** @odoo-module */
import {patch} from '@web/core/utils/patch';
import {ListRenderer} from "@web/views/list/list_renderer";
// var {patch} = require("web.utils") 这句话也行
patch(ListRenderer.prototype, 'jikimo_frontend.ListRenderer', {
// 你可以重写或者添加一些方法和属性
// The following code manipulates the DOM directly to avoid having to wait for a
// render + patch which would occur on the next frame and cause flickering.
freezeColumnWidths() {
// console.log('ccccccccccccccccccccccccccc')
if (!this.keepColumnWidths) {
this.columnWidths = null;
}
const table = this.tableRef.el;
const headers = [...table.querySelectorAll("thead th:not(.o_list_actions_header)")];
const column_num = headers.length;
if (!this.columnWidths || !this.columnWidths.length) {
// no column widths to restore
// Set table layout auto and remove inline style to make sure that css
// rules apply (e.g. fixed width of record selector)
table.style.tableLayout = "auto";
headers.forEach((th) => {
th.style.width = null;
th.style.maxWidth = null;
});
this.setDefaultColumnWidths(column_num);
// Squeeze the table by applying a max-width on largest columns to
// ensure that it doesn't overflow
this.columnWidths = this.computeColumnWidthsFromContent();
table.style.tableLayout = "fixed";
}
headers.forEach((th, index) => {
if (!th.style.width) {
th.style.width = `${Math.floor(this.columnWidths[index])}px`;
}
});
},
setDefaultColumnWidths(column_num) {
// const bbb = this.state.columns[0].name
const widths = this.state.columns.map((col) =>
this.calculateColumnWidth(col));
// const sumOfRelativeWidths = (widths
// .filter(({ type }) => type === "relative")
// .reduce((sum, { value }) => sum + value, 0));
// 获取数组的最后一项
const lastItem = widths[widths.length - 1];
// 复制最后一项
const newItem = { ...lastItem };
// 将新的对象添加到数组的末尾
widths.push(newItem);
// 判断销售的sequence
// if (this.state.columns[0].name === "sequence") {
// widths[1] = { type: "relative", value: 0.1 };
// }
// 判断 this.state.columns 是否存在且长度大于零
if (this.state.columns && this.state.columns.length > 0
&& this.state.columns[0].name === "sequence") {
widths[1] = { type: "relative", value: 0.1 };
}
// 1 because nth-child selectors are 1-indexed,
// 2 when the first column contains
// the checkboxes to select records.
const columnOffset = this.hasSelectors ? 2 : 1;
widths.forEach(({ type, value }, width) => {
const headerEl = this.tableRef.el.querySelector(
`th:nth-child(${width + columnOffset})`);
if (type === "absolute") {
if (this.isEmpty) {
headerEl.style.width = value;
} else {
headerEl.style.minWidth = value;
}
} else if (type === "relative" && this.isEmpty) {
headerEl.style.width = `${((value / column_num) * 100
).toFixed(2)}%`;
}
});
},
}
);