Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 重新排序表列?_Javascript_Jquery_Html Table - Fatal编程技术网

Javascript 重新排序表列?

Javascript 重新排序表列?,javascript,jquery,html-table,Javascript,Jquery,Html Table,有人知道使用jQuery对表列重新排序的方法吗 我不是说排序——我是说在表中动态地向左或向右移动整个列 我知道这很好,但我不需要允许用户移动列的东西,我需要能够以可配置的方式重新排序的东西 此代码应该适合您 $("table tr").each(function () { $(this).find("td").eq(1).after($(this).find("td").eq(0)); }); 编辑:如果将$(this).find(“td”)分配给变量,这将提供更好的性能。但背景是一

有人知道使用jQuery对表列重新排序的方法吗

我不是说排序——我是说在表中动态地向左或向右移动整个列


我知道这很好,但我不需要允许用户移动列的东西,我需要能够以可配置的方式重新排序的东西

此代码应该适合您

$("table tr").each(function () {
    $(this).find("td").eq(1).after($(this).find("td").eq(0));
});

编辑:如果将$(this).find(“td”)分配给变量,这将提供更好的性能。但背景是一个单一的tr。所以我认为只要给出一个想法就足够了

$("table tr").each(function () {
    var rows = $(this).find("td");
    rows.eq(1).after(rows.eq(0));
});

其想法是遍历表中的所有行(tr),并交换所需的td。让我们交换第2列和第4列:

$('table tr').each(function() {
    var tr = $(this);
    var td1 = tr.find('td:eq(1)'); // indices are zero-based here
    var td2 = tr.find('td:eq(3)');
    td1.detach().insertAfter(td2);
});

我希望这能有所帮助。

在阅读dragtable插件的源代码时,作者提到,实际移动表列的算法是在comp.lang.javascript新闻组的讨论中产生的。讨论内容如下:

在该线程中,OP不是询问重新排序的UI端,而是帮助调试他已经编写的交换两列的函数。在进一步的讨论中,它发展成代码,使您能够传递特定的列顺序,并让代码计算从当前顺序到指定顺序的所有必要交换/移动


它不是jQuery(c.l.js上的大多数海报都不喜欢它和大多数其他js框架),因此它的代码可以根据您的需要进行调整,然后包含在任何地方。

我将它与jQueryUI结合使用,以获得一些出色的拖放操作:

(function() {
    var local = {};
    local.containment = 'parent';
    local.revert = true;
    $('body thead th').draggable(local);
    $('body thead th').droppable({
        drop: dropZone
    });
    function dropZone(myEvent, myUI ) {
        var head = {};

        head.dragIndex = myUI.draggable.index();
        head.dropIndex = $(this).index();
        head.rows = $(this).closest('thead').find('tr');
        head.cellIndex = head.rows.find('th').length-1;
        head.rows.each(processTableHeaderRows);

        function processTableHeaderRows(index, element) {
            var row = {}

            row.tr = $(element);
            row.drag = row.tr.find('th:eq(' + head.dragIndex + ')');
            row.drop = row.tr.find('th:eq(' + head.dropIndex + ')');
            if (head.dropIndex === head.cellIndex) {
                row.drag.detach().insertAfter(row.drop);
            } else {
                row.drag.detach().insertBefore(row.drop);
            }
        }
        // Same thing here for td instead of th
        $(this).closest('table').find('tbody > tr').each(processRows);
        function processRows(index, element) {
            var row = {};

            row.tr = $(element);
            row.drag = row.tr.find('td:eq(' + head.dragIndex + ')');
            row.drop = row.tr.find('td:eq(' + head.dropIndex + ')');
            if (head.dropIndex === head.cellIndex) {
                row.drag.detach().insertAfter(row.drop);
            } else {
                row.drag.detach().insertBefore(row.drop);
            }
        }
    }
})();

试着让它工作,但我做不到。不过还是继续

重复“$(this)”,重复“.find”(“td”)”-是的,让我们编写较慢的代码。另外,我认为你会得到一个重复的列,但不确定这一点。不,它不会重复。当文档中当前的DOM节点插入其他位置时,它们将从其原始位置移除,而不是克隆。将更灵活地包装在传递对表元素和要交换的列索引的引用的函数中,但除此之外,这是非常正确的。您的网站不再处于活动状态
http://www.phillipsenn.co.uk/