简体   繁体   English

基于分层(父子)数据的数据驱动面包屑

[英]Data-driven breadcrumb based on hierarchical (parent-child) data

I would like to create a breadcrumb for my site that is completely data-driven. 我想为我的网站创建一个完全由数据驱动的面包屑

The data is stored using MariaDB and looks like this: 数据使用MariaDB存储,如下所示:

parent_id | parent_name | child_id | child_name
———————————————————————————————————————————————
    1     |     AAA     |    101   |    aaa
    1     |     Aaa     |    102   |    bbb
    1     |     Aaa     |    103   |    ccc
   101    |     aaa     |   1001   |   aaaa
   101    |     aaa     |   1002   |   bbbb
   102    |     bbb     |   1004   |   cccc
   102    |     bbb     |   1005   |   dddd    
    2     |     Bbb     |    104   |    ddd 

If I select let say record with id='1005', I want my breadcrumb to look like 1 / 102 / 1005 如果我选择让ID为'1005'的记录说,我希望我的面包屑看起来像1 / 102 / 1005
Equivalent HTML is: 等效的HTML是:

<div id="breadcrumb">
    <ul class="breadcrumb">
        <li><a href="#">1</a></li>
        <li><a href="#">102</a></li>
        <li>1005</li>
    </ul>
</div>

Alternatively, selecting 或者,选择

  • '1002' updates the breadcrumb to 1 / 101 / 1002 '1002'更新面包屑1 / 101 / 1002
  • '104' updates the breadcrumb to 2 / 104 “104”更新面包屑2 / 104

I found a solution for my question. 我为我的问题找到了解决方案。

Firstly, I changed the table structure, based on the suggestions mentioned in this article . 首先,根据本文提到的建议,我更改了表结构。 As a result, I changed my data structure to: 结果,我将数据结构更改为:

    id  |     name    |   lft    |   rgt
—————————————————————————————————————————————
     1  |     AAA     |     1    |    16
   101  |     aaa     |     2    |     7
  1001  |     aaaa    |     3    |     4
  1002  |     bbbb    |     5    |     6
   102  |     bbb     |     8    |    13
  1004  |     cccc    |     9    |    10
  1005  |     dddd    |    11    |    12
   103  |     ccc     |    14    |    15
     2  |     BBB     |    17    |    20
   104  |     ddd     |    18    |    19

Then using php, I can easily extract the data from the table in almost the correct format with the following SQL-statement (where '$id' is a variable and depending on the user selection as mentioned earlier): 然后使用php,我可以使用以下SQL语句(其中“ $ id”是变量并且取决于用户选择,如前所述)以几乎正确的格式轻松地从表中提取数据:

SELECT parent.id, parent.name
FROM
  table AS node,
  table AS parent
WHERE
  node.lft BETWEEN parent.lft AND parent.rgt
AND
  node.id = '$id'
ORDER BY
  parent.lft;

Finally, using d3.js, I can use the following code the create the HTML: 最后,使用d3.js,我可以使用以下代码创建HTML:

d3.json('php/breadcrumb.php?id=' + id).get(function(error, d_breadcrumb) {

  var ul = d3.select('#breadcrumb').append('ul')
    .attr('class', 'breadcrumb');

  ul.selectAll('li')
    .data(d_breadcrumb)
    .enter()
    .append('li')
      .append('a')
        .attr('href', function(d) {
          return '?id=' + d.id;
        })
        .text(function(d) {
          return d.name;
        })
});

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

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