Sort combination of <li> and <div> using jquery
I am new to JQuery. I want to use sortableUI for my datalist. Each line in the directory contains text with a serial number. I have used sortable ui successfully. But for now I want to move the text and not the serial number during sorting. Here is my example code. The display shows the serial number on the upper left corner of each li. Now if I try to drag the 2nd li instance with the 3rd cast then the 3rd element goes to 2nd position like but the problem is that it goes after the second div element (where it should before the div like first boot). As a result, according to style .. serial numbers 1 and 2 overlap at the position of the 1st element.
Is there a way that I could use the div right after its corresponding whether during sorting?
<ul id="ulSortable" class="ui-sortable" >
<li class="Decide_Rank_Item_Li">test A</li>
<div class="DisplayOrder">1</div>
<li class="Decide_Rank_Item_Li">Test B</li>
<div class="DisplayOrder">2</div>
<li class="Decide_Rank_Item_Li">Test C</li>
<div class="DisplayOrder">3</div>
<li class="Decide_Rank_Item_Li">Test D</li>
<div class="DisplayOrder">4</div>
</ul>
Image Before sorting:
Now if I start moving around a bit, you can see that the div containing the serial number is not showing in the right place yet.
After sorting 2nd item to 3rd
<ul id="ulSortable" class="ui-sortable" >
<li class="Decide_Rank_Item_Li">test A</li>
<div class="DisplayOrder">1</div>
<div class="DisplayOrder">2</div>
<li class="Decide_Rank_Item_Li">Test C</li>
<li class="Decide_Rank_Item_Li">Test B</li>
<div class="DisplayOrder">3</div>
<li class="Decide_Rank_Item_Li">Test D</li>
<div class="DisplayOrder">4</div>
</ul>
Image after sorting:
Can't see the image
Styles for Lee and Div are shown here. Style:
.Decide_Rank_Item_Li
{
position: relative;
display: block;
border-color: #C0C0C0;
border-width: 1px;
border-style: solid;
vertical-align: bottom;
width: 110px;
height: 100px;
float:left;
background-color: #F8F8F8;
}
.DisplayOrder
{
position: relative;
display: block;
float: left;
top: 5px;
left: -105px;
z-index: 100;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
font-weight: bold;
padding: 0px;
margin: 0px;
width: 0px;
clear: right;
color: #808080;
}
a source to share
Just thought of an alternative solution that is a little more elegant: http://jsbin.com/udina
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js"></script>
<style type="text/css" media="screen">
body { background-color: #000; font: 16px Helvetica, Arial; color: #fff; }
li {
width: 100px;
height: 100px;
float: left;
background: #F8F8F8;
border: 1px solid #C0C0C0;
list-style-position: inside;
color: #C0C0C0;
}
div {
text-align: center;
color: #C0C0C0;
font-size: 120%;
}
.placeholder {
list-style-type:decimal;
}
</style>
</head>
<body>
<ol>
<li><div>Test Content A</div></li>
<li><div>Test Content B</div></li>
<li><div>Test Content C</div></li>
<li><div>Test Content D</div></li>
<li><div>Test Content E</div></li>
<li><div>Test Content F</div></li>
<li><div>Test Content G</div></li>
<li><div>Test Content H</div></li>
<li><div>Test Content I</div></li>
<li><div>Test Content J</div></li>
<li><div>Test Content K</div></li>
<li><div>Test Content L</div></li>
<li><div>Test Content M</div></li>
<li><div>Test Content N</div></li>
<li><div>Test Content O</div></li>
<li><div>Test Content P</div></li>
<li><div>Test Content Q</div></li>
<li><div>Test Content R</div></li>
<li><div>Test Content S</div></li>
<li><div>Test Content T</div></li>
<li><div>Test Content U</div></li>
<li><div>Test Content V</div></li>
<li><div>Test Content W</div></li>
<li><div>Test Content X</div></li>
<li><div>Test Content Y</div></li>
<li><div>Test Content Z</div></li>
</ol>
<script>
$(function(){
$('ol').sortable({
helper: function(evt, el){
return el.clone().css('color', '#F8F8F8');
},
placeholder: 'placeholder'
});
});
</script>
</body>
</html>
a source to share
I had to change the HTML and CSS, but I think it achieves the effect you were.
A demo is hosted here: http://jsbin.com/erigu (edited via http://jsbin.com/erigu/edit )
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js"></script>
<style type="text/css" media="screen">
* { margin: 0; padding: 0; }
#numbers>div, #sortables>div {
width: 110px;
height: 100px;
float: left;
border: 1px solid #C0C0C0;
}
#numbers>div {
padding-bottom: 20px;
font: bold 12px Arial, Helvetica, sans-serif;
color: #808080;
background-color: #F8F8F8;
}
#sortables>div {
padding-top: 20px;
text-align: center;
color: black;
}
#sortables {
position: absolute;
left: 0px;
}
</style>
</head>
<body>
<div id="numbers">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<div id="sortables">
<div>Test A</div>
<div>Test B</div>
<div>Test C</div>
<div>Test D</div>
</div>
<script>
$(function(){
$('#sortables').sortable({
/*
The helper option allows you to customize the dragged element.
in this case we are adding a background.
*/
helper: function(evt, el){
return el.clone().css({background:'#F8F8F8'});
}
});
});
</script>
</body>
</html>
a source to share
The simplest thing could be to place pairs li
/ div
inside another element and sort based on that.
Something like this should work, although there are many options for how you want to do this. The sorted function does not require the use of elements li
.
<ul id="ulSortable" class="ui-sortable" >
<li class="Decide_Rank_Item_Li">
<span>test A</span>
<div class="DisplayOrder">1</div>
</li>
<li class="Decide_Rank_Item_Li">
<span>test B</span>
<div class="DisplayOrder">2</div>
</li>
<li class="Decide_Rank_Item_Li">
<span>test C</span>
<div class="DisplayOrder">3</div>
</li>
<li class="Decide_Rank_Item_Li">
<span>test D</span>
<div class="DisplayOrder">4</div>
</li>
</ul>
a source to share