dongtan4046 2016-08-15 18:20
浏览 50
已采纳

PHP排序DOM元素和替换

I currently have a ul list that looks like this:

<ul id="primary-nav">
    <li class="navButton" id="amps_off_button" data-order="6">
        <span class="navButton-color red"><p>AMPS Off</p></span>
        <span class="glare"></span>
    </li>
    <li class="navButton" id="amps_alarms" data-order="7">
        <span class="navButton-color"><p>AMPS Alarms</p></span>
        <span class="glare"></span>
    </li>
    <li class="navButton" id="sysConfig" data-order="3">
        <span class="navButton-color"><p>System Configuration</p></span>
        <span class="glare"></span></li>
    <li class="navButton" id="system" data-order="4">
        <span class="navButton-color"><p>System</p></span>
        <span class="glare"></span>
    </li>
    <li class="navButton" id="backBtn" data-order="100">
        <span class="navButton-color"><p>BACK</p></span>
        <span class="glare"></span>
    </li>
    <li class="navButton" id="rescan" data-order="101">
        <span class="navButton-color"><p>ReScan</p></span>
        <span class="glare"></span>
    </li>
</ul>

In my Navigation class, I have a function that can be called that will sort the order of the unordered list by the value in data-order:

public function setOrder() {
    $liNodeList = $this->xpath->query("//ul[@id='primary-nav']//li");
    $liNodes = iterator_to_array($liNodeList);

    usort($liNodes, array('Navigation', 'sortListByOrderAttr'));

    if ($liNodeList->length == count($liNodes)) {
        for ($i = 0; $i < $liNodeList->length; $i++) {
            $node = $liNodeList->item($i);
            $newNode = $liNodes[$i];
            $node->parentNode->replaceChild($newNode, $node);
        }
    }
    else echo "error";
}

private static function sortListByOrderAttr($a, $b) {
    return (int) $a->getAttribute('data-order') - (int) $b->getAttribute('data-order');
}

For some reason, I am not getting the correct order of the list. Instead, the output shows 4 items instead of the 6 items it's supposed to show:

<ul id="primary-nav">
    <li class="navButton" id="amps_off_button" data-order="6">
        <span class="navButton-color red"><p>AMPS Off</p></span>
        <span class="glare"></span>
    </li>
    <li class="navButton" id="amps_alarms" data-order="7">
        <span class="navButton-color"><p>AMPS Alarms</p></span>
        <span class="glare"></span>
    </li>
    <li class="navButton" id="backBtn" data-order="100">
        <span class="navButton-color"><p>BACK</p></span>
        <span class="glare"></span>
    </li>
    <li class="navButton" id="rescan" data-order="101">
        <span class="navButton-color"><p>ReScan</p></span>
        <span class="glare"></span>
    </li>
</ul>

I suspect my problem here is that my $node->parentNode->replaceChild($newNode, $node); is no es bueno. Maybe I need a different approach to sort this? Help appreciated!

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-08-15 19:48
    关注

    Well, after thinking about what cHao was saying, it appears that after re-reading how replaceChild works, it explains why my method didn't yield the correct result, due to the new child nodes already existed in the node set.

    I've fixed the code as such:

    public function setOrder() {
        $liNodeList = $this->xpath->query("//ul[@id='primary-nav']//li");
        $liNodes = iterator_to_array($liNodeList);
    
        usort($liNodes, array('Navigation', 'sortListByOrderAttr'));
    
        if ($liNodeList->length == count($liNodes)) {
            for ($i = 0; $i < $liNodeList->length; $i++) {
                $node = $liNodeList->item($i);
                $newNode = $this->doc->importNode($liNodes[$i], TRUE);
                $node->parentNode->appendChild($newNode);
            }
        }
        else echo "error with nav menu";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用