jQuery:如何处理 sortable('serialize') 返回的列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/654535/
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: What to do with the list that sortable('serialize') returns?
提问by bart
With jQuery I'm retrieving positions of a sortable list using 'serialize', like this:
使用 jQuery,我正在使用“序列化”检索可排序列表的位置,如下所示:
var order = $('ul').sortable('serialize');
var order = $('ul').sortable('serialize');
The variable 'order' then receives the following:
然后变量“order”接收以下信息:
id[]=2&id[]=3&id[]=1&id[]=4&id[]=5
id[]=2&id[]=3&id[]=1&id[]=4&id[]=5
Now how can I use this data in an ajax call?
现在如何在 ajax 调用中使用这些数据?
This is how I plan to do it, but it's ugly and I can't change the parameter name 'id':
这是我打算这样做的方式,但它很丑陋,我无法更改参数名称“id”:
$.post('ajax.php?'+order,{action:'updateOrder'});
$.post('ajax.php?'+order,{action:'updateOrder'});
Maybe I need to unserialize, then implode the variable 'order' and assign it to just one parameter?
也许我需要反序列化,然后内爆变量“order”并将其分配给一个参数?
I don't have a problem with the server side code, but I have a problem with the jQuery client site code. The question is, where do I place the 'order' variable in the script?
我的服务器端代码没有问题,但 jQuery 客户端站点代码有问题。问题是,我应该将“订单”变量放在脚本中的什么位置?
In the example I gave I added it as a query string:
在我给出的示例中,我将其添加为查询字符串:
'ajax.php?'+order
'ajax.php?'+order
But I would like to pass it as a parameter, just like the action parameter. The following doesn't work, it returns a syntax error:
但是我想将它作为参数传递,就像操作参数一样。以下不起作用,它返回一个语法错误:
$.post('ajax.php?'+order,{action:'updateOrder',order});
$.post('ajax.php?'+order,{action:'updateOrder',order});
回答by bart
Found it! I needed to add the option key:'string'
which changes the variable name to 'string' instead of 'id'.
找到了!我需要添加将key:'string'
变量名称更改为“字符串”而不是“id”的选项。
var order = $('#projects ul').sortable('serialize',{key:'string'});
$.post('ajax.php',order+'&action=updateOrder');
回答by David Waller
I think the best way is not to use sortable('serialize')
at all, but use jQuery to simply iterate over the sorted ids, like so:
我认为最好的方法是根本不使用sortable('serialize')
,而是使用 jQuery 简单地迭代排序的 id,如下所示:
order = [];
$('ul').children('li').each(function(idx, elm) {
order.push(elm.id.split('_')[1])
});
$.get('ajax.php', {action: 'updateOrder', 'order[]': order});
This has an advantage over explicitly appending the serialized sortable to the URL (or a parameter) in that it doesn't include the order[]
parameter at all if there are no li
in the list. (When I had this problem I was using sortable(connectWith: 'ul')
so it was entirely possible for a user to drag all the li
from one list). In contrast appending the serialized sortable would leave an unwanted '&'.
这比显式地将序列化可排序附加到 URL(或参数)有一个优势,因为order[]
如果li
列表中没有参数,它根本不包含参数。(当我遇到这个问题时,我正在使用,sortable(connectWith: 'ul')
因此用户完全有可能li
从一个列表中拖动所有内容)。相比之下,附加序列化的 sortable 会留下不需要的“&”。
回答by Daniel Von Fange
Lets say you were ordering news items, and your page sent this to "?id[]=2&id[]=3&id[]=1&id[]=4&id[]=5" your php code.
假设您正在订购新闻,并且您的页面将其发送到您的 php 代码“?id[]=2&id[]=3&id[]=1&id[]=4&id[]=5”。
$_POST['id'] # would be [2,3,1,4,5]
// Now we need to update the position field of these items
foreach($_POST['id'] as $i=>$id ):
// For our first record, $i is 0, and $id is 2.
$post = Post::get($id); # Do you own database access here
$post->position = $i; # Set the position to its location in the array
$post->save(); # Save the record
endforeach
回答by Lyubomir Marinov
var order = $('#projects ul').sortable('toArray'});
this might work too ;)
这也可能有效;)
回答by wise_phoenix
The best way is know to use var order = $('ul').sortable('serialize');
feature is shown in the following link :
最好的方法是知道使用 varorder = $('ul').sortable('serialize');
功能显示在以下链接中:
All you need to get the ordered list data in your php file is the code :
在你的 php 文件中获取有序列表数据所需的只是代码:
foreach( $_POST['id'] as $order => $order_nb )
{
...
}
回答by Svetoslav Marinov
Regarding the "key" param I used the square brackets in order to get an array.
关于“关键”参数,我使用方括号来获取数组。
var data = $('#cms-form').serialize();
data += '&' + $('.ui-tabs-nav').sortable('serialize', {key:'nav_order[]'});
回答by Ata ul Mustafa
Solution
解决方案
I have created my own solution by manipulating sortable('serialize'). I have simplified it by using jquery functions, and then posted it to php url for updating order of list in the database. Have a look at my code.
我通过操作 sortable('serialize') 创建了自己的解决方案。我通过使用 jquery 函数对其进行了简化,然后将其发布到 php url 以更新数据库中列表的顺序。看看我的代码。
$( "#covercast_sortable" ).sortable({
update : function () {
var order = $('#covercast_sortable').sortable('serialize',{key:'string'});
// order var gives something like string=3&string=2&string=1
var ar = order.split('&');
var i = 0;
var str = '';
for(i;i<ar.length;i++){
if(str != "") { str += ','; }
// slice is used to remove 'sting=' from every value of var ar
str += ar[i].slice(7);
}
// here value of str is 3,2,1 with respect to order variable
$.ajax({
type: "POST",
url: 'myURL.php',
data: 'order=' + str,
cache: false,
success: function (data) {
console.log(data);
}
});
}
});
Cheers
干杯
回答by Vikas Dwivedi
if data is
如果数据是
$data = 'album[]=stat&sort[]=157204&sort[]=157205&sort[]=157206&sort[]=157208&sort[]=157207&sort[]=157209&sort[]=157210&sort[]=157211&sort[]=157212&sort[]=157213';
$data = 'album[]=stat&sort[]=157204&sort[]=157205&sort[]=157206&sort[]=157208&sort[]=157207&sort[]=157209&sort[]=157210&sort[]=157211&sort[]=157212&sort[]=157213';
and you want to get sort as array in php
use parse_str()parse_str($data, $output);
print_r($output);
并且您想在 php 中
使用parse_str()将排序为数组parse_str($data, $output);
print_r($output);
Output :Array ( [album] => Array ( [0] => stat )
输出:数组( [专辑] => 数组( [0] => stat )
[sort] => Array
(
[0] => 157204
[1] => 157205
[2] => 157206
[3] => 157208
[4] => 157207
[5] => 157209
[6] => 157210
[7] => 157211
[8] => 157212
[9] => 157213
)
)
)