自定义节点,默认 maxLines 1,可以根据手动展开后的文本高度,自动调整 row 行高度吗 #3270
Unanswered
simplejason
asked this question in
Q&A
Replies: 1 comment
|
@simplejason 您好!以下是关于您问题的自动回复: 您好,感谢您的详细描述!针对 “自定义节点,默认 maxLines 1,可以根据手动展开后的文本高度,自动调整 row 行高度吗” 的需求,下面为您梳理解决思路及实现建议。 1️⃣ 实现自定义 DataCell 展开/收起图标
参考代码: import { DataCell } from '@antv/s2';
class ExpandableDataCell extends DataCell {
drawTextShape() {
// 判断当前是否展开,根据 props 或 meta 标识
const isExpanded = this.spreadsheet.store.get('expand-row-' + this.meta.rowIndex);
const maxLines = isExpanded ? Infinity : 1;
this.textShape = this.addShape('text', {
attrs: {
text: this.getText(),
...this.getTextStyle(),
// 控制最大显示行数
maxLines,
wordWrap: true,
textOverflow: 'ellipsis',
},
});
// 添加展开/收起 icon,监听点击
this.addShape('image', {
attrs: {
x: ...,
y: ...,
img: isExpanded ? collapseIcon : expandIcon,
cursor: 'pointer',
},
name: 'expand-icon',
}).on('click', () => {
this.spreadsheet.store.set('expand-row-' + this.meta.rowIndex, !isExpanded);
this.spreadsheet.render(false); // 重新渲染
});
}
}
// 在 s2Options 里指定自定义 cell
const s2Options = {
dataCell: (meta, s2) => new ExpandableDataCell(meta, s2),
// 注册 icon
customSVGIcons: [
{ name: 'expand', src: '...' },
{ name: 'collapse', src: '...' },
],
};2️⃣ 行高自适应文本高度方案
参考代码: const s2Options = {
style: {
rowCell: {
heightByField: {
// 例如:根据某行是否展开设置高度
[rowIndex]: isExpanded ? computedHeight : defaultHeight,
},
// 或者用 height 回调
height: (rowNode) => {
const isExpanded = ... // 判断标记
return isExpanded ? computedHeight : 30;
}
}
}
};
3️⃣ 切换 maxLines 参数,控制文本显示
参考代码: const cellTextWordWrapStyle = {
maxLines: isExpanded ? Infinity : 1,
wordWrap: true,
textOverflow: 'ellipsis',
};
const s2Options = {
style: {
dataCell: cellTextWordWrapStyle
}
};🔗 总结步骤
⚡️ 注意事项
如需完整代码样例或遇到具体实现障碍,欢迎补充问题或提供更多细节! 此回复由 AI 助手自动生成。如有任何问题,我们的团队会尽快跟进。 |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
目的:期望字段级列节点,实现展开收起能力,收起状态默认 maxLine 1 的省略号,展开设置 maxLines 999 全量换行展示
操作步骤:
目前 extends DataCell 有方法可以自动更新行高等自适应列高吗?
All reactions