每7个项目分为两个菜单列表

时间:2016-04-12 08:23:00

标签: php html twitter-bootstrap

我有一个使用bootstrap的动态菜单,但是一个菜单按钮有大约14个子项。对于一个网站来说太长了,所以我想把它分成两半。

这是我试图复制的代码:

var parentDiv2 = $(document).find('.nuoviServiziReview1');
$.get( "nuoviServiziReview.html", function( data ) {
    var nuovoServizioReview = $('<div/>',{id:'Servizio'+ incremento}).append(parentDiv2);
    nuovoServizioReview.html(data);
})

这就是我的尝试:

<ListView x:Name ="QuoteListView" ItemsSource ="{Binding dict}" HasUnevenRows="True">
    <ListView.ItemTemplate>
      <DataTemplate >
        <ViewCell >
          <ViewCell.View >
            <StackLayout>
              <StackLayout Orientation="Horizontal" Padding="10">
                <Label Text ="{Binding Key}" FontSize="10" FontAttributes="Bold" TextColor="White" />
              </StackLayout>
              <StackLayout Orientation="Horizontal" Padding="10">
                <ListView x:Name ="QuoteTextListView" ItemsSource ="{Binding }" HasUnevenRows="True">
                <Label Text ="{Binding }" FontSize="10" TextColor="White"/>
                </ListView>
              </StackLayout>
            </StackLayout>
          </ViewCell.View>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>

但是这会返回以下代码:

https://jsfiddle.net/sms16v44/

有谁看到我做错了什么?

5 个答案:

答案 0 :(得分:2)

我没有试图改变你的代码,而是想出了以下内容。这允许您从菜单中自动创建子菜单。我希望它有所帮助。

它创建它像下面的屏幕截图:

enter image description here

和其他功能/方式:

enter image description here

<?php

// recreating dummy menu data
function createDummyList() {
    $nmenuccr = array();
    for($i=0;$i<24;$i++) {
        $a = array();
        $a['id']=$i;
        $a['catid']=$i;
        $a['alias']="alias $i";
        $a['title']="title $i";
        array_push($nmenuccr, $a);
    }
    return $nmenuccr;
}


/**
 * Parse a menu into smaller menu's
 * @param type $menu the main menu
 * @param type $class html class
 * @param type $max_sub_items max items before creating a new submenu
 * @param type $submenu (used for recursion)
 * @return string
 */
function createMenuList(&$menu, $class, $max_sub_items) {    

    $out = "<ul class='$class'><li>";

    // shift items
    $submenu = array();
    $i=0;
    $loop=0;
    while(count($menu) > 0) {        

        array_push($submenu, array_shift($menu));

        $i++;
        if ($i == $max_sub_items || count($menu) == 0) {
            $out .= createList($submenu, $loop);
            $out .= '<li>In between</li>';
            $i=0;
            $submenu = array();
            $loop++;            
        }
    }           

    $out .= "</li></ul>";

    return $out;
}

/**
 * 
 * @param type $list
 * @param type $submenu_label
 * @return string
 */
function createList(&$list, $loop) {

    $out = '<ul>';
    foreach($list as $l) {
        $out .= '<li><a href="'.$l['alias'].'.html">'.$l['title'].'</a></li>';    
    }
    $out .= '</ul>';
    return $out;
}


/**
 * Parse a menu into smaller menu's
 * @param type $menu the main menu
 * @param type $class html class
 * @param type $max_sub_items max items before creating a new submenu
 * @param type $submenu (used for recursion)
 * @return string
 */
function createSubMenuList(&$menu, $class, $max_sub_items, $submenu = 0) {

    // create new list
    $out = "<ul class='$class'>";

    // shift items
    for($i=0;$i<$max_sub_items;$i++) {              

       $item = array_shift($menu);

        // add item to list
        if (isset($item)) {
            $out .= '<li><a href="'.$item['alias'].'.html">'.$item['title'].'</a></li>';   
        }
    }

    // check if we're done
    if (count($menu) > 0) {           
        $submenu++;
        $out .= "<li class='submenu_$submenu'>";
        // create submenu in parent menu
        $out .= createSubMenuList($menu, $class, $max_sub_items, $submenu);
        $out .= "</li>";
    }

    $out .= "</ul>";

    return $out;

}

// call menu creation function
$list1=createDummyList();
$list2=createDummyList();
echo createSubMenuList($list1, 'hoofdmenu', 7);
echo "<hr />";
echo createMenuList($list2, 'hoofdmenu', 7);

?>

答案 1 :(得分:1)

如果你也可以将输出数组包含在注释中,那将更容易提供帮助。

然而,试试这个。改变这个:

    if(($tel % 7) != 0){
        $menu .= '</li>';
    }

为:

    if(($tel % 7) != 0){
        $menu .= '</ul></li>';
    }

希望它会起作用

答案 2 :(得分:0)

您对类别ID 11的检查似乎与您的问题无关,因此我没有在那里包含该逻辑。当您将开启和关闭部件留在循环外时,事情变得更加简单。然后,您只需输出重复的项目,并检查放置分隔符的一个案例($tel % 4)。

<?php
$nmenuccr = [ // sample data
    ["alias"=>"life-insurance", "title"=>"Life Insurance"],
    ["alias"=>"life-insurance", "title"=>"Home Insurance"],
    ["alias"=>"life-insurance", "title"=>"Travel Insurance"],
    ["alias"=>"life-insurance", "title"=>"Pet Insurance"],
    ["alias"=>"life-insurance", "title"=>"Boat Insurance"],
    ["alias"=>"life-insurance", "title"=>"Auto Insurance"],
    ["alias"=>"life-insurance", "title"=>"Bike Insurance"],
    ["alias"=>"life-insurance", "title"=>"Business Insurance"],
];

$hmenu = "<li class=\"col-sm-6\">\n\t<ul>\n";
foreach ($nmenuccr as $tel=>$cmenu) {
    if ($tel % 4 == 0 && $tel > 0) {
        $hmenu .= "\t</ul>\n</li>\n<li class=\"col-sm-6\">\n\t<ul>\n"; 
    }
    $hmenu.= "\t\t<li><a href=\"$cmenu[alias].html\">$cmenu[title]</a></li>\n";
}
$hmenu .= "\t</ul>\n</li>";
echo $hmenu;

输出:

<li class="col-sm-6">
    <ul>
        <li><a href="life-insurance.html">Life Insurance</a></li>
        <li><a href="life-insurance.html">Home Insurance</a></li>
        <li><a href="life-insurance.html">Travel Insurance</a></li>
        <li><a href="life-insurance.html">Pet Insurance</a></li>
    </ul>
</li>
<li class="col-sm-6">
    <ul>
        <li><a href="life-insurance.html">Boat Insurance</a></li>
        <li><a href="life-insurance.html">Auto Insurance</a></li>
        <li><a href="life-insurance.html">Bike Insurance</a></li>
        <li><a href="life-insurance.html">Business Insurance</a></li>
    </ul>
</li>

答案 3 :(得分:0)

保持它是一段垃圾代码,您可以通过这种方式快速修复:

$tel = 0;

/* .. */

// If id matches and category id is 11 (services) split in half
if ( $cmenu['id'] && $cmenu['catid'] == '11' ){

    if(++$tel == 1) {
        $hmenu .= '<li class="col-sm-6"><ul>';  
    }


    $hmenu.= '<li><a href="'.$cmenu['alias'].'.html">'.$cmenu['title'].'</a></li>';

    if(($tel % 7) == 0){
        if ($tel == 7) {
            $hmenu .= '</ul></li><li class="col-sm-6"><ul>'; 
        } else {
            $hmenu .= '</ul></li>';
        }
    }
//Else use the normal dropdown layout
} // ..

工作示例(更新): http://sandbox.onlinephpfunctions.com/code/13fd6974cd66c847747f44a4be9b892aa47e4979

但是你应该通过一个函数重构你的$nmenuccr,这将使它成为一个反映菜单目标结构的数组,以KISS生成视图的部分代码。

修改$tel上的更新操作。

答案 4 :(得分:0)

尝试使用任何长度的菜单.....

<ul class="dropdown-menu dropdown-menu-large row" role="menu">
<?php
        $counter = 1;
        foreach ($nmenuccr as $cmenu ) {
            if($counter == 1){
                echo '<li class="col-sm-6"><ul>';
            }//.... end of if() .....//

            echo '<li><a href="'.$cmenu['alias'].'.html">'.$cmenu['title'].'</a></li>';
            $counter ++;
            if($counter == 5){
                echo "</ul></li>";
                $counter = 1;
            }//.... end of if() .....//
        }//.... end of foreach() .....//

?>
</ul>