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

如何在Kendo TreeList单个字段中使用多个按钮?求解决方案

Absolutely! You can absolutely add multiple buttons within a single custom command column in Kendo TreeList—just like you do in Kendo Grid. The trick is to ditch the default command shorthand and use the template property for your action column instead. Let me walk you through a working example that mirrors the multi-button setup you’re used to in Grid.

Here’s how to pull it off:

Instead of relying on TreeList’s built-in command configuration, we’ll create a dedicated "Actions" column with a custom template that renders as many buttons as you need. We’ll then wire up click events for each button to handle your desired operations.

<div id="treelist"></div>
// Initialize the TreeList
$("#treelist").kendoTreeList({
  dataSource: {
    data: [
      { id: 1, parentId: null, name: "Project Root", budget: 100000 },
      { id: 2, parentId: 1, name: "Frontend Development", budget: 40000 },
      { id: 3, parentId: 1, name: "Backend Development", budget: 50000 },
      { id: 4, parentId: 2, name: "UI Implementation", budget: 25000 }
    ],
    schema: {
      model: {
        id: "id",
        parentId: "parentId",
        fields: {
          parentId: { nullable: true },
          id: { type: "number" }
        }
      }
    }
  },
  columns: [
    { field: "name", title: "Item Name", expandable: true },
    { field: "budget", title: "Budget", format: "{0:c}" },
    {
      title: "Actions",
      // Custom template for multiple buttons
      template: `
        <button class="k-button k-primary edit-btn" style="margin-right: 8px;">Edit</button>
        <button class="k-button k-danger delete-btn">Delete</button>
      `,
      width: 220
    }
  ],
  editable: "inline" // Optional, if you want to use TreeList's built-in editing
});

// Handle Edit button clicks (event delegation for dynamic rows)
$(document).on("click", ".edit-btn", function() {
  const treelist = $("#treelist").data("kendoTreeList");
  const row = $(this).closest("tr");
  const dataItem = treelist.dataItem(row);

  // Trigger TreeList's built-in row editing, or add your custom logic here
  treelist.editRow(row);
  console.log("Editing item:", dataItem.name);
});

// Handle Delete button clicks
$(document).on("click", ".delete-btn", function() {
  const treelist = $("#treelist").data("kendoTreeList");
  const dataItem = treelist.dataItem($(this).closest("tr"));

  if (confirm(`Are you sure you want to delete "${dataItem.name}"?`)) {
    treelist.remove(dataItem);
    console.log("Deleted item:", dataItem.name);
  }
});

Key Details to Note:

  • Template Flexibility: The template property lets you render any HTML, so you can add as many buttons (or even other UI elements) as you need. Use Kendo’s built-in k-button classes to match the TreeList’s styling.
  • Event Delegation: We use $(document).on() to bind click events instead of direct event binding. This ensures buttons in dynamically loaded rows (like when expanding child nodes) still work correctly.
  • Accessing Data Items: Use treelist.dataItem(row) to grab the full data object for the clicked row—this lets you access all fields tied to that TreeList item for your operations.

This approach works exactly like the multi-button command columns in Kendo Grid because TreeList shares much of Grid’s underlying API and rendering logic. You can extend it further by adding custom modals, status toggles, or any other actions you need.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:31:42