知道父项id,根据父项与子项联合按父项与子项排序
时间: 2024-03-11 12:51:45 浏览: 25
您可以使用SQL语句来实现这个需求,具体的实现方式如下:
```
SELECT *
FROM your_table
ORDER BY parent_id, child_id
```
其中`your_table`是您的表名,`parent_id`和`child_id`是您的父项和子项的列名,您可以根据您的实际情况进行修改。这条SQL语句会按照父项id进行升序排序,如果有相同的父项id,则按照子项id进行升序排序。
相关问题
js树结构数组根据子项的id查询父项的pid
可以通过遍历树结构数组来查询子项的父项,具体代码实现如下:
```javascript
/**
* 根据子项的id查询父项的pid
* @param {Array} treeData 树结构数组
* @param {String} childId 子项的id
* @return {String} 父项的pid
*/
function findParentPidById(treeData, childId) {
for (let i = 0; i < treeData.length; i++) {
const node = treeData[i]
if (node.id === childId) {
return node.pid
} else if (node.children && node.children.length > 0) {
const parentPid = findParentPidById(node.children, childId)
if (parentPid !== null) {
return parentPid
}
}
}
return null
}
```
调用方式如下:
```javascript
const treeData = [
{
id: '1',
pid: '0',
label: '一级菜单1',
children: [
{
id: '11',
pid: '1',
label: '二级菜单1-1',
},
{
id: '12',
pid: '1',
label: '二级菜单1-2',
},
],
},
{
id: '2',
pid: '0',
label: '一级菜单2',
children: [
{
id: '21',
pid: '2',
label: '二级菜单2-1',
},
{
id: '22',
pid: '2',
label: '二级菜单2-2',
children: [
{
id: '221',
pid: '22',
label: '三级菜单2-2-1',
},
],
},
],
},
]
const parentPid = findParentPidById(treeData, '221')
console.log(parentPid) // 22
```
qt 图形项,父项隐藏但子项要显示
在 Qt 的 QGraphicsView 中,如果想要实现父项隐藏但子项显示的效果,可以通过以下方法实现:
1. 在父项的 paint() 函数中,设置 QPainter 的 opacity(透明度)属性为 0,这样父项就不会被绘制出来了。
```cpp
void ParentItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->save();
painter->setOpacity(0);
// 绘制父项内容
painter->restore();
}
```
2. 在子项的 paint() 函数中,设置 QPainter 的 opacity 属性为 1,这样子项就能够正常绘制。
```cpp
void ChildItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->save();
painter->setOpacity(1);
// 绘制子项内容
painter->restore();
}
```
这样就可以实现父项隐藏但子项显示的效果了。注意,如果子项也是一个父项,它的子项也需要按照同样的方式处理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)