jQuery UI Sortable表和单元格在拖动tr时正在缩小 [英] jQuery UI Sortable table and cell is shrinking while dragging tr

查看:63
本文介绍了jQuery UI Sortable表和单元格在拖动tr时正在缩小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在拖动时面临两个问题.

While dragging am facing two issues.

  1. 表当我有一个隐藏的td时,它会自我缩小.
  2. 被拖曳的tr单元格(td)正在缩小
  1. table it self is shrinking when i have a hidden td.
  2. dragged tr cell(td)s are shrinking

这是可排序的代码:

$('tbody').sortable({
    items: ">tr",
    appendTo: "parent",
    opacity: 1,
    containment: "document",
    placeholder: "placeholder-style",
    cursor: "move",
    delay: 150,
});

jsfiddle链接

推荐答案

缩小表的问题是因为您有一个隐藏的单元格(在sortable为您创建的占位符中有5个单元格,没有一个是隐藏的. 您可以通过在拖动开始后将placeholder内的第二个td设置为隐藏来解决此问题:

The problem with the shrinking of the table is because you have a hidden cell (and in the placeholder that the sortable creates for you there are 5 cells and non of them are hidden. You can fix this by setting the 2nd td inside the placeholder as hidden once you start the drag:

$(this).find('.placeholder-style td:nth-child(2)').addClass('hidden-td')

第二个问题是由于您拖动的tr已更改为position: absolute而不再具有表的属性所致.您可以通过在该行上设置display: table来解决此问题:

The second problem is caused by the fact that the tr that you drag was changed to position: absolute and it is no longer have the properties of the table. You can fix this by setting display: table to that row:

ui.helper.css('display', 'table')

排序完成后,必须撤消此更改.

This change must be un-done when the the sorting is done.

这是完整的更改:

start: function(event, ui) {
    $(this).find('.placeholder-style td:nth-child(2)').addClass('hidden-td')
    ui.helper.css('display', 'table')
},
stop: function(event, ui) {
    ui.item.css('display', '')
}

这是一个可行的例子

$('tbody').sortable({
  items: ">tr",
  appendTo: "parent",
  opacity: 1,
  containment: "document",
  placeholder: "placeholder-style",
  cursor: "move",
  delay: 150,
  start: function(event, ui) {
  	$(this).find('.placeholder-style td:nth-child(2)').addClass('hidden-td')
    ui.helper.css('display', 'table')
  },
  stop: function(event, ui) {
  	ui.item.css('display', '')
  }
});

.sort-table{
  width: 100%;
  border: 1px solid #cecece;
  background-color: #d5a45a;
}
thead{
  background-color: #0e79c4;
}
th{
     font-size: 1em;
    line-height: 1.375em;
    font-weight: 400;
    background-color: #0e79c4;
    vertical-align: middle;
    padding: 0.5em 0.9375em;
    text-align: left;
}
tr{
  border: 1px solid #cecece;
}
td{
  padding: 1em;
  vertical-align: middle;
   display: table-cell;
   border-top: 1px solid #cecece;
}
.hidden-td{
  display: none;
}
.placeholder-style{
  background-color: grey;
}

<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<table class="sort-table">
  <thead>
    <tr>
      <th class="header-td">Column1 </th>
      <td class="hidden-td">row1 hidden td</td>
      <th class="header-td">Column2 </th>
      <th class="header-td">Column3 </th>
      <th class="header-td">Column4 </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="body-td">row1 td1</td>
      <td class="body-td hidden-td">row1 hidden td</td>
      <td class="body-td">row1 td2</td>
      <td class="body-td">row1 td3</td>
      <td class="body-td">row1 td4</td>
    </tr>
    <tr>
      <td class="body-td">row2 td1</td>
      <td class="body-td hidden-td">row1 hidden td</td>
      <td class="body-td">row2 td2</td>
      <td class="body-td">row2 td3</td>
      <td class="body-td">row2 td4</td>
    </tr>
    <tr>
      <td class="body-td">row3 td1</td>
      <td class="body-td hidden-td">row1 hidden td</td>
      <td class="body-td">row3 td2</td>
      <td class="body-td">row3 td3</td>
      <td class="body-td">row3 td4</td>
    </tr>
    <tr>
      <td class="body-td">row4 td1</td>
      <td class="body-td hidden-td">row1 hidden td</td>
      <td class="body-td">row4 td2</td>
      <td class="body-td">row4 td3</td>
      <td class="body-td">row4 td4</td>
    </tr>
  </tbody>
</table>

和一个jsfiddle: http://jsfiddle.net/mjkq4fb6/

And a jsfiddle: http://jsfiddle.net/mjkq4fb6/

这篇关于jQuery UI Sortable表和单元格在拖动tr时正在缩小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆