[英]How to get updated sortable list in Jquery Sortable widgets
我试图通过添加jquery排序方法来提供动态ui。 最终结果是使用PHP将新排序的列表存储在txt文件中。 不幸的是,在新创建的文件“ sort.txt ”中,未写入新列表!
错误似乎是可排序div的更新功能中的js。
var sorted = $( ".column" ).sortable( "serialize", { key: "sort" } );
如何获得排序的ID? 码:
的HTML
<div class="column">
<div class="portlet" id="feeds">
<div class="portlet-header" name="feeds">Feeds</div>
<div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div>
</div>
<div class="portlet" id="news">
<div class="portlet-header">News</div>
<div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div>
</div>
<div class="portlet" id="shopping">
<div class="portlet-header" name="shopping">Shopping</div>
<div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div>
</div>
<div class="portlet" id="links">
<div class="portlet-header">Links</div>
<div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</div>
</div>
<div class="portlet" id="images">
<div class="portlet-header">Images</div>
<div class="portlet-content">Lorem ipsum dolor sit amet, consectetuer adipiscing elitasdasdasdasdasdasd</div>
</div>
</div>
JS:
$(function() {
$( ".column" ).sortable({
connectWith: ".column",
handle: ".portlet-header",
cancel: ".portlet-toggle",
placeholder: "portlet-placeholder ui-corner-all",
update: function(event, ui) {
var sorted = $( ".column" ).sortable( "serialize", { key: "sort" } );
$.post( "sort1.php",{ 'choices[]': sorted});
}
});
$( ".portlet" )
.addClass( "ui-widget ui-widget-content ui-helper-clearfix ui-corner-all" )
.find( ".portlet-header" )
.addClass( "ui-widget-header ui-corner-all" )
.prepend( "<span class='ui-icon ui-icon-minusthick portlet-toggle'></span>");
$( ".portlet-toggle" ).click(function() {
var icon = $( this );
icon.toggleClass( "ui-icon-minusthick ui-icon-plusthick" );
icon.closest( ".portlet" ).find( ".portlet-content" ).toggle();
});
});
的PHP
<?php
$data = $_POST["choices"];
$newarray = explode("&", $data[0]);
$filename = 'sort.txt';
$handle = fopen($filename, "w");
$i = count($newarray);
foreach ($newarray as $key => $value) {
fwrite($handle, $value."\n");
}
fclose($handle);
?>
PHP和JS出现了错误。
JS错误: var sorted = $( ".column" ).sortable( "serialize", { key: "sort" } );
PHP错误$newarray = explode("&", $data[0]);
解:
JS:
$( ".column" ).sortable({
axis: 'y',
connectWith: ".column",
handle: ".portlet-header",
cancel: ".portlet-toggle",
placeholder: "portlet-placeholder ui-corner-all",
update: function(event, ui) {
var sorted = $(".column" ).sortable( "toArray" );
$.post( "../sort1.php",{ 'choices[]': sorted});
}
});
PHP:
<?php
$data = $_POST["choices"];
$filename = 'sort.txt';
$handle = fopen($filename, "w");
foreach ($data as $value) {
// Execute statement:
// UPDATE [Table] SET [Position] = $i WHERE [EntityId] = $value
fwrite($handle, $value."\n");
}
fclose($handle);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.