Javascript Jquery插入具有可排序位置的列表元素

Javascript Jquery插入具有可排序位置的列表元素,javascript,jquery,html,jquery-ui,jquery-ui-sortable,Javascript,Jquery,Html,Jquery Ui,Jquery Ui Sortable,如何使用jquery使其能够从两个列表中的任意一个单击项目,从而将所选项目移动到另一个列表中 将项目移动到另一个列表时,新的排序应首先基于项目编号(数据值),然后是左项目,然后是右项目 例如,如果要单击“左项目2”,则应将该项目从左列表中删除,右列表应显示右项目1、左项目2、右项目2、右项目3、右项目4、右项目5 左侧项目1 左侧项目2 左侧项目3 左项4 左侧第5项 右侧项目1 右侧项目2 右侧项目3 右项4 右侧第5项 我会做类似的事情,尽管我会花更多的时间清理它 您需要将单击的

如何使用jquery使其能够从两个列表中的任意一个单击项目,从而将所选项目移动到另一个列表中

将项目移动到另一个列表时,新的排序应首先基于项目编号(数据值),然后是左项目,然后是右项目

例如,如果要单击“左项目2”,则应将该项目从左列表中删除,右列表应显示右项目1、左项目2、右项目2、右项目3、右项目4、右项目5


  • 左侧项目1
  • 左侧项目2
  • 左侧项目3
  • 左项4
  • 左侧第5项
  • 右侧项目1
  • 右侧项目2
  • 右侧项目3
  • 右项4
  • 右侧第5项

我会做类似的事情,尽管我会花更多的时间清理它

您需要将单击的元素从源列表移动到目标列表,然后根据属性对目标列表进行排序

下面的Javascript以两种方式实现这一点

var moveToListAndSort = function(source, target, attribute) {
    attribute = attribute || 'data-value';  

    // Whenever a list item is clicked in the source list
    $(source).on('click', 'li', function() {
        // Remove the clicked list item from its current list
        $(this).remove();

        // Append it to the target list
        $(target).append($(this));

        // Sort the target list based on an attribute
        $(target).children('li').detach().sort(function(a, b) {
            var valueA = a.getAttribute(attribute);
            var valueB = b.getAttribute(attribute);

            var sortA = a.getAttribute('data-sort');
            var sortB = b.getAttribute('data-sort');

            // Compare the value attributes of the two list items
            if (valueA > valueB) {
                return 1;
            }

            if (valueA < valueB) {
                return -1;
            }

            // The value attributes are the same, so compare the
            // sort attributes
            if (sortA > sortB) {
                return 1;
            }

            if (sortA < sortB) {
                return -1;  
            }

            return 0;
        }).appendTo(target);
    });
};

moveToListAndSort('.my-list-left', '.my-list-right');
moveToListAndSort('.my-list-right', '.my-list-left');

jQuery DOM操作和事件处理。
打开
test1.html
,并添加一些Javascript代码,这样单击两个列表中的任何一个列表中的项目都会将所选项目移动到另一个列表中

将项目移动到其他列表时,新的排序应首先基于项目编号(
数据值
),然后是左项目,然后是右项目

例如,如果要单击“左侧项目2”,则该项目应从左侧列表中删除,右侧列表应显示
右侧项目1、左侧项目2、右侧项目2、右侧项目3、右侧项目4、右侧项目5


  • 左侧项目1
  • 左侧项目2
  • 左侧项目3
  • 左项4
  • 左侧项目5
  • 右项1
  • 右项2
  • 右项3
  • 右项4
  • 右项5

我会做类似的事情,尽管我会花更多的时间清理它

您需要将单击的元素从源列表移动到目标列表,然后根据属性对目标列表进行排序

下面的Javascript以两种方式实现这一点

var moveToListAndSort = function(source, target, attribute) {
    attribute = attribute || 'data-value';  

    // Whenever a list item is clicked in the source list
    $(source).on('click', 'li', function() {
        // Remove the clicked list item from its current list
        $(this).remove();

        // Append it to the target list
        $(target).append($(this));

        // Sort the target list based on an attribute
        $(target).children('li').detach().sort(function(a, b) {
            var valueA = a.getAttribute(attribute);
            var valueB = b.getAttribute(attribute);

            var sortA = a.getAttribute('data-sort');
            var sortB = b.getAttribute('data-sort');

            // Compare the value attributes of the two list items
            if (valueA > valueB) {
                return 1;
            }

            if (valueA < valueB) {
                return -1;
            }

            // The value attributes are the same, so compare the
            // sort attributes
            if (sortA > sortB) {
                return 1;
            }

            if (sortA < sortB) {
                return -1;  
            }

            return 0;
        }).appendTo(target);
    });
};

moveToListAndSort('.my-list-left', '.my-list-right');
moveToListAndSort('.my-list-right', '.my-list-left');

jQuery DOM操作和事件处理。
打开
test1.html
,并添加一些Javascript代码,这样单击两个列表中的任何一个列表中的项目都会将所选项目移动到另一个列表中

将项目移动到其他列表时,新的排序应首先基于项目编号(
数据值
),然后是左项目,然后是右项目

例如,如果要单击“左侧项目2”,则该项目应从左侧列表中删除,右侧列表应显示
右侧项目1、左侧项目2、右侧项目2、右侧项目3、右侧项目4、右侧项目5


  • 左侧项目1
  • 左侧项目2
  • 左侧项目3
  • 左项4
  • 左侧项目5
  • 右项1
  • 右项2
  • 右项3
  • 右项4
  • 右项5
这是一个好的开始<