简体   繁体   English

使用d3的嵌套jQuery手风琴

[英]Nested jQuery Accordions using d3

I'm running into a frustrating problem while trying to nest jQuery accordions using d3. 我在尝试使用d3嵌套jQuery手风琴时遇到了一个令人沮丧的问题。 The problem seems to stem from the seeming inability (or at least difficulty) of trying to create html structures like the following, recursively, and using d3's nest and data binding mechanisms: 该问题似乎源于试图递归地创建如下所示的html结构以及使用d3的嵌套和数据绑定机制的看似无能(或至少有困难):

<div>
    <h1>        // accordion 1 header
    <div>       // accordion 1 content
        <h1>           // nested accordion header
        <div>          // nested accordion content            
        </div>
    </div>
    <h1>        // accordion 2 header
    <div>       // accordion 2 content
    </div>
</div>

The d3 approaches i've unsuccessfully tried thus far are based on the following, non accordion friendly, nesting/binding code: 到目前为止,我尚未成功尝试过的d3方法基于以下非手风琴友好的嵌套/绑定代码:

var nest = d3.nest()
             .key(//someFun)
             .key(//someFun)
             ...  // undetermined number of keys
             .entries(someData);

d3.select(someNode)
            .selectAll("div")
                .data(nest)
                .enter()
                .append("div")
                .html(htmlFun[0])
            .selectAll("div")
                .data(function(d){return d.values})
                .enter()
                .append("div")
                .html(htmlFun[1])
            .selectAll("div")
                .data(function(d){return d.values})
                .enter()
                .append("div")
                .html(htmlFun[2])
                         ... // repeat for each key

where 'hmtlFun' is a preComputed array of functions to generate content for a given nest key level. 其中“ hmtlFun”是函数的预计算数组,用于为给定的嵌套键级别生成内容。 This code works fine for generating html structures like 此代码可以很好地生成html结构,例如

<div>
    <div> 
        <div>                      
        </div>
    </div>
</div>

but jQuery Accordion's require basic html phrases with mixed/interleaved elements under each div, eg 但是jQuery手风琴要求每个div下具有混合/交错元素的基本html短语,例如

<div>
    <h1>
    <div/> 

    <h1>
    <div/>

    ...

</div>

I know the following won't work, but I'd love to be able to do something like: 我知道以下内容将行不通,但我希望能够执行以下操作:

d3.select(someNode)
            .selectAll("div")
                .data(nest)
                .enter()
                .append("div")
                .each(function(d) {
                    d3.select(this).append("h1")
                      .text(htmlFun[0]);
                    d3.select(this).append("div")
                })
            .selectAll("div")
                .data(function(d){return d.values})
                .enter()
                .append("div")
                .each(function(d) {
                    d3.select(this).append("h1")
                      .text(htmlFun[1]);
                    d3.select(this).append("div")
                })
            .selectAll("div")
                .data(function(d){return d.values})
                .enter()
                .append("div")
                .each(function(d) {
                    d3.select(this).append("h1")
                      .text(htmlFun[2]);
                    d3.select(this).append("div")
                }) 
                         ... // repeat for each key
            .selectAll("div")
                .data(function(d){return d.values})
                .enter()
                .append("div")
                // at bottom of key recursion append leaf content

Any help/suggestions (related to a solution) are greatly appreciated in advance :^) 任何帮助/建议(与解决方案有关)在此先感谢:^)

For anyone interested, here's a working solution to the problem based on a simple depth-first traversal, nestDFT (not included), which calls nestNodeFilter on each nest node encountered during the traversal. 任何有兴趣,这里是一个有效的解决方案基于一个简单的深度优先遍历,nestDFT(不含税),这在遍历期间遇到的每个巢节点上调用nestNodeFilter问题。 One thing to note, d.unid refers to 'unique node id's which were generated for each nest node prior to running nestDFT. 有一点需要注意,d.unid是指被用于在运行之前nestDFT每窝节点产生的独特节点ID。

function nestNodeFilter(rootDiv, d){
        var node        = d,
            container   = d3.select(rootDiv);
        if(h==0 && d.values == undefined){ // nest root
        } else if(d.values != undefined && Array.isArray(d.values)) {
            node        = d.values;
            container   = d3.select('#div_'+d.unid);
        } else if(!Array.isArray(d.values)){
            // generate leaf content at bottom of nest
            return
        }
        node.forEach(function(d){
            this.append("h1")
                .attr('id','h1_'+d.unid)
                .text(accordFuns[h](d));
            this.append("div")
                .attr('id','div_'+d.unid);
        }, container);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM