简体   繁体   English

重新关联模型laravel

[英]Reorder associated models laravel

In my database, I have a table of items, sorted by a numeric "position" column. 在我的数据库中,我有一个项目表,并按数字“位置”列排序。 Each position is unique, and they define how the data is displayed (1 is displayed first, and 10 is displayed last, for example). 每个位置都是唯一的,它们定义了数据的显示方式(例如,首先显示1,最后显示10)。

I'm trying to write out a controller that allows me to do two key things: 我正在尝试写出一个控制器,该控制器使我能够执行以下两项关键操作:

Firstly, I need to be able to move any item up one place or down one place (obviously if it's at the beginning or end already, it can't be moved up or down, respectively). 首先,我需要能够将任何项目上移或下移一个位置(显然,如果它已经在开始或结束,则不能分别向上或向下移动)。

Secondly, I would like to be able to reorder the items using a drag and drop interface. 其次,我希望能够使用拖放界面对商品进行重新排序。 I've found such interfaces, but I don't know how to use one. 我已经找到了这样的接口,但是我不知道如何使用。

If you've ever successfully implemented a reordering feature for a table, whether it uses drag and drop or "up" and "down" buttons, please help me! 如果您已经成功实现了表格的重新排序功能,无论它是使用拖放还是“上”和“下”按钮,请帮助我! This is proving very, very complicated, and yet it's very important to my website. 事实证明,这非常非常复杂,但是对我的网站来说却非常重要。 I'm not exactly a newbie to Laravel, it's just I underestimated how hard it was to do this! 我并不是Laravel的新手,只是我低估了这样做的难度!

I just tackled the exact same issue. 我只是解决了完全相同的问题。 I will tear down how I handled it. 我将拆除我的处理方式。

For the client side, I used this library. 对于客户端,我使用了这个库。

Javascript: Javascript:

$( document ).ready(function() {
        $("#table-categories").rowSorter({
            onDrop: function(tbody, row, new_index, old_index) {
                $.post("/path/to/api/priority", {old_index: old_index + 1, new_index: new_index + 1, _token: "<?php echo csrf_token(); ?>" }, function(result){
                    console.log('server result: ' + result);
                });
            },
        });
    });

Mind the +1's on old_index and new_index because I am not overwriting the row indexes on the rowsorter library, yet my priority index on model starts from 1, not 0. If you start it from 0, just send the indexes as they are. 请注意在old_indexnew_index上的old_index ,因为我没有覆盖rowsorter库上的行索引,但是我在模型上的priority索引从1开始,而不是0。如果从0开始,则按原样发送索引。

Server Side (Laravel Controller): 服务器端(Laravel控制器):

public function priority()
{
    $old = \Input::get('old_index');
    $new = \Input::get('new_index');

    foreach (\App\Category::all() as $category) 
    {
        if ($category->priority == $old) 
        {
            $category->priority = $new;
            $category->save();
        }
        else
        {
            if ($old < $new) 
            {
                if ($category->priority <= $new AND $category->priority >= $old) 
                {
                    $category->priority -= 1;
                    $category->save();
                }
            }
            else
            {
                if ($category->priority <= $old AND $category->priority >= $new) 
                {
                    $category->priority += 1;
                    $category->save();
                }
            }
        }
    }

    return 'OK';
}

Note: Only implementation missing about this logic is tackling the priority when a row is inserted or deleted from the table, remember to implement them. 注意:只有缺少此逻辑的实现才可以在从表中插入或删除行时解决优先级,请记住实现它们。 If you have issues doing that, let me know so I can add the implementation. 如果您在执行此操作时遇到问题,请告诉我,以便添加实现。

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

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