javascript - JQuery Each Function Give Style Dependant On List Level - Resolved just needs explaining -


list example;

  • inbox
    • sub-folder 1
    • sub-folder 2
    • sub-folder 3
      • sub-folder 1
        • sub-folder 1
        • sub-folder 2
      • sub-folder 2
    • sub-folder 4
    • sub-folder 5
  • important
  • sent
  • draft
  • trash
  • promotions
  • news

list example source

<ul class="inbox-nav" id="inbox-nav">     <li class="active">         <a href="javascript:;" data-type="inbox" data-title="inbox">             <div class="arrow"></div> inbox             <span class="badge badge-success">3</span>         </a>         <ul>         <li><a href="javascript:;" data-type="inbox" data-title="sub_1">sub-folder 1</a></li>         <li><a href="javascript:;" data-type="inbox" data-title="sub_2">sub-folder 2</a></li>         <li>             <a href="javascript:;" data-type="inbox" data-title="sub_3">sub-folder 3</a>             <ul>                 <li><a href="javascript:;" data-type="inbox" data-title="sub_1">sub-folder 1</a></li>                 <ul>                     <li><a href="javascript:;" data-type="inbox" data-title="sub_1">sub-folder 1</a></li>                     <li><a href="javascript:;" data-type="inbox" data-title="sub_1">sub-folder 2</a></li>                 </ul>                 <li><a href="javascript:;" data-type="inbox" data-title="sub_2">sub-folder 2</a></li>             </ul>         </li>         <li><a href="javascript:;" data-type="inbox" data-title="sub_4">sub-folder 4</a></li>         <li><a href="javascript:;" data-type="inbox" data-title="sub_5">sub-folder 5</a></li>         </ul>     </li>     <li>         <a href="javascript:;" data-type="important" data-title="inbox"> important </a>     </li>     <li>         <a href="javascript:;" data-type="sent" data-title="sent"> sent </a>     </li>     <li>         <a href="javascript:;" data-type="draft" data-title="draft"> draft             <span class="badge badge-danger">8</span>         </a>     </li>     <li>         <a href="javascript:;" class="sbold uppercase" data-title="trash"> trash             <span class="badge badge-info">23</span>         </a>     </li>     <li>         <a href="javascript:;" data-type="inbox" data-title="promotions"> promotions             <span class="badge badge-warning">2</span>         </a>     </li>     <li>         <a href="javascript:;" data-type="inbox" data-title="news"> news </a>     </li> </ul> 

question

on list, user can create many sub-folders wish. need loop through list , create <select><option... drop down list showing list.

var listitems = $(".inbox-nav").find("li"); $('.foo').prepend($(listitems).html()); 

i know can duplicate list each li .text() need translate <option>text value</option> , furthermore each tier need add class 'tier' + tier_value example code turn out like;

<select>   <option>sub-folder 1</option>   <option>sub-folder 2</option>   <option>sub-folder 3</option>   <option class="tier1">sub-folder 1</option>   <option class="tier2">sub-folder 1</option>   <option class="tier2">sub-folder 2</option>   <option class="tier1">sub-folder 2</option>   <option>sub-folder 4</option>   <option>sub-folder 5</option>   ... </select> 

my best attempt far:

var sel = $('<select />');  $('.alpha').prepend('<select />');  $('.inbox-nav li').each( function( index ) {    if(!$(this).children("ul").length) {      $('.alpha select').append('<option>' + $(this).text() + '</option>');    }  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>    <ul class="inbox-nav" id="inbox-nav" style="display:none;">      <li class="active">          <a href="javascript:;" data-type="inbox" data-title="inbox">              <div class="arrow"></div> inbox              <span class="badge badge-success">3</span>          </a>          <ul>          <li><a href="javascript:;" data-type="inbox" data-title="sub_1">sub-folder 1</a></li>          <li><a href="javascript:;" data-type="inbox" data-title="sub_2">sub-folder 2</a></li>          <li>              <a href="javascript:;" data-type="inbox" data-title="sub_3">sub-folder 3</a>              <ul>                  <li><a href="javascript:;" data-type="inbox" data-title="sub_1">sub-folder 1</a></li>                  <ul>                      <li><a href="javascript:;" data-type="inbox" data-title="sub_1">sub-folder 1</a></li>                      <li><a href="javascript:;" data-type="inbox" data-title="sub_1">sub-folder 2</a></li>                  </ul>                  <li><a href="javascript:;" data-type="inbox" data-title="sub_2">sub-folder 2</a></li>              </ul>          </li>          <li><a href="javascript:;" data-type="inbox" data-title="sub_4">sub-folder 4</a></li>          <li><a href="javascript:;" data-type="inbox" data-title="sub_5">sub-folder 5</a></li>          </ul>      </li>      <li>          <a href="javascript:;" data-type="important" data-title="inbox"> important </a>      </li>      <li>          <a href="javascript:;" data-type="sent" data-title="sent"> sent </a>      </li>      <li>          <a href="javascript:;" data-type="draft" data-title="draft"> draft              <span class="badge badge-danger">8</span>          </a>      </li>      <li>          <a href="javascript:;" class="sbold uppercase" data-title="trash"> trash              <span class="badge badge-info">23</span>          </a>      </li>      <li>          <a href="javascript:;" data-type="inbox" data-title="promotions"> promotions              <span class="badge badge-warning">2</span>          </a>      </li>      <li>          <a href="javascript:;" data-type="inbox" data-title="news"> news </a>      </li>  </ul>    <div class="alpha"></div>

update i

previously prior update, drop down showing <li></li> .text() values .children() i've figured out if statement resolve this; if(!$(this).children("ul").length) {

update ii

i have managed find right resources answer own question such not understand how works, i'm not posting answer @ moment feel free take @ working jsfiddle , add bit of explaining. (ignore part ignores span sext understand part).

you can use recursive function process each list , sub-lists.

$(function() {  	var options = processlist($('#inbox-nav'), 0); // compile options starting @ #inbox-nav    $('.alpha').html('<select>' + options + '</select>'); // create , add select box        function processlist(list, level) { // recursive function      var options = '';      list.children('li').each(function() { // each child li        options += '<option' + (level ? ' class="tier' + level + '"' : '') + '>' +          $(this).children('a').text() + '</option>'; // add option li        var sublist = $(this).children('ul'); // find embedded list        if (sublist.length) { // if there sub-list          options += processlist(sublist, level + 1); // compile options starting @ list, , on next level        }      });      return options; // return compile options    }  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>   <ul class="inbox-nav" id="inbox-nav" style="display:none;">          <li class="active">              <a href="javascript:;" data-type="inbox" data-title="inbox">                  <div class="arrow"></div> inbox                  <span class="badge badge-success">3</span>              </a>              <ul>              <li><a href="javascript:;" data-type="inbox" data-title="sub_1">sub-folder 1</a></li>              <li><a href="javascript:;" data-type="inbox" data-title="sub_2">sub-folder 2</a></li>              <li>                  <a href="javascript:;" data-type="inbox" data-title="sub_3">sub-folder 3</a>                  <ul>                      <li><a href="javascript:;" data-type="inbox" data-title="sub_1">sub-folder 1</a></li>                      <ul>                          <li><a href="javascript:;" data-type="inbox" data-title="sub_1">sub-folder 1</a></li>                          <li><a href="javascript:;" data-type="inbox" data-title="sub_1">sub-folder 2</a></li>                      </ul>                      <li><a href="javascript:;" data-type="inbox" data-title="sub_2">sub-folder 2</a></li>                  </ul>              </li>              <li><a href="javascript:;" data-type="inbox" data-title="sub_4">sub-folder 4</a></li>              <li><a href="javascript:;" data-type="inbox" data-title="sub_5">sub-folder 5</a></li>              </ul>          </li>          <li>              <a href="javascript:;" data-type="important" data-title="inbox"> important </a>          </li>          <li>              <a href="javascript:;" data-type="sent" data-title="sent"> sent </a>          </li>          <li>              <a href="javascript:;" data-type="draft" data-title="draft"> draft                  <span class="badge badge-danger">8</span>              </a>          </li>          <li>              <a href="javascript:;" class="sbold uppercase" data-title="trash"> trash                  <span class="badge badge-info">23</span>              </a>          </li>          <li>              <a href="javascript:;" data-type="inbox" data-title="promotions"> promotions                  <span class="badge badge-warning">2</span>              </a>          </li>          <li>              <a href="javascript:;" data-type="inbox" data-title="news"> news </a>          </li>      </ul>            <div class="alpha"></div>


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -