【问题标题】:How to add sortable cell to table row where is no other sortable items?如何将可排序单元格添加到没有其他可排序项目的表格行?
【发布时间】:2018-06-17 18:53:19
【问题描述】:

尝试使用 JQuery 制作带有可排序单元格的表格。我面临的问题是,当行中没有更多可排序的单元格时,无法将单元格移入其中。我试图添加不可见的可排序单元格(到第一行),但没有帮助。

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<table>
  <tbody>
    <tr>
      <td class="ui-state-disabled"></td>
      <td class="ui-state-disabled">a</td>
      <td class="ui-state-disabled">b</td>
    </tr>
    <tr>
      <td class="ui-state-disabled">1</td>
      <td>First row: 1</td>
      <td>First row: 2</td>
      <td class=" cell-placeholder">holder</td>
    </tr>
    <tr>
      <td class="ui-state-disabled">2</td>
      <td>
        <button>
          Second row: 1
        </button>
      </td>
      <td>
        <input style="text" value="Second row: 2" />
      </td>
    </tr>
  </tbody>
</table>


$("table tbody ").sortable({
  items: 'td:not(.ui-state-disabled)',
  cancel: ".disabled",
  revert: true
}).disableSelection();

td {
  padding: 5px 10px;
  border: dotted 1px black;
}

.cell-placeholder {
  display: none;
}

这里是jsfiddle

【问题讨论】:

    标签: jquery html-table jquery-ui-sortable


    【解决方案1】:

    您需要对 rows 元素进行排序并使用connectWith

    $("table tbody tr").sortable({
       items: 'td:not(.ui-state-disabled)',
       connectWith: "table tbody tr",
       cancel: ".disabled",
    revert: true
     }).disableSelection();
    td {
      padding: 5px 10px;
      border: dotted 1px black;
    }
    
    .cell-placeholder {
      display: none;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
    <table>
      <tbody>
        <tr>
          <td class="ui-state-disabled"></td>
          <td class="ui-state-disabled">a</td>
          <td class="ui-state-disabled">b</td>
        </tr>
        <tr>
          <td class="ui-state-disabled">1</td>
          <td>First row: 1</td>
          <td>First row: 2</td>
          <td class=" cell-placeholder">holder</td>
        </tr>
        <tr>
          <td class="ui-state-disabled">2</td>
          <td>
            <button>
              Second row: 1
            </button>
          </td>
          <td>
            <input style="text" value="Second row: 2" />
          </td>
        </tr>
      </tbody>
    </table>

    【讨论】:

    • 这也可以帮助 diabling 行。谢谢你
    猜你喜欢
    • 2021-10-14
    • 1970-01-01
    • 2015-07-27
    • 2016-06-06
    • 1970-01-01
    • 2010-10-16
    • 2014-10-17
    • 1970-01-01
    • 2016-10-10
    相关资源
    最近更新 更多