这是我到目前为止所拥有的。 我所拥有的代码包含在php中,因此其中有一些php。
$(document).ready(function() {
$(\"ul.droptrue\").sortable({
connectWith: '.droptrue',
remove:function(event, ui){
var order = $(ui.item).serialize;
alert( order );
},
receive : function () {
var order = $(this).sortable('serialize');
var list = 'list[]='+this.id+'&list[]=".$module[1]."&';
$.ajax({
type: 'POST',
url: '".__BASE_URL."modules/admin/moduleUpdate.php',
data: list+ order,
success: function(msg){alert(msg);},
error: function(msg){alert( \"Data Error: \"
+ msg );}
});
}
}); });
所以我要做的是获取ui.item的序列化版本。使用接收部分,我得到“listItem [] = 3& listItem [] = 4 ...”的输出,但是使用ui.item我得到“function(){return o.param(this.serializeArray()); }”。
如何获得ui.item的序列化版本?
答案 0 :(得分:1)
您写道:
var order = $(ui.item).serialize;
你可能想写:
var order = $(ui.item).serialize();
答案 1 :(得分:1)
我弄清楚我做错了什么。所以这个问题不再需要帮助。
我做了什么的简短摘要。
我使用了sortable的更新功能,然后我使用php来确定更改内容以及如何更改它。
这是jquery代码:
$('ul.droptrue').sortable({
placeholder: 'ui-state-highlight',
connectWith: '.droptrue',
update : function (event, ui){
//vars needed
var sender = null;
var item = ui.item.context.id;
var target = event.target.id;
var order = $(this).sortable('serialize');
if(ui.sender != null){
sender = ui.sender.context.id;
}
$.ajax({
type: 'POST',
data: 'list[]='+this.id+'&list[]='+item+'&list[]=".$dirt[1]."&list[]='+sender+'&'+order,
url: 'modules/admin/moduleUpdate.php'
});
}
});
这是我使用的php:
$widgetParts = explode('_', $_POST['list'][1]);
$widget = $widgetParts[1];
switch ($_POST['list'][0]) {
case 'header_layout':
if($_POST['list'][3] == 'null'){
if(isset ($_POST['listItem'])){
if(in_array($widget, $_POST['listItem'])){
foreach ($_POST['listItem'] as $key => $value) {
$sql = "UPDATE widget_layouts SET weight=".$key." WHERE module_id=".$_POST['list'][2]." AND position=1 AND widget_id=".$value;
mysql_query($sql);
}
}
}else{
$sql = "DELETE FROM widget_layouts WHERE module_id=".$_POST['list'][2]." AND widget_id=".$widget." AND position=1";
mysql_query($sql);
}
}else{
foreach ($_POST['listItem'] as $key => $value) {
$sql = 'SELECT * FROM widget_layouts WHERE module_id='.$_POST['list'][2].' AND widget_id='.$value.' AND position=1';
if(mysql_num_rows(mysql_query($sql)) == 0){
$sql = "INSERT INTO widget_layouts VALUES (".$_POST['list'][2].",".$widget.",1,".$key.")";
mysql_query($sql);
}
}
}
break;
case 'content_layout':
if($_POST['list'][3] == 'null'){
if(isset ($_POST['listItem'])){
if(in_array($widget, $_POST['listItem'])){
foreach ($_POST['listItem'] as $key => $value) {
$sql = "UPDATE widget_layouts SET weight=".$key." WHERE module_id=".$_POST['list'][2]." AND position=2 AND widget_id=".$value;
mysql_query($sql);
}
}
}else{
$sql = "DELETE FROM widget_layouts WHERE module_id=".$_POST['list'][2]." AND widget_id=".$widget." AND position=2";
mysql_query($sql);
}
}else{
foreach ($_POST['listItem'] as $key => $value) {
$sql = 'SELECT * FROM widget_layouts WHERE module_id='.$_POST['list'][2].' AND widget_id='.$value.' AND position=2';
if(mysql_num_rows(mysql_query($sql)) == 0){
$sql = "INSERT INTO widget_layouts VALUES (".$_POST['list'][2].",".$widget.",2,".$key.")";
mysql_query($sql);
}
}
}
break;
case 'sidebar_layout':
if($_POST['list'][3] == 'null'){
if(isset ($_POST['listItem'])){
if(in_array($widget, $_POST['listItem'])){
foreach ($_POST['listItem'] as $key => $value) {
$sql = "UPDATE widget_layouts SET weight=".$key." WHERE module_id=".$_POST['list'][2]." AND position=3 AND widget_id=".$value;
mysql_query($sql);
}
}
}else{
$sql = "DELETE FROM widget_layouts WHERE module_id=".$_POST['list'][2]." AND widget_id=".$widget." AND position=3";
mysql_query($sql);
}
}else{
foreach ($_POST['listItem'] as $key => $value) {
$sql = 'SELECT * FROM widget_layouts WHERE module_id='.$_POST['list'][2].' AND widget_id='.$value.' AND position=3';
if(mysql_num_rows(mysql_query($sql)) == 0){
$sql = "INSERT INTO widget_layouts VALUES (".$_POST['list'][2].",".$widget.",3,".$key.")";
mysql_query($sql);
}
}
}
break;
case 'footer_layout':
if($_POST['list'][3] == 'null'){
if(isset ($_POST['listItem'])){
if(in_array($widget, $_POST['listItem'])){
foreach ($_POST['listItem'] as $key => $value) {
$sql = "UPDATE widget_layouts SET weight=".$key." WHERE module_id=".$_POST['list'][2]." AND position=4 AND widget_id=".$value;
mysql_query($sql);
}
}
}else{
$sql = "DELETE FROM widget_layouts WHERE module_id=".$_POST['list'][2]." AND widget_id=".$widget." AND position=4";
mysql_query($sql);
}
}else{
foreach ($_POST['listItem'] as $key => $value) {
$sql = 'SELECT * FROM widget_layouts WHERE module_id='.$_POST['list'][2].' AND widget_id='.$value.' AND position=4';
if(mysql_num_rows(mysql_query($sql)) == 0){
$sql = "INSERT INTO widget_layouts VALUES (".$_POST['list'][2].",".$widget.",4,".$key.")";
mysql_query($sql);
}
}
}
break;
case 'widget_layout':
foreach ($_POST['listItem'] as $key => $value) {
$sql = "DELETE FROM widget_layouts WHERE module_id=".$widget." AND widget_id=".$value;
$query = mysql_query($sql) or die(mysql_error());
}
break;
}
这些代码允许我创建一个拖放列表来动态布局我的网站,没有任何麻烦。我希望这些代码可以帮助那些遇到与我相同类型的问题的人。