在props中添加height属性, 保证在父组件中可以传值。如果父组件不传值,那么就是undefined
props: [
.....
"height",
],
最主要的是thead和tbody的样式,其中都是根据height去动态控制thead和tbody的样式,注意下面三个类所在的位置以及对应的样式。
function table(cols, fixed) {
if (fixed === "right") mark = "right";
return createElement(
"table",
{
class: {
"cvn-table-inner": true,
"cvn-table-simple": vm.theme === "simple",
"cvn-table-main": fixed === undefined,
"cvn-table-fixed-left": fixed === "left",
"cvn-table-fixed-right": fixed === "right",
"cvn-table-oneline": vm.validOneline,
},
attrs: {
"cvn-table-id": vm.tableID,
name:
fixed === "left"
? "cvn-table-fixed-left"
: fixed === "right"
? "cvn-table-fixed-right"
: "cvn-table-main",
},
},
[
createElement(
"thead",
{
class: {
"cvn-table-row-container": true,
"cvn-table-head-row": vm.height,
},
},
[parseHeader(cols)]
),
createElement(
"tbody",
{
class: {
"cvn-table-row-container": true,
"cvn-table-body-row": vm.height,
"cvn-table-body-row-left":
fixed === "left" || (mark === "right" && fixed !== "right"),
},
attrs: {
name: "cvn-table-body",
},
ref: `tableBodyRef${vm.tableID}`,
on: {
scroll: () => {
vm.throttlenScrollYWatch && vm.throttlenScrollYWatch();
},
},
},
vm.validData.length === 0
? [parseEmptyRow(cols.length, fixed === undefined ? true : false)]
: expandCol
? parseRowPairs(
cols,
fixed === undefined ? expandCol : null,
vm.validData
)
: vm.validData.map((rowData, index) =>
parseRow(cols, rowData, index)
)
),
]
);
}
相关的样式设计:
/**固定表头的样式--thead相关设置 */
.cvn-table-head-row {
width: 100%;
display: table;
}
/**固定表头的样式--tbody相关设置 */
.cvn-table-body-row {
height: v-bind(heightN);
overflow: auto;
overflow-x: hidden;
display: block;
}
.cvn-table-body-row-left {
overflow-y: hidden;
}
在2中的tbody中,绑定了一个滚动事件,就是在body滚动的时候触发的事件。
在触发事件中,主要是让固定列和其他列保持一个滚动的一致性。
做法:通过nameItem值去获取所有列的body标签,进行同步的scrollTop一致即可。
function scrollYWathEffect($$el) {
let elFixedLeftBody =
$$el.children.namedItem("cvn-table-fixed-left") &&
$$el.children
.namedItem("cvn-table-fixed-left")
.children.namedItem("cvn-table-body");
let elFixedRightBody =
$$el.children.namedItem("cvn-table-fixed-right") &&
$$el.children
.namedItem("cvn-table-fixed-right")
.children.namedItem("cvn-table-body");
let elMainBody = $$el.children
.namedItem("cvn-table-view")
.children.namedItem("cvn-table-main")
.children.namedItem("cvn-table-body");
if (elFixedRightBody) {
let elFixedRightBodyScroll = elFixedRightBody.scrollTop;
if (elMainBody) elMainBody.scrollTop = elFixedRightBodyScroll;
if (elFixedLeftBody) elFixedLeftBody.scrollTop = elFixedRightBodyScroll;
} else if (elFixedLeftBody) {
let elMainBodyScroll = elMainBody.scrollTop;
elFixedLeftBody.scrollTop = elMainBodyScroll;
}
}