php JQuery UI 保存可排序列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7342727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery UI Saving Sortable List
提问by Laurence
I know this question has been asked before but the solutions did not work for me. I am trying to save the new ordering of items to the database.
我知道以前有人问过这个问题,但解决方案对我不起作用。我正在尝试将项目的新排序保存到数据库中。
I have simplified it very considerably but this is the basic idea of it. I have a form with a sortable list embedded in it.
我已经大大简化了它,但这是它的基本思想。我有一个表单,其中嵌入了一个可排序的列表。
<form id="itemlist">
<ul id="itemsort">
<li id="Item_1">Item<input type="hidden" name="itemid[]" value="itemsRowID01"/></li>
<li id="Item_2">Item<input type="hidden" name="itemid[]" value="itemsRowID02"/></li>
<li id="Item_3">Item<input type="hidden" name="itemid[]" value="itemsRowID03"/></li>
<li id="Item_4">Item<input type="hidden" name="itemid[]" value="itemsRowID04"/></li>
</ul>
</form>
I have JQuery and JQuery UI Loaded and the The Following code enables the sortable list function and posts the item ids and New sort order to a php script. the "editor" variable is a public variable that is set on load it works fine. The sorting works fine but the neworder value that posts doesn't seem to change when I re-order the list.
我加载了 JQuery 和 JQuery UI,以下代码启用了可排序列表功能,并将项目 ID 和新排序顺序发布到 php 脚本。“编辑器”变量是在加载时设置的公共变量,它工作正常。排序工作正常,但当我重新排序列表时,发布的 neworder 值似乎没有改变。
//sorting feature
$("#itemsort").live('hover', function() {
$("#itemsort").sortable({
opacity:.5,
update : function () {
var neworder = $('#itemsort').sortable('serialize');
var inputs = serializePost('#itemlist');
$.post("core/actions.php",{
'order': editor,
'inputs': inputs,
'neworder': neworder},function(){
alert("Order saved.", 1);
});
}
});
});
On actions.php...
在actions.php...
if(isset($_POST['order'])){
//set a variable for each post
$batchid = $_POST['inputs']['itemid'];
parse_str($_POST['neworder'], $neworder);
//count the number of entries to be ordered
$count = count($batchid);
//use the count to create an incremental loop for each item to be updated.
$i=0;
while ($i <= $count) {
$query ="UPDATE {$_POST['order']} SET order=$neworder[item][$i] WHERE id=$batchid[$i]";
++$i;
}
}
I'm not sure why the order I get for each item will not change.
我不知道为什么我为每个项目得到的订单不会改变。
Any Ideas?
有任何想法吗?
-L
-L
回答by Laurence
$("#list").live('hover', function() {
$("#list").sortable({
update : function () {
var neworder = new Array();
$('#list li').each(function() {
//get the id
var id = $(this).attr("id");
//create an object
var obj = {};
//insert the id into the object
obj[] = id;
//push the object into the array
neworder.push(obj);
});
$.post("pagewhereyouuselist.php",{'neworder': neworder},function(data){});
}
});
});
Then in your PHP file, or in this example "pagewhereyouuselist.php"
然后在你的 PHP 文件中,或者在这个例子中“pagewhereyouuselist.php”
$neworderarray = $_POST['neworder'];
//loop through the list of ids and update your db
foreach($neworderarray as $order=>$id){
//you prob jave a connection already i just added this as an example
$con = mysql_connect("host","username","password");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("UPDATE table SET order = {$order} WHERE id = {$id}");
mysql_close($con);
}
that should do it i didn't test it as it is an example connection. the actual script I am actually using is more specific to my program this is a simplified version to show the concept
应该这样做我没有测试它,因为它是一个示例连接。我实际使用的实际脚本更特定于我的程序,这是一个简化版本以展示概念
回答by Artem Oliynyk
Try this:
尝试这个:
Your HTML fields
您的 HTML 字段
<form id="itemlist" method="POST">
<ul id="itemsort">
<li id="Item_1">Item 1<input type="hidden" name="itemid[]" value="itemsRowID01"/></li>
<li id="Item_2">Item 2<input type="hidden" name="itemid[]" value="itemsRowID02"/></li>
<li id="Item_3">Item 3<input type="hidden" name="itemid[]" value="itemsRowID03"/></li>
<li id="Item_4">Item 4<input type="hidden" name="itemid[]" value="itemsRowID04"/></li>
</ul>
</form>
JS to send order:
JS发送订单:
$("#itemsort").live( 'hover', function() {
$("#itemsort").sortable({
update: function () {
var inputs = $('#itemlist').serialize();
$.post("./jq-ui-test.php", inputs, alert("Order saved.") );
}
});
});
Saving order:
保存顺序:
if( isset( $_POST['itemid'] ) && is_array( $_POST['itemid'] ) ) {
foreach( $_POST['itemid'] as $order => $item ) {
$order = intval( $order );
$item_esc = mysql_real_escape_string( $item );
$sql_query = "UPDATE {$_POST['order']} SET order={$order} WHERE id = '{$item_esc}'";
}
}
Also, if you want that start order start from 1 ( not from 0 ) change $order = intval( $order );
to $order = intval( $order ) + 1;
另外,如果您希望开始顺序从 1 (而不是从 0 )更改$order = intval( $order );
为$order = intval( $order ) + 1;