【问题标题】:Jquery ui sortable on specified table column可在指定表列上排序的 Jquery ui
【发布时间】:2015-07-29 05:37:18
【问题描述】:

我正在使用HTML 表,其中一些数据被动态添加到表中。 表idtitle 中有两列。我正在使用jQuery UI sortable 对表格行进行排序。 HTMLJS 如下:

<table class="table table-striped table-bordered" id="questions-list-table">
            <thead>
            <tr>
                <th>ID</th>
                <th>Title</th>                    
            </tr>
            </thead>
            <tbody>
                <tr>
                    <td> 1 </td>
                    <td>Title 1</td>
                </tr>
                <tr>
                    <td> 2 </td>
                    <td> Title 2</td>
                </tr>               
            </tbody>
        </table>

            <script>

    $(document).ready(function() {

        $("#questions-list-table tbody").sortable({
            disableSelection: true,
            placeholder: "ui-state-highlight",
            axis:"y",
            stop: function(e) {
                console.log("Sorting started");
            }

        });
    });
</script>

目前整行正在排序。我希望唯一的标题是可排序的,id 列应保留在其原始位置。我该怎么做?

【问题讨论】:

    标签: javascript jquery jquery-ui


    【解决方案1】:

    如果 id 列是从 1 开始的序号,则一种可能的解决方案是 t

    $(document).ready(function() {
    
      var start;
      $("#questions-list-table tbody").sortable({
        disableSelection: true,
        placeholder: "ui-state-highlight",
        axis: "y",
        start: function(e, ui) {
          start = ui.item.index();
        },
        stop: function(e, ui) {
          var end = ui.item.index();
    
          var from = start <= end ? start : end,
            to = start <= end ? end + 1 : start + 1;
          $("#questions-list-table tbody > tr").slice(from, to).find('td:first').text(function(i) {
            return from + i + 1;
          })
        }
      });
    });
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
    <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>
    
    <table class="table table-striped table-bordered" id="questions-list-table">
      <thead>
        <tr>
          <th>ID</th>
          <th>Title</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>Title 1</td>
        </tr>
        <tr>
          <td>2</td>
          <td>Title 2</td>
        </tr>
        <tr>
          <td>3</td>
          <td>Title 3</td>
        </tr>
        <tr>
          <td>4</td>
          <td>Title 4</td>
        </tr>
        <tr>
          <td>5</td>
          <td>Title 5</td>
        </tr>
      </tbody>
    </table>

    【讨论】:

    • 谢谢阿伦。现在......你的解决方案对我来说是最可行的
    【解决方案2】:

    您希望一次只对一列进行排序。

    因此您不需要匹配各个行的相应值。

    如果这是您的情况(这不是 Bootstrap 案例),请尝试将其实现为具有一列的两个单独的表

    希望对你有帮助

    【讨论】:

    • 谢谢...但是您的解决方案不可行,正如我之前所说,我的表格中的内容是动态的。所以表格单元格的高度可以变化。所以要保持 id 和 title 的并发会很困难...希望你明白我的原因...
    【解决方案3】:

    如果您真的只想对一列进行排序,那么唯一的选择是两个使用两个表,正如@vijay kani 所说。

    第一个表将仅包含 ID 列,第二个表将包含 Title 列。然后你可以使用 css 让它看起来只有一张桌子。之后,只需使第二个表可排序。

    这是一个例子

    $("#questions-title-table tbody").sortable({
      disableSelection: true,
      axis: "y"
    });
    #questions-id-table {
      float: left;
    }
    <link href="https://code.jquery.com/ui/1.11.4/themes/black-tie/jquery-ui.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
    
    <table id="questions-id-table">
      <thead>
        <tr>
          <th>ID</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
        </tr>
        <tr>
          <td>2</td>
        </tr>
      </tbody>
    </table>
    <table id="questions-title-table">
      <thead>
        <tr>
          <th>Title</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Title 1</td>
        </tr>
        <tr>
          <td>Title 2</td>
        </tr>
      </tbody>
    </table>

    也是fiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-12
      • 2013-09-28
      • 1970-01-01
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多