使用sortorder对列表进行排序和保存

时间:2014-08-08 05:08:10

标签: jquery sql asp.net-mvc

您好我需要对我的列表进行排序并保存它基本上我有一些评论列表我希望为某些人提供更多优先级,而对某些人来说更少,例如我使用这样的东西。

<div class="group">
<div id="item1">
    this is item 1
</div>
<div id="item2">
    this is item 2
</div>
<div id="item3">
    this is item 3
</div>
<div id="item4">
    this is item 4
</div>
</div>

我使用JQueryUI排序功能将我的列表拖动和排序到自定义顺序问题是什么是在后端保存排序顺序的最佳方法。

感谢

2 个答案:

答案 0 :(得分:0)

循环遍历已排序的div,您将获得新序列并保存

  var divlist = $(".group").children()
    var divseq= {}
$.each(divlist ,function(key ,val){

    console.log($(this).attr('id'));
    divseq[key] = $(this).attr('id') 

})
console.log(divseq);
    $.get('your url',divseq) //send request to your page which will process sequece

//它将为div的位置发送类似0,1,2,3的参数

答案 1 :(得分:0)

要完成此操作,您可以使用可嵌套的jquery插件(https://github.com/dbushell/Nestable

下载插件并查看以下代码

的index.php

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="nestable.js"></script> 
<link rel="stylesheet" href="nestable.css" type="text/css"/>
<?php
error_reporting(0);
$connection = mysql_connect("localhost","root",""); 
$db         = mysql_select_db("testdb", $connection) or die("Could not select dataBase");
?>

<div id="nestable3" class="dd" style="width:300px">
<ol class="dd-list">
<?php 
$get = mysql_query("select * from sample order by sort");
while($set = mysql_fetch_object($get))
{
?>
<li class="dd-item dd3-item" data-id="<?php echo $set->id; ?>">
    <div class="dd-handle dd3-handle"></div>
    <div class="dd3-content"><?php echo $set->heading; ?></div>    
</li>
<?php
}   
?>
</ol>
</div>

<script>
$(document).ready(function() 
{
    var updateOutput = function(e)
    {
        var list   = e.length ? e : $(e.target),
        output = list.data('output');
        $.ajax({
        type: "POST",
        url: "sort.php",
        dataType:'json',
        data: {myData:JSON.stringify(list.nestable('serialize'))},
        }).done(function( msg )
        {
            //display some message if you want
        });
    };
    $('#nestable3').nestable().on('change', updateOutput);
});
</script>

sort.php

<?php 
$connection = mysql_connect("localhost","root",""); 
$db         = mysql_select_db("testdb", $connection) or die("Could not select dataBase");

$stringJSON = $_REQUEST['myData'];  
$stringObject = json_decode(stripslashes($stringJSON),true);

recursor($stringObject, $db);
function recursor($complex_array, $db)
{
    foreach ($complex_array as $n => $v)
    {
        $id = $v['id'];
        $indx = $n+1;
        if($id)
        {
        $qry = mysql_query("update sample set sort = {$indx} where id = ".$id);
        }
        if (is_array($v))
            {
            recursor($v, $db);
            }
    }
}
?>

并且如果有帮助则不要忘记标记