Pie*_*ter 20 html javascript jquery jquery-ui jquery-ui-sortable
以下是JavaScript的有趣用法:使用拖放重新排序项目.我的页面中的实现本身工作正常,但有没有办法确定用户放置项目的顺序?
我问,因为我想加载并保存cookie中的商品订单.
Luc*_*ofi 42
2012年更新
获取元素的索引位置尝试阅读:
用于jquery的COOKIE插件:
JQUERY:
$(function() {
//coockie name
var LI_POSITION = 'li_position';
$('ul#sortable').sortable({
//observe the update event...
update: function(event, ui) {
//create the array that hold the positions...
var order = [];
//loop trought each li...
$('#sortable li').each( function(e) {
//add each li position to the array...
// the +1 is for make it start from 1 instead of 0
order.push( $(this).attr('id') + '=' + ( $(this).index() + 1 ) );
});
// join the array as single variable...
var positions = order.join(';')
//use the variable as you need!
alert( positions );
// $.cookie( LI_POSITION , positions , { expires: 10 });
}
});
});?
Run Code Online (Sandbox Code Playgroud)
HTML:
<ul id="sortable">
<li id="id_1"> Item 1 </li>
<li id="id_2"> Item 2 </li>
<li id="id_3"> Item 3 </li>
<li id="id_4"> Item 4 </li>
<li id="id_5"> Item 5 </li>
</ul>
Run Code Online (Sandbox Code Playgroud)
PHP:
这只是一个例子,但你明白了:你可能想要使用数据库而使用AJAX来获取lis:
<?php
//check if cookie is set..
if ( isset( $_COOKIE['li_position'] ) ) {
//explode the cockie by ";"...
$lis = explode( ';' , $_COOKIE['li_position'] );
// loop for each "id_#=#" ...
foreach ( $lis as $key => $val ) {
//explode each value found by "="...
$pos = explode( '=' , $val );
//format the result into li...
$li .= '<li id="'.$pos[0].'" >'.$pos[1].'</li>';
}
//display it
echo $li;
// use this for delete the cookie!
// setcookie( 'li_position' , null );
} else {
// no cookie available display default set of lis
echo '
<li id="id_1"> Fuji </li>
<li id="id_2"> Golden </li>
<li id="id_3"> Gala </li>
<li id="id_4"> William </li>
<li id="id_5"> Jordan </li>
';
}
?>
Run Code Online (Sandbox Code Playgroud)
Gle*_*lar 37
使用toArray
将可排序项的id序列化为字符串数组的方法.
$( "#sortable" ).sortable('toArray');
Run Code Online (Sandbox Code Playgroud)
HTML
<ul id="sortable">
<li id="id1"> Item 1 </li>
<li id="id2"> Item 2 </li>
<li id="id3"> Item 3 </li>
<li id="id4"> Item 4 </li>
<li id="id5"> Item 5 </li>
</ul>
Run Code Online (Sandbox Code Playgroud)
JQUERY
$("#sortable").sortable(
{
update: function () {
var strItems = "";
$("#sortable").children().each(function (i) {
var li = $(this);
strItems += li.attr("id") + ':' + i + ',';
});
updateSortOrderJS(strItems); <--- do something with this data
}
});
Run Code Online (Sandbox Code Playgroud)
strItems看起来像(new-item-order:item-id)
0,49:1365:2,50:3,364:4366:5,39:6
然后你可以将它解析成更新函数
列出eachTask = new List(itemsList.Trim().Split(new char [] {','}));
然后
String [] item = i.Split(new Char [] {':'});
归档时间: |
|
查看次数: |
41421 次 |
最近记录: |