简体   繁体   English

JQuery Select2 - 从 PHP/MySQL 结果格式化嵌套列表

[英]JQuery Select2 - Format nested list from PHP/MySQL results

I have a Select2 list which I want to populate with a nested structure for the data:我有一个Select2列表,我想用数据的嵌套结构填充它:

Level 1
  Condition 1
  Condition 2
Level 2
  Condition 3
  Condition 4
etc

The JSON needs to be in the following format: JSON 需要采用以下格式:

data: [{
        'text': 'Level 1',
        'children': [{
            'id': 1,
            'text': 'Condition 1'
        }, {
            'id': 2,
            'text': 'Condition 2'
        }, ],
        'text': 'Level 2',
        'children': [{
            'id': 3,
            'text': 'Condition 3'
        }, {
            'id': 4,
            'text': 'Condition 4'
        }, ]
    }]

The JQuery Select2: JQuery Select2:

$.ajax( {
                url: "scripts/get_conditions.php",
                dataType: 'json'
            } ).then( function ( response ) {
                $( "#condition_tree" ).select2( {
                    placeholder: "Select a Condition...",
                    allowClear: true,
                    width: 'resolve',
                    containerCssClass: "show-hide",
                    data: response
                } );
            } );

Currently using the following PHP and MySQL which I am unsure of how to change to produce the required results:目前使用以下 PHP 和 MySQL 我不确定如何更改以产生所需的结果:

$query = 'SELECT * FROM mcondition ORDER BY mcondition_name ASC';
$result = $connection->query( $query );

$presentations = array();

while ($row = mysqli_fetch_array($result)) {
$mconditions[] = array("id"=>$row['mcondition_pk'], "text"=>$row['mcondition_name']);
}
echo json_encode($mconditions);
?>

And the mcondition table:和 mcondition 表:

+---------+-----------+------------------------------+
| mcondition_pk | mcondition_name | mcondition_level |
+---------+-----------+------------------------------+
| 1             | Condition 1     | Level 1          |
+---------+-----------+------------------------------+
| 2             | Condition 2     | Level 1          |
+---------+-----------+------------------------------+
| 3             | Condition 3     | Level 2          |
+---------+-----------+------------------------------+
| 4             | Condition 4     | Level 2          |
+---------+-----------+------------------------------+

Note PHP version is 5.3.3, no chance of upgrading at present.注意 PHP 版本为 5.3.3,目前没有升级机会。

Use the level name as key of a two-dimensional array to gather your data under, add each row's data to the children under the appropriate key.使用级别名称作为二维数组的键来收集您的数据,将每一行的数据添加到相应键下的子项中。

// fake mysql row data
$data = [
  ['mcondition_pk' => 1, 'mcondition_name' => 'Condition 1', 'mcondition_level' => 'Level 1'],
  ['mcondition_pk' => 2, 'mcondition_name' => 'Condition 2', 'mcondition_level' => 'Level 1'],
  ['mcondition_pk' => 3, 'mcondition_name' => 'Condition 3', 'mcondition_level' => 'Level 2'],
  ['mcondition_pk' => 4, 'mcondition_name' => 'Condition 4', 'mcondition_level' => 'Level 2'],
];

$temp = [];

// foreach loop over fake data, replace that with your original `while(…)` again
foreach($data as $row) {
  $temp[$row['mcondition_level']]['text'] = $row['mcondition_level'];
  $temp[$row['mcondition_level']]['children'][] = [
    'id' => $row['mcondition_pk'],
    'text' => $row['mcondition_name']
  ];
}

// replace the associative keys with simple numeric ones again
$temp = array_values($temp);

echo json_encode($temp);

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

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