【问题标题】:Sortable: disable a column in a sortable row可排序:禁用可排序行中的列
【发布时间】:2013-05-07 13:46:54
【问题描述】:

我有一个 4 列和几行的 html 表。行需要是可排序的(我使用的是 jquery ui 可排序的),但是左外层 td 永远不应该移动。所以基本上我希望能够移动该行而不移动该行中的外部左 td。 我试图弄乱项目选项和取消方法,但没有成功。 我查找了类似的线程,但我的目标不是让每个 td 都可排序:

jquery sortable with multiple tbody

How to make a single <td> element in a <tr> not sortable (jquery ui)

使用 sortable 是否可行? 如果有人能指出我正确的方向,我将不胜感激。

我的标记:

<table>
    <tr class="head_row">
    <td>Counter</td>
        <td>Position</td>
        <td>Note</td>
        <td>Visible</td>
    </tr>
    <tbody class="sort">
        <tr>
            <td>1</td>
            <td>1</td>
            <td>TEST1</td>
            <td>Y</td>
     </tr>
     <tr>
            <td>2</td>
            <td>1</td>
            <td>TEST2</td>
            <td>Y</td>
    </tr>
    <tr>
            <td>3</td>
            <td>1</td>
            <td>TEST3</td>
            <td>Y</td>
   </tr>

   </tbody>

jquery:

$(function(){
    $(".sort").sortable({
        helper: fixWidth,
        revert : true,
        cursor: "move",
        opacity: 0.5
    });
});


// fixes the width of the table rows while sorting
    var fixWidth = function(e, tr) {
        var $originals = tr.children();
        var $helper = tr.clone();
        $helper.children().each(function(index)
        {
          $(this).width($originals.eq(index).width())
        });
        return $helper;
    };

这是我目前所拥有的链接:

http://jsfiddle.net/zSzks/2/

在这种情况下,“计数器”列不应移动。

谢谢你, 莫妮卡

【问题讨论】:

    标签: jquery jquery-ui jquery-ui-sortable


    【解决方案1】:

    一种方法——实际上不是最聪明的——可能是将 Counter 部分分离到另一个表中。 它看起来和你想要的一样工作,但正如我所说,我认为这不是解决你的问题的最聪明的方法。

    不管怎样,看看我做了什么:jsfiddle

    HTML

    <table id="counterTable">
        <thead>
            <tr class="head_row">
                <th>Counter</th>
            </tr>
        </thead>
        <tbody class="fake">
            <tr>
                <td>1</td>
            </tr>
            <tr>
                <td>2</td>
            </tr>
            <tr>
                <td>3</td>
            </tr>
        </tbody>
    </table>
    

    CSS

    #counterTable {
        float: left;
        /* to hide the noticeable difference between the 2 tables */
        margin-right: -2px;
    }
    .sort td, .fake td {
        border: 1px solid;
        width: 100px;
    }
    

    顺便说一下,您通常应该将表格标题包含在&lt;th&gt; 标签中,以便您的标题(如计数器、位置、注释...)可以识别为标题。

    编辑(解决方案)

    updated fiddle

    $(".sort").sortable({
        //other config
        update: function (event, ui) {
            $('.sort > tr .counter').each(function (index) {
                $(this).text(index + 1);
            });
        }
    });
    

    【讨论】:

    • 感谢您的回复。通过在 foreach 循环中迭代对象集合(我正在使用 jsp el 和 java)来填充表行,计数器实际上类似于 ;在循环中递增 1,我需要在同一行中的计数器和位置。还有其他建议吗?
    • 抱歉澄清一下:我正在尝试用 counter 的值覆盖 position 的值,并且只要我不必拆分表,我就知道如何做到这一点。我想知道这在可排序中是否可行。我这是不可能的,那么我必须考虑另一种方法。
    • 如果移动了一行,您可以更正计数器值。查看我更新的小提琴。
    • 谢谢一百万,太好了!
    • 酷...不确定这是否对您有帮助,但似乎确实如此。总是乐于助人! :)
    猜你喜欢
    • 2010-11-07
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 2012-04-22
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    相关资源
    最近更新 更多