简体   繁体   English

带有ajax更新的Jqueryui可排序列表

[英]Jqueryui sortable list with ajax update

I am using CodeIgniter with the jQuery UI Sortable widget, to change my menu list position. 我正在使用CodeIgniter和jQuery UI Sortable小部件来更改我的菜单列表位置。

For example, if my menu list is like this: 例如,如果我的菜单列表是这样的:

<li>menu 1</li>
<li>menu 2</li>
<li>menu 3</li>
<li>menu 4</li>

I want to place the first menu under the second and have it stay there. 我想把第一个菜单放在第二个菜单下,让它留在那里。

However I am stuck on the jQuery a bit. 但是我有点困在jQuery上。

This is what gets the list elements: 这是获取列表元素的原因:

 <ul id="sortable"> 
    <?php  foreach ($rows as $r)
    {
        echo '
        <li id="sort_'.$r->pid.'" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span> 
            ' . $r->page_name . '
        </li>';
    }

    ?>
</ul>

and the jquery: 和jquery:

$( "#sortable" ).sortable({
    placeholder: "ui-state-highlight",
    opacity: 0.6,
    update: function(event, ui) {
        var info = $(this).sortable("serialize");
        alert(info);
    }
});
$( "#sortable" ).disableSelection(); 

I managed to alert the array of the results. 我设法提醒结果阵列。

Now I don't want anybody to write this for me, just a hint on how to use ajax with this for the update. 现在我不希望任何人为我写这个,只是暗示如何使用ajax进行更新。

I think you can use $.ajax(..) inside your update method. 我认为您可以在更新方法中使用$ .ajax(..)。

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

$.ajax({
  url: "submit.php",
  data: info,
  context: document.body,
  success: function(){
    // success
  }
});

I just check info is already serialized, so this should work. 我只是检查信息已经序列化,所以这应该工作。 You can add method property depending on submit type (post, get). 您可以根据提交类型(post,get)添加method属性。

First of all thanks for Kamil Lach for his hint, i managed to do it 首先感谢Kamil Lach的暗示,我设法做到了

Here is my code maybe someone wull make a use for it 这是我的代码,也许某人wull使用它

created a function in my controller and loaded the model in it 在我的控制器中创建了一个函数并将模型加载到其中

function sort_items()
{
    $this->load->model("admin/pages_model");
    $this->pages_model->sort_insert();
}

the model 该模型

function sort_insert()
{
    foreach($this->input->post("sort")  as $p => $id)
    {
        $this->db->query(" UPDATE c_pages SET sort = ".$p." WHERE pid = ".$id." ");

    }   

}

the $p vaiable is the short position and the id is the menu id $ p vaiable是空头头寸,id是菜单ID

and the jquery 和jquery

$( "#sortable" ).sortable({
        placeholder: "ui-state-highlight",
        opacity: 0.6,
    update: function(event, ui) {
        var info = $(this).sortable("serialize");
        $.ajax({
            type: "POST",
            url: "http://localhost/frame/admin/pages/sort_items",
            data: info,
            context: document.body,
            success: function(){
                // alert("cool");
            }
      });

    }
    });
    $( "#sortable" ).disableSelection();

i passed the url to my controller function where my update model is loaded 我将url传递给我的控制器函数,我的加载了更新模型

and the view file 和视图文件

<?php  if($rows) { ?>
    <ul id="sortable">  
        <?php  foreach ($rows as $r)
        {
            echo '
            <li id="sort_'.$r->pid.'" class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span> 
                ' . $r->page_name . '
            </li>';
        }

        ?>

    </ul>
    <?php } else
    {
        echo "There are no pages created yet";
    } 

     ?>

And thanks again for your hint Kamil Lach 再次感谢您的暗示Kamil Lach

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM