0

0

Kendo UI OrgChart 高级定制:显示多字段与移除头像

花韻仙語

花韻仙語

发布时间:2025-11-25 14:47:02

|

533人浏览过

|

来源于php中文网

原创

Kendo UI OrgChart 高级定制:显示多字段与移除头像

本教程详细介绍了如何利用 kendo ui orgchart 的 `template` 选项,实现对组织结构图节点内容的深度定制。通过自定义 html 模板,您可以轻松展示多个业务字段(如绩效指标),并移除默认的头像显示,从而满足特定的数据可视化需求,提升图表的专业性和信息密度。

Kendo UI OrgChart 简介与默认行为

Kendo UI OrgChart 是一个功能强大的组织结构图组件,用于以层级结构可视化数据。它默认提供了一种简洁的节点显示方式,通常只包含“姓名”和“职位”两个核心信息,并可能附带一个头像。这种默认配置在许多标准场景下已足够使用。然而,在面对更复杂的数据模型时,例如需要在一个节点中展示多个业务指标或自定义属性,并且可能不需要头像时,就需要进行更深层次的定制。

默认的 Kendo UI OrgChart 节点结构通常是固定的,它通过内部机制绑定数据源中的 name 和 position 字段来显示信息。当您的数据源包含 item_desc、eoy_target、ytd_plan 等多个自定义字段,并且希望将它们全部呈现在组织结构图的每个节点上时,就需要利用 Kendo UI 提供的模板机制。

自定义节点模板:template 选项

Kendo UI OrgChart 提供了 template 选项,允许开发者完全控制每个节点(Node)的渲染方式。通过指定一个包含 HTML 结构的字符串作为 template 的值,您可以定义节点内部的任何内容、布局和样式。

在模板字符串中,您可以使用 #: fieldName # 的语法来访问当前节点数据项中的任意字段。例如,如果您的数据项包含 item_desc 字段,您就可以在模板中使用 #: item_desc # 来显示其值。

实现多字段与无头像显示

要实现 Kendo UI OrgChart 节点显示多个自定义字段并移除头像,核心步骤是构建一个包含所有所需字段的 HTML 模板,并将其赋值给 template 选项。

1. 数据准备

首先,确保您的 JSON 数据源包含了所有需要显示的信息。例如,以下是包含多个业务指标的数据结构:

TTSMaker
TTSMaker

TTSMaker是一个免费的文本转语音工具,提供语音生成服务,支持多种语言。

下载
[
  {
    "item_id": "195",
    "item_desc": "Fuel Cost",
    "parent_pi_kode": "193",
    "parent_pid_desc": "Blasting Cost",
    "eoy_target": 0.2,
    "eoy_actual": 0.32,
    "ytd_plan": 0.13,
    "ytd_actual": 0.14,
    "achi_pi": 107.69,
    "achi_ia": 0.0,
    "achi_ra": 0.0,
    "achi_ip": 0.0,
    "has_child": true,
    "is_expanded": true
  },
  {
    "item_id": "194",
    "item_desc": "AN Cost",
    "parent_pi_kode": "193",
    "parent_pid_desc": "Blasting Cost",
    "eoy_target": 0.2,
    "eoy_actual": 0.32,
    "ytd_plan": 0.1,
    "ytd_actual": 0.12,
    "achi_pi": 120.0,
    "achi_ia": 0.0,
    "achi_ra": 0.0,
    "achi_ip": 0.0,
    "has_child": true,
    "is_expanded": true
  },
  {
    "item_id": "196",
    "item_desc": "Oil Cost",
    "parent_pi_kode": "193",
    "parent_pid_desc": "Blasting Cost",
    "eoy_target": 0.2,
    "eoy_actual": 0.32,
    "ytd_plan": 0.08,
    "ytd_actual": 0.1,
    "achi_pi": 125.0,
    "achi_ia": 0.0,
    "achi_ra": 0.0,
    "achi_ip": 0.0,
    "has_child": true,
    "is_expanded": true
  }
]

2. 构建自定义 HTML 模板

根据您需要显示的字段,创建一个 HTML 字符串。在这个模板中,我们将移除默认的头像 <img> 标签,并为每个字段创建相应的显示元素,例如 div 或 span。为了提高可读性,可以使用 <label> 或 <strong> 标签来标识字段名称。

<div class='custom-orgchart-node'>
    <div class='node-header'>
        <strong>#: item_desc #</strong>
    </div>
    <div class='node-body'>
        <p>EOY Target: <span>#: kendo.toString(eoy_target, "n2") #</span></p>
        <p>EOY Actual: <span>#: kendo.toString(eoy_actual, "n2") #</span></p>
        <p>YTD Plan: <span>#: kendo.toString(ytd_plan, "n2") #</span></p>
        <p>YTD Actual: <span>#: kendo.toString(ytd_actual, "n2") #</span></p>
        <p>ACHI PI: <span>#: kendo.toString(achi_pi, "n2") #</span></p>
        <p>ACHI IA: <span>#: kendo.toString(achi_ia, "n2") #</span></p>
        <p>ACHI RA: <span>#: kendo.toString(achi_ra, "n2") #</span></p>
        <p>ACHI IP: <span>#: kendo.toString(achi_ip, "n2") #</span></p>
    </div>
</div>

注意:

  • kendo.toString(value, "n2") 用于格式化数字,保留两位小数。
  • 模板中的类名(如 custom-orgchart-node)可以用于后续的 CSS 样式定义。
  • 模板中不再包含 <img> 标签,从而移除了头像显示。

3. Kendo UI OrgChart 初始化配置

在初始化 Kendo UI OrgChart 时,将上述自定义模板字符串赋值给 template 选项。同时,您需要配置 dataSource 和 schema 来正确解析数据。schema.model.id 和 schema.model.parentId 字段是构建层级关系的关键。

$("#orgchart").kendoOrgChart({
    dataSource: new kendo.data.OrgChartDataSource({
        data: orgChartData, // 您的JSON数据
        schema: {
            model: {
                id: "item_id", // 映射数据中的唯一标识字段
                parentId: "parent_pi_kode", // 映射数据中的父节点标识字段
                fields: {
                    item_desc: { type: "string" },
                    eoy_target: { type: "number" },
                    eoy_actual: { type: "number" },
                    ytd_plan: { type: "number" },
                    ytd_actual: { type: "number" },
                    achi_pi: { type: "number" },
                    achi_ia: { type: "number" },
                    achi_ra: { type: "number" },
                    achi_ip: { type: "number" }
                }
            }
        }
    }),
    // 启用展开/折叠功能,如果您的数据有has_child和is_expanded字段
    // 这通常由OrgChartDataSource自动处理,但确保数据源配置正确
    template: "<div class='custom-orgchart-node'>" +
                "<div class='node-header'>" +
                    "<strong>#: item_desc #</strong>" +
                "</div>" +
                "<div class='node-body'>" +
                    "<p>EOY Target: <span>#: kendo.toString(eoy_target, 'n2') #</span></p>" +
                    "<p>EOY Actual: <span>#: kendo.toString(eoy_actual, 'n2') #</span></p>" +
                    "<p>YTD Plan: <span>#: kendo.toString(ytd_plan, 'n2') #</span></p>" +
                    "<p>YTD Actual: <span>#: kendo.toString(ytd_actual, 'n2') #</span></p>" +
                    "<p>ACHI PI: <span>#: kendo.toString(achi_pi, 'n2') #</span></p>" +
                    "<p>ACHI IA: <span>#: kendo.toString(achi_ia, 'n2') #</span></p>" +
                    "<p>ACHI RA: <span>#: kendo.toString(achi_ra, 'n2') #</span></p>" +
                    "<p>ACHI IP: <span>#: kendo.toString(achi_ip, 'n2') #</span></p>" +
                "</div>" +
            "</div>"
});

4. 样式调整 (CSS)

为了让自定义节点看起来更美观,可以添加一些 CSS 样式。

.k-orgchart-node {
    width: 250px; /* 调整节点宽度以容纳更多信息 */
    min-height: 180px; /* 调整节点最小高度 */
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    border-radius: 8px;
    overflow: hidden; /* 确保内容不溢出 */
}

.custom-orgchart-node {
    padding: 10px;
    background-color: #fff;
    color: #333;
    font-family: Arial, sans-serif;
    font-size: 13px;
    line-height: 1.4;
}

.custom-orgchart-node .node-header {
    background-color: #f0f0f0;
    padding: 8px 10px;
    margin: -10px -10px 10px -10px; /* 扩展背景到边缘 */
    border-bottom: 1px solid #ddd;
    font-size: 14px;
    color: #0056b3;
}

.custom-orgchart-node .node-header strong {
    display: block;
    text-align: center;
}

.custom-orgchart-node .node-body p {
    margin: 3px 0;
    display: flex;
    justify-content: space-between;
}

.custom-orgchart-node .node-body span {
    font-weight: bold;
    color: #007bff;
}

完整代码示例

将上述 HTML、JavaScript 和 CSS 片段整合到一个页面中,即可看到自定义的 Kendo UI OrgChart。

<!DOCTYPE html>
<html>
<head>
    <title>Kendo UI OrgChart 自定义节点</title>
    <link href="https://kendo.cdn.telerik.com/2024.1.130/styles/kendo.default-v2.min.css" rel="stylesheet" />
    <script src="https://kendo.cdn.telerik.com/2024.1.130/js/jquery.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2024.1.130/js/kendo.all.min.js"></script>
    <style>
        html, body {
            margin: 0;
            padding: 0;
            height: 100%;
            overflow: hidden;
            font-family: Arial, sans-serif;
        }
        #orgchart {
            height: 100%;
            width: 100%;
            margin: 0 auto;
        }
        .k-orgchart-node {
            width: 280px; /* 调整节点宽度以容纳更多信息 */
            min-height: 200px; /* 调整节点最小高度 */
            box-shadow: 0 2px 8px rgba(0,0,0,0.15);
            border-radius: 8px;
            overflow: hidden;
            background-color: #fff;
            border: 1px solid #e0e0e0;
        }

        .custom-orgchart-node {
            padding: 10px;
            color: #333;
            font-size: 13px;
            line-height: 1.4;
            display: flex;
            flex-direction: column;
            height: 100%; /* 确保内部div填充节点高度 */
        }

        .custom-orgchart-node .node-header {
            background-color: #f8f9fa;
            padding: 8px 10px;
            margin: -10px -10px 10px -10px; /* 扩展背景到边缘 */
            border-bottom: 1px solid #dee2e6;
            font-size: 15px;
            color: #212529;
            text-align: center;
            font-weight: bold;
        }

        .custom-orgchart-node .node-body {
            flex-grow: 1; /* 让内容区域填充剩余空间 */
            display: flex;
            flex-direction: column;
            justify-content: space-around; /* 均匀分布内容 */
        }

        .custom-orgchart-node .node-body p {
            margin: 2px 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .custom-orgchart-node .node-body p strong {
            color: #555;
            font-weight: normal;
            margin-right: 5px;
        }

        .custom-orgchart-node .node-body p span {
            font-weight: bold;
            color: #007bff;
            white-space: nowrap; /* 防止数字换行 */
        }
    </style>
</head>
<body>
    <div id="orgchart"></div>

    <script>
        $(document).ready(function () {
            var orgChartData = [
                {
                    "item_id": "193", // 假设这是一个根节点或父节点
                    "item_desc": "Blasting Cost",
                    "parent_pi_kode": null, // 根节点没有父节点
                    "eoy_target": 0.6,
                    "eoy_actual": 0.96,
                    "ytd_plan": 0.31,
                    "ytd_actual": 0.36,
                    "achi_pi": 116.13,
                    "achi_ia": 0.0,
                    "achi_ra": 0.0,
                    "achi_ip": 0.0,
                    "has_child": true,
                    "is_expanded": true
                },
                {
                    "item_id": "195",
                    "item_desc": "Fuel Cost",
                    "parent_pi_kode": "193",
                    "parent_pid_desc": "Blasting Cost",
                    "eoy_target": 0.2,
                    "eoy_actual": 0.32,
                    "ytd_plan": 0.13,
                    "ytd_actual": 0.14,
                    "achi_pi": 107.69,
                    "achi_ia": 0.0,
                    "achi_ra": 0.0,
                    "achi_ip": 0.0,
                    "has_child": true,
                    "is_expanded": true
                },
                {
                    "item_id": "194",
                    "item_desc": "AN Cost",
                    "parent_pi_kode": "193",
                    "parent_pid_desc": "Blasting Cost",
                    "eoy_target": 0.2,
                    "eoy_actual": 0.32,
                    "ytd_plan": 0.1,
                    "ytd_actual": 0.12,
                    "achi_pi": 120.0,
                    "achi_ia": 0.0,
                    "achi_ra": 0.0,
                    "achi_ip": 0.0,
                    "has_child": true,
                    "is_expanded": true
                },
                {
                    "item_id": "196",
                    "item_desc": "Oil Cost",
                    "parent_pi_kode": "193",
                    "parent_pid_desc": "Blasting Cost",
                    "eoy_target": 0.2,
                    "eoy_actual": 0.32,
                    "ytd_plan": 0.08,
                    "ytd_actual": 0.1,
                    "achi_pi": 125.0,
                    "achi_ia": 0.0,
                    "achi_ra": 0.0,
                    "achi_ip": 0.0,
                    "has_child": true,
                    "is_expanded": true
                }
            ];

            $("#orgchart").kendoOrgChart({
                dataSource: new kendo.data.OrgChartDataSource({
                    data: orgChartData,
                    schema: {
                        model: {
                            id: "item_id",
                            parentId: "parent_pi_kode",
                            fields: {
                                item_desc: { type: "string" },
                                eoy_target: { type: "number" },
                                eoy_actual: { type: "number" },
                                ytd_plan: { type: "number" },
                                ytd_actual: { type: "number" },
                                achi_pi: { type: "number" },
                                achi_ia: { type: "number" },
                                achi_ra: { type: "number" },
                                achi_ip: { type: "number" }
                            }
                        }
                    }
                }),
                template: "<div class='custom-orgchart-node'>" +
                            "<div class='node-header'>" +
                                "#: item_desc #" +
                            "</div>" +
                            "<div class='node-body'>" +
                                "<p><strong>EOY Target:</strong> <span>#: kendo.toString(eoy_target, 'n2') #</span></p>" +
                                "<p><strong>EOY Actual:</strong> <span>#: kendo.toString(eoy_actual, 'n2') #</span></p>" +
                                "<p><strong>YTD Plan:</strong> <span>#: kendo.toString(ytd_plan, 'n2') #</span></p>" +
                                "<p><strong>YTD Actual:</strong> <span>#: kendo.toString(ytd_actual, 'n2') #</span></p>" +
                                "<p><strong>ACHI PI:</strong> <span>#: kendo.toString(achi_pi, 'n2') #</span></p>" +
                                "<p><strong>ACHI IA:</strong> <span>#: kendo.toString(achi_ia, 'n2') #</span></p>" +
                                "<p><strong>ACHI RA:</strong> <span>#: kendo.toString(achi_ra, 'n2') #</span></p>" +
                                "<p><strong>ACHI IP:</strong> <span>#: kendo.toString(achi_ip, 'n2') #</span></p>" +
                            "</div>" +
                        "</div>"
            });
        });
    </script>
</body>
</html>

注意事项

  1. 数据字段映射: 确保 schema.model.id 和 schema.model.parentId 正确映射到您数据源中的唯一标识和父节点标识字段。这是构建正确层级关系的基础。
  2. 模板内容与样式: 模板中的 HTML 结构和 CSS 样式直接决定了节点的视觉效果。合理设计布局和样式,以确保所有信息清晰可读,并适应不同

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
json数据格式
json数据格式

JSON是一种轻量级的数据交换格式。本专题为大家带来json数据格式相关文章,帮助大家解决问题。

457

2023.08.07

json是什么
json是什么

JSON是一种轻量级的数据交换格式,具有简洁、易读、跨平台和语言的特点,JSON数据是通过键值对的方式进行组织,其中键是字符串,值可以是字符串、数值、布尔值、数组、对象或者null,在Web开发、数据交换和配置文件等方面得到广泛应用。本专题为大家提供json相关的文章、下载、课程内容,供大家免费下载体验。

547

2023.08.23

jquery怎么操作json
jquery怎么操作json

操作的方法有:1、“$.parseJSON(jsonString)”2、“$.getJSON(url, data, success)”;3、“$.each(obj, callback)”;4、“$.ajax()”。更多jquery怎么操作json的详细内容,可以访问本专题下面的文章。

335

2023.10.13

go语言处理json数据方法
go语言处理json数据方法

本专题整合了go语言中处理json数据方法,阅读专题下面的文章了解更多详细内容。

82

2025.09.10

js 字符串转数组
js 字符串转数组

js字符串转数组的方法:1、使用“split()”方法;2、使用“Array.from()”方法;3、使用for循环遍历;4、使用“Array.split()”方法。本专题为大家提供js字符串转数组的相关的文章、下载、课程内容,供大家免费下载体验。

760

2023.08.03

js截取字符串的方法
js截取字符串的方法

js截取字符串的方法有substring()方法、substr()方法、slice()方法、split()方法和slice()方法。本专题为大家提供字符串相关的文章、下载、课程内容,供大家免费下载体验。

221

2023.09.04

java基础知识汇总
java基础知识汇总

java基础知识有Java的历史和特点、Java的开发环境、Java的基本数据类型、变量和常量、运算符和表达式、控制语句、数组和字符串等等知识点。想要知道更多关于java基础知识的朋友,请阅读本专题下面的的有关文章,欢迎大家来php中文网学习。

1567

2023.10.24

字符串介绍
字符串介绍

字符串是一种数据类型,它可以是任何文本,包括字母、数字、符号等。字符串可以由不同的字符组成,例如空格、标点符号、数字等。在编程中,字符串通常用引号括起来,如单引号、双引号或反引号。想了解更多字符串的相关内容,可以阅读本专题下面的文章。

649

2023.11.24

Python异步编程与Asyncio高并发应用实践
Python异步编程与Asyncio高并发应用实践

本专题围绕 Python 异步编程模型展开,深入讲解 Asyncio 框架的核心原理与应用实践。内容包括事件循环机制、协程任务调度、异步 IO 处理以及并发任务管理策略。通过构建高并发网络请求与异步数据处理案例,帮助开发者掌握 Python 在高并发场景中的高效开发方法,并提升系统资源利用率与整体运行性能。

37

2026.03.12

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Sass 教程
Sass 教程

共14课时 | 0.9万人学习

Bootstrap 5教程
Bootstrap 5教程

共46课时 | 3.6万人学习

CSS教程
CSS教程

共754课时 | 42.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号