如何使用 Webforms 和 C# 显示包含董事、经理和员工的分层组织结构图?

发布于 2024-08-20 00:52:56 字数 2912 浏览 6 评论 0原文

假设我有一份包含员工以及向他们报告的人员的 XML 文档。像这样的事情:

<?xml version="1.0" encoding="utf-8" ?>
<employees>
  <employee>
    <id>1</id>
    <name>Ashley King</name>
    <title>Director</title>
    <directReports>
      <employee>
        <id>2</id>
        <name>Jim Warden</name>
        <title>Manager</title>
        <directReports>
          <employee>
            <id>3</id>
            <name>Jeff Reese</name>
            <title>Lead</title>
            <directReports>
              <employee>
                <id>4</id>
                <name>Leann Thompson</name>
                <title>Staff</title>
                <directReports />
              </employee>
              <employee>
                <id>5</id>
                <name>Amber Herzer</name>
                <title>Staff</title>
                <directReports />
              </employee>
              <employee>
                <id>6</id>
                <name>Ben Lively</name>
                <title>Staff</title>
                <directReports />
              </employee>
            </directReports>
          </employee>
          <employee>
            <id>3</id>
            <name>Mary Hibdon</name>
            <title>Lead</title>
            <directReports>
              <employee>
                <id>50</id>
                <name>Brandon Burns</name>
                <title>Staff</title>
                <directReports />
              </employee>
              <employee>
                <id>55</id>
                <name>David Peck</name>
                <title>Staff</title>
                <directReports />
              </employee>
              <employee>
                <id>62</id>
                <name>Ben Lively</name>
                <title>Staff</title>
                <directReports />
              </employee>
            </directReports>
          </employee>
        </directReports>
      </employee>
    </directReports>
  </employee>
</employees>

我想使用 webforms (c#) 根据我提供的 xml 文档中的数据创建动态组织结构图。我尝试了几种不同程度的蹩脚方法。

在我看来,解决方案必须包含一个 html 表(畏缩)。主要是因为每个员工的职位都很重要,我们不能让他们浮动在浏览器窗口中(如果您不同意,请纠正我)。顶级员工 Askley King 将成为顶部单元格,其 colspan 为 X。Jim Wardon 将成为具有相同 colspan 的下一行,因为他在该 XML 文档中没有任何同时代人。 Jeff Reese 和 Mary Hibdon 将是下一行,其跨度为 x / 2。然后,下一行有下一个员工,我开始陷入如何完成它的困境。

你会怎么做?代码或概念......没有区别。我只需要指明正确的方向。

Assume I have an XML doc with employees and who reports to them. Something like this:

<?xml version="1.0" encoding="utf-8" ?>
<employees>
  <employee>
    <id>1</id>
    <name>Ashley King</name>
    <title>Director</title>
    <directReports>
      <employee>
        <id>2</id>
        <name>Jim Warden</name>
        <title>Manager</title>
        <directReports>
          <employee>
            <id>3</id>
            <name>Jeff Reese</name>
            <title>Lead</title>
            <directReports>
              <employee>
                <id>4</id>
                <name>Leann Thompson</name>
                <title>Staff</title>
                <directReports />
              </employee>
              <employee>
                <id>5</id>
                <name>Amber Herzer</name>
                <title>Staff</title>
                <directReports />
              </employee>
              <employee>
                <id>6</id>
                <name>Ben Lively</name>
                <title>Staff</title>
                <directReports />
              </employee>
            </directReports>
          </employee>
          <employee>
            <id>3</id>
            <name>Mary Hibdon</name>
            <title>Lead</title>
            <directReports>
              <employee>
                <id>50</id>
                <name>Brandon Burns</name>
                <title>Staff</title>
                <directReports />
              </employee>
              <employee>
                <id>55</id>
                <name>David Peck</name>
                <title>Staff</title>
                <directReports />
              </employee>
              <employee>
                <id>62</id>
                <name>Ben Lively</name>
                <title>Staff</title>
                <directReports />
              </employee>
            </directReports>
          </employee>
        </directReports>
      </employee>
    </directReports>
  </employee>
</employees>

I'd like to use webforms (c#) to create dynamic org charts based on the data in the xml doc I feed it. I have tried a few methods with varying levels of crappiness.

It seems to me like the solution will have to include an html table (cringe). Mainly because the position of each employee is significant and we can't have them floating around the browser window (please correct me if you disagree). The top employee, Askley King, would be be the top cell, a with a colspan of X. Jim Wardon would be the next row with the same colspan since he doesn't have any contemporaties in that XML doc. Jeff Reese and Mary Hibdon would be the next row with colspans of x / 2. Then, the next row has the next employees and I start to get bogged down in how to complete it.

How would you do it? Code or concept... makes no difference. I just need to be pointed in the right direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

转身泪倾城 2024-08-27 00:52:56

XML 喜欢递归。将节点一一布置或布置到某个巨大的表格网格中需要做大量工作——您的数据格式是分层的,您的处理方法也应该是分层的。

是的。试过了。我只是遇到麻烦了
让我思考最好的方法
做吧。我可以使用桌子。我可以用
CSS。绝对定位。但什么也没有
我在 HTML 中想出的确实是
为我工作。

XSLT 非常擅长将 xml 转换为 html。以下 xslt 将您的 xml 转换为一系列嵌套的 div 和表。我正在使用表格来使每个同事组保持一致。似乎是最简单的方法。

<xsl:template match="employee">
  <div class="wrapper">
    <div class="smallline" />
    <div class="box">
      <xsl:value-of select="name"/> <br/>
      <xsl:value-of select="title"/>
    </div>
    <xsl:if test="directReports/employee">
      <div class="smallline"> </div>
      <table>
        <tr>
          <xsl:for-each select="directReports/employee">
            <td> <xsl:apply-templates select="."  /> </td>
          </xsl:for-each>
        </tr>
      </table>
    </xsl:if>
  </div>
</xsl:template>

要预览,请从 http://pastebin.com/f6597d519 保存 xslt

添加
到员工 xml 的顶部,然后在浏览器中打开。在实践中,您可能希望在服务器上进行转换以避免兼容性问题。要在 C# 中执行此操作,请参阅如何在 C# 中应用 XSLT 样式表< /a>

XML loves recursion. It's a lot of work to lay out the nodes one-by-one or into some gigantic table grid -- your data format is heirarchical and your processing method should be as well.

Yep. Tried it. I'm just having trouble
getting my mind around the best way to
do it. I could use tables. I could use
css. Absolute positioning. But nothing
I come up with in HTML is really
working for me.

XSLT is pretty good at transforming xml into html. the following xslt transforms your xml into a series of nested divs and tables. I'm using a table to get each colleague group aligned. seemed like the easiest way.

<xsl:template match="employee">
  <div class="wrapper">
    <div class="smallline" />
    <div class="box">
      <xsl:value-of select="name"/> <br/>
      <xsl:value-of select="title"/>
    </div>
    <xsl:if test="directReports/employee">
      <div class="smallline"> </div>
      <table>
        <tr>
          <xsl:for-each select="directReports/employee">
            <td> <xsl:apply-templates select="."  /> </td>
          </xsl:for-each>
        </tr>
      </table>
    </xsl:if>
  </div>
</xsl:template>

To preview, save the xslt from http://pastebin.com/f6597d519

Add <?xml-stylesheet href="foo.xslt" type="text/xsl"?>
to the top of your employees xml, and open in a browser. In practice, you may want to transform on the server to avoid compatibility issues. To do that in C#, see How to apply an XSLT Stylesheet in C#

段念尘 2024-08-27 00:52:56

有一个商业网络控件,有免费版本。
http://unifosys.com/chart4.net/

来自 XML 在线演示的分层图表位于此处:
http://organization.unifosys.com/demo.aspx

There is a commercial web control, has free version.
http://unifosys.com/chart4.net/

Hierarchical chart from XML online demo is here:
http://organization.unifosys.com/demo.aspx

空心↖ 2024-08-27 00:52:56

SmartDraw 这里有免费的组织结构图: http://www.smartdraw.com/specials/ppc/org-chart-software.htm?id=141414&gclid=CPSep4-a158CFYsz3god1VRybg

我自己没有使用过它们。

据我所知,msCharts 似乎不支持组织结构图。

SmartDraw have free org charts here: http://www.smartdraw.com/specials/ppc/org-chart-software.htm?id=141414&gclid=CPSep4-a158CFYsz3god1VRybg

I have not used them myself.

From what I can find it looks like msCharts does not support org charts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文