如何在Jquery Sortable小部件中获取更新的可排序列表

时间:2015-06-27 06:11:15

标签: javascript jquery sorting portlet

我试图通过添加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);    
?>

1 个答案:

答案 0 :(得分:0)

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);    
?>