[英]How to get updated sortable list in Jquery Sortable widgets
I am trying to provide dynamic ui by adding jquery Sorting methods. 我试图通过添加jquery排序方法来提供动态ui。 The end result is the new sorted list stored in a txt file using PHP.
最终结果是使用PHP将新排序的列表存储在txt文件中。 Unfortunately in the newly created file " sort.txt " The new list is not written!
不幸的是,在新创建的文件“ sort.txt ”中,未写入新列表!
The error seems to be js in update function of sortable div. 错误似乎是可排序div的更新功能中的js。
var sorted = $( ".column" ).sortable( "serialize", { key: "sort" } );
How to get the sorted ids? 如何获得排序的ID? Code:
码:
HTML 的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: 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
<?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 and JS was bugged. PHP和JS出现了错误。
JS error: var sorted = $( ".column" ).sortable( "serialize", { key: "sort" } );
JS错误:
var sorted = $( ".column" ).sortable( "serialize", { key: "sort" } );
PHP Error $newarray = explode("&", $data[0]);
PHP错误
$newarray = explode("&", $data[0]);
Solution: 解:
JS: 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:
<?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.