You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为sap.ui.table.TreeTable设置动态生成的RowAction模板

动态生成TreeTable的RowAction(JS版)

嘿,这就帮你实现用JavaScript动态构建sap.ui.table.TreeTable的RowAction,效果和你给出的XML视图完全一致!

核心思路

我们需要对应XML里的每个元素,用JS API逐个创建:

  • 先构建行操作的RowActionItem和RowAction模板
  • 再创建对应的数据列
  • 最后组装TreeTable并配置好rowActionCount和rowActionTemplate

完整代码示例

// 确保在控制器的define中引入了所需模块:
// ["sap/ui/table/TreeTable", "sap/ui/table/Column", "sap/ui/table/RowAction", "sap/ui/table/RowActionItem", "sap/m/Label"]

// 1. 创建行操作项(对应XML里的t:RowActionItem)
const oRowActionItem = new sap.ui.table.RowActionItem({
  icon: "sap-icon://edit", // 可替换为你需要的图标,比如sap-icon://delete
  press: (oEvent) => {
    // 点击行操作项后的逻辑
    const oRowContext = oEvent.getSource().getBindingContext();
    console.log("触发行操作,当前行数据:", oRowContext.getObject());
  }
});

// 2. 创建RowAction模板(对应XML里的t:rowActionTemplate)
const oRowActionTemplate = new sap.ui.table.RowAction({
  items: [oRowActionItem] // 这里可以放多个操作项,对应rowActionCount的数量
});

// 3. 创建表格列(对应XML里的t:Column)
const oColumnTemplate = new sap.m.Label({
  text: "{Description}" // 绑定数据模型里的Description字段
});

const oColumn = new sap.ui.table.Column({
  label: new sap.m.Label({
    text: this.getView().getModel("i18n").getProperty("ServicesTree_Name") // 获取i18n文本
  }),
  template: oColumnTemplate
});

// 4. 创建TreeTable实例(对应XML里的t:TreeTable)
const oTreeTable = new sap.ui.table.TreeTable({
  id: "tree",
  rowActionCount: 1, // 和XML里的rowActionCount保持一致
  columns: [oColumn],
  rowActionTemplate: oRowActionTemplate,
  // 配置树结构的绑定(根据你的实际数据模型调整)
  rows: {
    path: "/yourTreeDataSource",
    parameters: {
      treeAnnotationProperties: {
        hierarchyLevelFor: "hierarchyLevel", // 你的层级字段名
        parentNodeFor: "parentId", // 父节点ID字段名
        nodeId: "id" // 当前节点ID字段名
      }
    }
  }
});

// 5. 将表格添加到视图的容器中(比如一个VBox或者Page的content)
this.getView().byId("yourContainerId").addItem(oTreeTable);

关键细节说明

  • rowActionCount设置为1,确保表格行只显示1个操作项,和XML配置完全匹配
  • 行操作项的press事件可以根据业务需求自定义逻辑
  • i18n文本的获取要确保视图已经正确加载了i18n模型
  • 树结构的绑定参数需要根据你的实际数据结构调整字段名

内容的提问来源于stack exchange,提问作者Pille

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:05:10