我有一个ul
li
列表,可以选择对元素进行排序和上下移动。我的要求是,一旦用户以所需的方式对元素进行排序,我希望能够将该序列保存在数据库中。
我是否可以获得一个包含li
标识的数组以及li
的位置索引。
例如。在这种情况下,当值= 9时,索引应为0,
value = 11,index = 1等。
渲染的示例html是:
<ul id="ul_li_SubCategories" style="width:200px;" class="chargeCapturetable margin0">
<li sequence="1" title="Category 1" class="liEllipsis" value="9"><a href="#"><span class="viewIcons delFaceName _delete fl"></span>Category 1</a></li>
<li sequence="2" title="Category 3" class="liEllipsis" value="11"><a href="#"><span class="viewIcons delFaceName _delete fl"></span>Category 3</a></li>
<li sequence="4" title="Category 4" class="liEllipsis" value="12"><a href="#"><span class="viewIcons delFaceName _delete fl"></span>Category 4</a></li>
<li sequence="5" title="Category 6" class="liEllipsis" value="22"><a href="#"><span class="viewIcons delFaceName _delete fl"></span>Category 6</a></li>
<li sequence="6" title="Category 5" class="liEllipsis" value="13"><a href="#"><span class="viewIcons delFaceName _delete fl"></span>Category 5</a></li>
<li sequence="7" title="Category 7" class="liEllipsis" value="55"><a href="#"><span class="viewIcons delFaceName _delete fl"></span>Category 7</a></li>
<li sequence="99999" title="Category 8" class="liEllipsis" value="62"><a href="#"><span class="viewIcons delFaceName _delete fl"></span>Category 8</a></li>
</ul>
答案 0 :(得分:3)
获取一个数组,其中包含li
的ID和li
的位置索引。
var arr = $('#ul_li_SubCategories li').map(function (elem) {
return {
value : $(this).attr('value'),
index : $(this).index()
}
}).get();
$(function() {
var arr = $('#ul_li_SubCategories li').map(function(elem) {
return {
value: $(this).attr('value'),
index: $(this).index()
}
}).get();
console.log(arr)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="ul_li_SubCategories" style="width:200px;" class="chargeCapturetable margin0">
<li sequence="1" title="Category 1" class="liEllipsis" value="9"><a href="#"><span class="viewIcons delFaceName _delete fl"></span>Category 1</a>
</li>
<li sequence="2" title="Category 3" class="liEllipsis" value="11"><a href="#"><span class="viewIcons delFaceName _delete fl"></span>Category 3</a>
</li>
<li sequence="4" title="Category 4" class="liEllipsis" value="12"><a href="#"><span class="viewIcons delFaceName _delete fl"></span>Category 4</a>
</li>
<li sequence="5" title="Category 6" class="liEllipsis" value="22"><a href="#"><span class="viewIcons delFaceName _delete fl"></span>Category 6</a>
</li>
<li sequence="6" title="Category 5" class="liEllipsis" value="13"><a href="#"><span class="viewIcons delFaceName _delete fl"></span>Category 5</a>
</li>
<li sequence="7" title="Category 7" class="liEllipsis" value="55"><a href="#"><span class="viewIcons delFaceName _delete fl"></span>Category 7</a>
</li>
<li sequence="99999" title="Category 8" class="liEllipsis" value="62"><a href="#"><span class="viewIcons delFaceName _delete fl"></span>Category 8</a>
</li>
</ul>
答案 1 :(得分:1)
How do I iterate through child elements of a div using jQuery?
这应该指向正确的方向。只需将div
替换为有问题的ul
...
或者,在jsFiddle中编写代码...... http://jsfiddle.net/
答案 2 :(得分:1)
使用此:
[9, 11, 12, 22, 13, 55, 62]
输出:
df = pd.DataFrame({
'A':
['1980-01-01', '1980-02-02', '1980-03-02', '1980-01-21',
'1981-01-21', '1991-02-21', '1991-03-23'] })
df['A'] = pd.to_datetime(df['A']).astype('int64')
max_a = df.A.max()
min_a = df.A.min()
min_norm = -1
max_norm =1
df['NORMA'] = (df.A- min_a) *(max_norm - min_norm) / (max_a-min_a) + min_norm
答案 3 :(得分:0)
首先,您需要获取每个值,然后您需要构建数组并将其序列化,最后您可以使用Ajax调用来保存它。这是JSFiddle complete
$(document).ready(function() {
var items = [];
$('#ul_li_SubCategories > li').each(function () {
items.push({
value : $(this).attr('value'),
index : $(this).index()
});
});
$.ajax({
url: "your url to save the items",
type: "POST",
data: items,
success:function(result){
alert(result);
},
error:function(xhr,status,error){
alert(status);
}
});
});
答案 4 :(得分:0)
参考以下答案
$(function(){
$('.glyphicon-arrow-up').on('click', function(e){
e.preventDefault();
var _this = $(this);
var _parent = _this.closest('ul');
var _child = $(_parent).find('li');
var selected= $(this).closest('li').index();
jQuery($(_parent).children().eq(selected-1)).before(jQuery($(_parent).children().eq(selected)));
var _child = _this.closest('ul').children();
var _childLength = _child.length;
var input = Array();
$.each(_child, function(key, val){
input[key] = $(val).data('id');
})
// Use below if required to update into database
$.ajax({
url: 'ajax.php',
data:{
'inputArray':input,
'queryString':'updatePrecedence',
},
type: "POST",
dataType : 'JSON',
success: function(data){
}
});
});
$('.glyphicon-arrow-down').on('click', function(e){
e.preventDefault();
var _this = $(this);
var _parent = _this.closest('ul');
var _child = $(_parent).find('li');
var selected= $(this).closest('li').index();
jQuery($(_parent).children().eq(selected+1)).after(jQuery($(_parent).children().eq(selected)));
selected=selected+1;
var _child = _this.closest('ul').children();
var _childLength = _child.length;
var input = Array();
$.each(_child, function(key, val){
input[key] = $(val).data('id');
})
// Use below if required to update into database
$.ajax({
url: 'ajax.php',
data:{
'inputArray':input,
'queryString':'updatePrecedence',
},
type: "POST",
dataType : 'JSON',
success: function(data){
}
});
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>Sample 1 <span class="glyphicon-arrow-up">Up</span><span class="glyphicon-arrow-down">Down</span></li>
<li>Sample 2 <span class="glyphicon-arrow-up">Up</span><span class="glyphicon-arrow-down">Down</span></li>
<li>Sample 3 <span class="glyphicon-arrow-up">Up</span><span class="glyphicon-arrow-down">Down</span></li>
<li>Sample 4 <span class="glyphicon-arrow-up">Up</span><span class="glyphicon-arrow-down">Down</span></li>
<li>Sample 5 <span class="glyphicon-arrow-up">Up</span><span class="glyphicon-arrow-down">Down</span></li>
<li>Sample 6 <span class="glyphicon-arrow-up">Up</span><span class="glyphicon-arrow-down">Down</span></li>
<li>Sample 7 <span class="glyphicon-arrow-up">Up</span><span class="glyphicon-arrow-down">Down</span></li>
</ul>
您将在以下行
之后获得索引值var input = Array();
$.each(_child, function(key, val){
input[key] = $(val).data('id');
})
如果您打印console.log(input)
,您将获得数组索引