jQuery UI - 使用 Sortable 拖放行时,行之间的空间会增加

发布于 2024-12-11 10:08:31 字数 1770 浏览 0 评论 0原文

我正在使用 jQuery Ui 的 Sortable 小部件来实现表行中的拖放功能。问题是,如果我将第二行和第三行向下拖动一点(不足以移动下一行),行之间的空间就会增加。现在,如果我确实将第二行与第三行交换(通过将第二行拖动到第三行下方),则第一行和第二行之间会积累大量空间。如果重复上述步骤,我们可以继续增加行间距

最初,Initially

最后,Finally

The code is as follows:

<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.ui.sortable.min.js"></script>
<script>
    $(document).ready(function() {
        $( "#table tbody " ).sortable({placeholder : 'red',axis:'y',containment:'tbody'});
        $( "#table tbody" ).disableSelection();
    });
</script>
<style>
.red{
    background-color : red;height: 1.5em; line-height: 1.2em;
}
#table{
    border-spacing : 2px;
    background-color : light gray;
}
#table tr{
    background-color : yellow;  
}
body{
    background-color : gray;
}
</style>

</head>
<body>
    <table id="table">
    <thead>
    </thead>
    <tbody>
    <tr>
        <td>
        <label for="name1">Enter name</label>
        <input type="text" id="name1"/>
        </td>
    </tr>
    <tr>
        <td>
        <label for="name2">Enter name</label>
        <input type="text" id="name2"/>
        </td>
    </tr>
    <tr>
        <td>
        <label for="name3">Enter name</label>
        <input type="text" id="name2"/>
        </td>
    </tr>
    </tbody>
    </table>
</body>
</html>

它对表的使用很糟糕,但是我必须容忍一些遗留代码

另外,为什么占位符选项不起作用?

I am using jQuery Ui's Sortable widget to implement drag and drop functionality in table rows. The problem is that if I drag the 2nd and 3rd rows down a little(not enough for the following row to be displaced), the space between the rows increases. Now, if I actually do swap the 2nd row with the 3rd (by dragging the 2nd below the 3rd), a lot of space accumulates between the first and 2nd row. If the above steps are repeated, we can continue to increase space between rows

Initially,Initially

Finally,Finally

The code is as follows:

<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.ui.sortable.min.js"></script>
<script>
    $(document).ready(function() {
        $( "#table tbody " ).sortable({placeholder : 'red',axis:'y',containment:'tbody'});
        $( "#table tbody" ).disableSelection();
    });
</script>
<style>
.red{
    background-color : red;height: 1.5em; line-height: 1.2em;
}
#table{
    border-spacing : 2px;
    background-color : light gray;
}
#table tr{
    background-color : yellow;  
}
body{
    background-color : gray;
}
</style>

</head>
<body>
    <table id="table">
    <thead>
    </thead>
    <tbody>
    <tr>
        <td>
        <label for="name1">Enter name</label>
        <input type="text" id="name1"/>
        </td>
    </tr>
    <tr>
        <td>
        <label for="name2">Enter name</label>
        <input type="text" id="name2"/>
        </td>
    </tr>
    <tr>
        <td>
        <label for="name3">Enter name</label>
        <input type="text" id="name2"/>
        </td>
    </tr>
    </tbody>
    </table>
</body>
</html>

Its a bad use of tables, but there is some legacy code I have to tolerate

Also, why is the placeholder option not working ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情独悲 2024-12-18 10:08:31

似乎您正在加载一组非常旧的库,如果您将代码更改为这两行,问题就消失了。

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>

如果你愿意,你可以将 jqueryui 缩小到可排序,大约 38kb

Seems that you are loading a very old set of librarys, if you change your code to this two line the issue is gone.

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>

If you want you can narrow jqueryui just to sortable and that is about 38kb

我ぃ本無心為│何有愛 2024-12-18 10:08:31

它似乎与表格边框间距有关。如果您设置“table { border-spacing:0px; }”,您将最大限度地减少空间蠕变——如果我在测试中给表格行添加边框,则最多 1px。

您可以向表格单元格添加标记吗?如果是这样,您可以将标签和输入嵌套到一个容器中,然后使用该容器创建间距。

<tr>
  <td>
    <div style="margin:2px 0;">
      <label for="name1">Enter name</label>
      <input type="text" id="name1"/>
    </div>
  </td>
</tr>

It appears to be related to the table border-spacing. If you set the "table { border-spacing:0px; }" you will minimize the space creep--at most 1px if I give my table rows a border on my tests.

Are you able to add markup to the table cells? If so, you can nest the label and inputs into a container that you can then use to create spacing.

<tr>
  <td>
    <div style="margin:2px 0;">
      <label for="name1">Enter name</label>
      <input type="text" id="name1"/>
    </div>
  </td>
</tr>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文