Solved

Javascript jQuery: I want to sort (via onclick) to sort an ul li.

Posted on 2013-01-07
3
360 Views
Last Modified: 2013-09-12
I want to sort my <ul id="vlist"> li (via onclick)
For example if Unit is selected from the ddmenu ID order the ul vlist ID li list based on that span class.

<div class="widgetbox">
                            <div class="title"><h3> Fleet Vehicles</h3></div>
                            <div class="widgetoptions" style="height:33px; font-weight:bold">
                                <div style=" float:left; display:inline-block;">
                                <ul id="ddmenu" class="photosharing_menu">
                                    <li class="marginleft5 dropdown" id="actions">
                                        <a class="dropdown_label" style=" padding-right:30px" href="#actions">
                                        Sort By
                                        <span class="arrow"></span>
                                        </a>
                                        <ul style="background-color:Aqua; z-index:200">
                                            <li style="background-color:Aqua; z-index:200"><a href="">Name</a></li>
                                            <li><a href="">Unit</a></li>
                                            <li><a href="">Type</a></li>
                                            <li><a href="">Status</a></li>
                                            <li><a href="">Location</a></li>
                                            <li><a href="">Alerts</a></li>
                                        </ul>
                                    </li>
                                </ul>
                                </div>
                                <div style=" float:right; display:inline-block; width:50%; text-align:right"><a href="">Add a Vehicle</a></div>
                            </div>
                            <div class="widgetcontent userlistwidget userlistwidget_o nopadding">
                            <div id="scroll1" class="mousescroll">
                                <ul id="vlist">
                                    <li>
                                        <div class="avatar"><img src="images/thumbs/car1.jpg" alt="" /></div>
                                        <div class="info" style="padding: 2px 0px;">
                                            <span class="name"><a href="">Supply Truck #1 (Ford F150)</a></span><br />
                                            <span class="location">Atlantic City Golden Nugget</span><br />
                                            <div style="float:left;">
                                                <span class="status tags color-gray" >Out Of Service</span> <span class="unit">Unit # T1202310004</span>
                                            </div> 
                                            <div style="float:right; display:inline-block; visibility:block"><a class="alert color-red" href="#">
                                                <span class="alerts">12</span></a>
                                            </div>
                                            <br />
                                        </div><!--info-->
                                    </li>
                                    <li>
                                        <div class="avatar"><img src="images/thumbs/car2.jpg" alt="" /></div>
                                        <div class="info" style="padding: 2px 0px;">
                                            <span class="name"><a href="">The Boss</a></span><br />
                                            <span class="location">Atlantic City Golden Nugget</span><br />
                                            <div style="float:left;">
                                                <span class="status tags color-blue">In Service</span>  <span class="unit">Unit # C1202310005</span>
                                            </div>
                                            <div style="float:right; display:inline-block; visibility:hidden"><a class="alert color-red" href="#">
                                                <span class="alerts"></span></a>
                                            </div>
                                            <br />
                                        </div>
                                    </li>
                                    <li>
                                        <div class="avatar"><img src="images/thumbs/car3.jpg" alt="" /></div>
                                        <div class="info" style="padding: 2px 0px;">
                                            <span class="name"><a href="">Captain Gutt</a></span><br />
                                            <span class="location">Atlantic City Golden Nugget</span><br />
                                            <div style="float:left;">
                                                <span class="status tags color-orange">Checked Out</span> <span class="unit">Unit # C1202310006</span>
                                            </div>
                                            <div style="float:right; display:inline-block; visibility:hidden"><a class="alert color-red" href="#">
                                                <span class="alerts"></span></a>
                                            </div>
                                            <br />
                                        </div>
                                    </li>
                                    <li>
                                        <div class="avatar"><img src="images/thumbs/car4.jpg" alt="" /></div>
                                        <div class="info" style="padding: 2px 0px;">
                                            <span class="name"><a href="">Flynn</a></span><br />
                                            <span class="location">Atlantic City Golden Nugget</span><br />
                                            <div style="float:left;">
                                                <span class="status tags color-black">Sold</span> <span class="unit">Unit # B1202310001</span>
                                            </div>
                                            <div style="float:right; display:inline-block; visibility:hidden"><a class="alert color-red" href="#">
                                                <span class="alerts"></span></a>
                                            </div>
                                            <br />
                                        </div>
                                    </li>
                                    <li>
                                        <div class="avatar"><img src="images/thumbs/car5.jpg" alt="" /></div>
                                        <div class="info" style="padding: 2px 0px;">
                                            <span class="name"><a href="">Passenger Pickup #1</a></span><br />
                                            <span class="location">Atlantic City Golden Nugget</span><br />
                                            <div style="float:left;">
                                                <span class="status tags color-blue">In Service</span> <span class="unit">Unit # T1202310005</span>
                                            </div>
                                            <div style="float:right; display:inline-block; visibility:hidden"><a class="alert color-red" href="#">
                                                <span class="alerts"></span></a>
                                            </div>
                                            <br />
                                        </div>
                                    </li>
                                    <li>
                                        <div class="avatar"><img src="images/thumbs/car5.jpg" alt="" /></div>
                                        <div class="info" style="padding: 2px 0px;">
                                            <span class="name"><a href="">Passenger Pickup #2</a></span><br />
                                            <span class="location">Atlantic City Golden Nugget</span><br />
                                            <div style="float:left;">
                                                <span class="tags color-blue">In Service</span> <span class="unit">Unit # T1202310006</span>
                                            </div>
                                            <div style="float:right; display:inline-block; visibility:block"><a class="alert color-red" href="#">
                                                <span class="alerts">3</span></a>
                                            </div>
                                            <br />
                                        </div>
                                    </li>
                                    <li>
                                        <div class="avatar"><img src="images/thumbs/car5.jpg" alt="" /></div>
                                        <div class="info" style="padding: 2px 0px;">
                                            <span class="name"><a href="">Passenger Pickup #3</a></span><br />
                                            <span class="location">Atlantic City Golden Nugget</span><br />
                                            <div style="float:left;">
                                                <span class="tags color-blue">In Service</span> <span class="unit">Unit # T1202310007</span>
                                            </div>
                                            <div style="float:right; display:inline-block; visibility:hidden"><a class="alert color-red" href="#">
                                                <span class="alerts"></span></a>
                                            </div>
                                            <br />
                                        </div>
                                    </li>
                                </ul>
                               </div>
                            </div><!--widgetcontent-->
                        </div><!--widgetbox-->

Open in new window

0
Comment
Question by:quest_capital
  • 2
3 Comments
 
LVL 9

Expert Comment

by:sognoct
ID: 38752523
don't need jquery

<script>
function sortUnorderedList(ul, sortDescending) {
  if(typeof ul == "string")
    ul = document.getElementById(ul);

  // Idiot-proof, remove if you want
  if(!ul) {
    alert("The UL object is null!");
    return;
  }

  // Get the list items and setup an array for sorting
  var lis = ul.getElementsByTagName("LI");
  var vals = [];

  // Populate the array
  for(var i = 0, l = lis.length; i < l; i++)
    vals.push(lis[i].innerHTML);

  // Sort it
  vals.sort();

  // Sometimes you gotta DESC
  if(sortDescending)
    vals.reverse();

  // Change the list on the page
  for(var i = 0, l = lis.length; i < l; i++)
    lis[i].innerHTML = vals[i];
}

Open in new window


example :

<ul id="listid">
  <li>two</li>
  <li>one</li>
  <li>bee</li>
  <li>test</li>
  <li>zeta</li>
  <li>five</li>
</ul>
<input type="Button" Text="sort" onClick="javascript:sortUnorderedList('listid')">

Open in new window

0
 

Author Comment

by:quest_capital
ID: 38753059
sognoct

Thx for your responce.
However I have seen the example before.

Can you modify that exp to where you click on a link to sort based on a <span id> thats within the <li> tag.
0
 
LVL 9

Accepted Solution

by:
sognoct earned 500 total points
ID: 38753881
the script works but the sorting criteria is the inner html itself (in this case the sorting order users the thumbs name, because thumbs are the first part of string inside <li> tag).
To choose a different criteria you shoud change code on  vals.sort() including a control function.

vals.sort(sortfunction)

function sortfunction(a, b){
//Compare innert html of "a" and "b" and then return -1, 0, or 1
}

Open in new window


The easiest way is to call thumbs with the same name of the <a href=""> title </a> so you don't have to create a sortfunction but just to assign images the same name of the article


(in this example you sort descending the  vlist accoring to thumb name)

<script>
function sortUnorderedList(ul, sortDescending) {
  if(typeof ul == "string")
    ul = document.getElementById(ul);

  // Idiot-proof, remove if you want
  if(!ul) {
    alert("The UL object is null!");
    return;
  }

  // Get the list items and setup an array for sorting
  var lis = ul.getElementsByTagName("LI");
  var vals = [];

  // Populate the array
  for(var i = 0, l = lis.length; i < l; i++)
    vals.push(lis[i].innerHTML);

  // Sort it
  vals.sort();

  // Sometimes you gotta DESC
  if(sortDescending)
    vals.reverse();

  // Change the list on the page
  for(var i = 0, l = lis.length; i < l; i++)
    lis[i].innerHTML = vals[i];
}
</script>
<div class="widgetbox">
                            <div class="title"><h3> Fleet Vehicles</h3></div>
                            <div class="widgetoptions" style="height:33px; font-weight:bold">
                                <div style=" float:left; display:inline-block;">
                                <ul id="ddmenu" class="photosharing_menu">
                                    <li class="marginleft5 dropdown" id="actions">
                                        <a class="dropdown_label" style=" padding-right:30px" href="javascript:sortUnorderedList('vlist', 1)">
                                        Sort By
                                        <span class="arrow"></span>
                                        </a>
                                        <ul style="background-color:Aqua; z-index:200">
                                            <li style="background-color:Aqua; z-index:200"><a href="">Name</a></li>
                                            <li><a href="">Unit</a></li>
                                            <li><a href="">Type</a></li>
                                            <li><a href="">Status</a></li>
                                            <li><a href="">Location</a></li>
                                            <li><a href="">Alerts</a></li>
                                        </ul>
                                    </li>
                                </ul>
                                </div>
                                <div style=" float:right; display:inline-block; width:50%; text-align:right"><a href="">Add a Vehicle</a></div>
                            </div>
                            <div class="widgetcontent userlistwidget userlistwidget_o nopadding">
                            <div id="scroll1" class="mousescroll">
                                <ul id="vlist">
                                    <li>
                                        <div class="avatar"><img src="images/thumbs/car1.jpg" alt="" /></div>
                                        <div class="info" style="padding: 2px 0px;">
                                            <span class="name"><a href="">Supply Truck #1 (Ford F150)</a></span><br />
                                            <span class="location">Atlantic City Golden Nugget</span><br />
                                            <div style="float:left;">
                                                <span class="status tags color-gray" >Out Of Service</span> <span class="unit">Unit # T1202310004</span>
                                            </div> 
                                            <div style="float:right; display:inline-block; visibility:block"><a class="alert color-red" href="#">
                                                <span class="alerts">12</span></a>
                                            </div>
                                            <br />
                                        </div><!--info-->
                                    </li>
                                    <li>
                                        <div class="avatar"><img src="images/thumbs/car2.jpg" alt="" /></div>
                                        <div class="info" style="padding: 2px 0px;">
                                            <span class="name"><a href="">The Boss</a></span><br />
                                            <span class="location">Atlantic City Golden Nugget</span><br />
                                            <div style="float:left;">
                                                <span class="status tags color-blue">In Service</span>  <span class="unit">Unit # C1202310005</span>
                                            </div>
                                            <div style="float:right; display:inline-block; visibility:hidden"><a class="alert color-red" href="#">
                                                <span class="alerts"></span></a>
                                            </div>
                                            <br />
                                        </div>
                                    </li>
                                    <li>
                                        <div class="avatar"><img src="images/thumbs/car3.jpg" alt="" /></div>
                                        <div class="info" style="padding: 2px 0px;">
                                            <span class="name"><a href="">Captain Gutt</a></span><br />
                                            <span class="location">Atlantic City Golden Nugget</span><br />
                                            <div style="float:left;">
                                                <span class="status tags color-orange">Checked Out</span> <span class="unit">Unit # C1202310006</span>
                                            </div>
                                            <div style="float:right; display:inline-block; visibility:hidden"><a class="alert color-red" href="#">
                                                <span class="alerts"></span></a>
                                            </div>
                                            <br />
                                        </div>
                                    </li>
                                    <li>
                                        <div class="avatar"><img src="images/thumbs/car4.jpg" alt="" /></div>
                                        <div class="info" style="padding: 2px 0px;">
                                            <span class="name"><a href="">Flynn</a></span><br />
                                            <span class="location">Atlantic City Golden Nugget</span><br />
                                            <div style="float:left;">
                                                <span class="status tags color-black">Sold</span> <span class="unit">Unit # B1202310001</span>
                                            </div>
                                            <div style="float:right; display:inline-block; visibility:hidden"><a class="alert color-red" href="#">
                                                <span class="alerts"></span></a>
                                            </div>
                                            <br />
                                        </div>
                                    </li>
                                    <li>
                                        <div class="avatar"><img src="images/thumbs/car5.jpg" alt="" /></div>
                                        <div class="info" style="padding: 2px 0px;">
                                            <span class="name"><a href="">Passenger Pickup #1</a></span><br />
                                            <span class="location">Atlantic City Golden Nugget</span><br />
                                            <div style="float:left;">
                                                <span class="status tags color-blue">In Service</span> <span class="unit">Unit # T1202310005</span>
                                            </div>
                                            <div style="float:right; display:inline-block; visibility:hidden"><a class="alert color-red" href="#">
                                                <span class="alerts"></span></a>
                                            </div>
                                            <br />
                                        </div>
                                    </li>
                                    <li>
                                        <div class="avatar"><img src="images/thumbs/car5.jpg" alt="" /></div>
                                        <div class="info" style="padding: 2px 0px;">
                                            <span class="name"><a href="">Passenger Pickup #2</a></span><br />
                                            <span class="location">Atlantic City Golden Nugget</span><br />
                                            <div style="float:left;">
                                                <span class="tags color-blue">In Service</span> <span class="unit">Unit # T1202310006</span>
                                            </div>
                                            <div style="float:right; display:inline-block; visibility:block"><a class="alert color-red" href="#">
                                                <span class="alerts">3</span></a>
                                            </div>
                                            <br />
                                        </div>
                                    </li>
                                    <li>
                                        <div class="avatar"><img src="images/thumbs/car5.jpg" alt="" /></div>
                                        <div class="info" style="padding: 2px 0px;">
                                            <span class="name"><a href="">Passenger Pickup #3</a></span><br />
                                            <span class="location">Atlantic City Golden Nugget</span><br />
                                            <div style="float:left;">
                                                <span class="tags color-blue">In Service</span> <span class="unit">Unit # T1202310007</span>
                                            </div>
                                            <div style="float:right; display:inline-block; visibility:hidden"><a class="alert color-red" href="#">
                                                <span class="alerts"></span></a>
                                            </div>
                                            <br />
                                        </div>
                                    </li>
                                </ul>
                               </div>
                            </div><!--widgetcontent-->
                        </div><!--widgetbox-->

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now