【问题标题】:jQuery UI sortable with table and modal pop upsjQuery UI 可排序的表格和模式弹出窗口
【发布时间】:2013-01-09 15:30:52
【问题描述】:

一个:我需要一个带有可排序行的表(请参阅 jqueryui.com)。通常,这些示例会为您提供列表项,但很有可能使用表格行来执行此操作。这是我的作业http://www.foliotek.com/devblog/make-table-rows-sortable-using-jquery-ui-sortable/ 和他的 jsfiddle:http://jsfiddle.net/bgrins/tzYbU/ 他基本上解释了与使表格行可排序相关的修复。

两个:我需要这些可排序的表格行在悬停时弹出模式。看来我只能有一个或另一个。可排序的行移动,但弹出窗口不起作用(http://jsfiddle.net/anschwem/gAmnQ/2/ 它在我的末端排序/拖动,但不是小提琴)模态弹出窗口工作和排序,但它是列表项目。 (http://jsfiddle.net/anschwem/gAmnQ/1/)。然后有一个奇怪的事件,一行被踢出表格,只有它的悬停工作(http://jsfiddle.net/anschwem/gAmnQ/2/)。无论如何,由于正确的间距以及我还需要动态创建新行的事实,我需要行。有任何想法吗?

这是我的可排序表格的 HTML:

<table class="table_177" id="sortable2" class="connectedSortable inputboxes">
<thead>
  <tr>
    <th>Vessel Name</th>
    <th>Hull/IMO No.</th>
  </tr>
</thead>  
  <tr>
    <a class="productsModal1" style="text-decoration:none">
    <td class="ui-state-highlight" style="border-right:none">SS Mary Mae</td>
    <td class="ui-state-highlight" style="border-left:none">12345</td></a>
  </tr>      
  <tr>
    <a class="productsModal1" style="text-decoration:none">
    <td class="ui-state-highlight" style="border-right:none">EMS 234</td>
    <td class="ui-state-highlight" style="border-left:none">12346</td></a>
  </tr> 
</table>

我的隐藏模式的 HTML 和 CSS:

<style>
div.productsModal1
{
    display:none;
    position:absolute;
    border:solid 1px black;
    padding:8px;
    background-color:white;
}
a.productsModal1:hover + div.productsModal1
{
    display:block;
/*  animation:fade-out .5s 1;
    animation-transition-property: opacity;*/
}
div.productsModal1:hover
{
    display:block;
/*  animation:fade-out .5s 1;
    animation-transition-property: opacity;*/
}
</style>
    <div class="productsModal1" style="top: 230px; left: 320px; z-index:9999" >
  <table id="menu1">
    <tr>
      <th>Vessel Name</th>
      <th>Vessel Type</th>
      <th>Hull/IMO No.</th>
    </tr>
    <tr>
      <td>SS Mary Mae</td>
      <td>Barge</td>
      <td>12345</td>
    </tr>
  </table>
  </div>
  <div class="productsModal1" style="top: 230px; left: 320px; z-index:9999" >
  <table id="menu1">
    <tr>
      <th>Vessel Name</th>
      <th>Vessel Type</th>
      <th>Hull/IMO No.</th>
    </tr>
    <tr>
      <td>EMS 234</td>
      <td>Barge</td>
      <td>67891</td>
    </tr>
  </table>
  </div>

还有我的代码:

$(window).load(function(){
// Return a helper with preserved width of cells
var fixHelperModified = function(e, tr) {
    var $originals = tr.children();
    var $helper = tr.clone();
    $helper.children().each(function(index)
    {
      $(this).width($originals.eq(index).width())
    });
    return $helper;
};
    $("#sortable2 tbody").sortable({helper: fixHelperModified}).disableSelection();
});//]]> 

【问题讨论】:

  • 我不认为锚标签是 &lt;tr&gt; 标签的有效子代.. 或 &lt;td&gt; TR Documentation , TD Documentation.. 的有效父代。您还在寻找一张可排序的表行?还是多张桌子?
  • 只有一张桌子。表格需要可排序并具有弹出模式。

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


【解决方案1】:
  • 首先,删除包裹td 的锚标记,因为那是无效的HTML.. 并将类添加到您的tr,还使用数据索引来存储默认索引,因为您将移动它们并且需要一种方法将它们与模态联系起来

把 tr 改成这个

<tr class="productsModal1" data-index=0>
  <td class="ui-state-highlight" style="border-right:none">SS Mary Mae</td>
  <td class="ui-state-highlight" style="border-left:none">12345</td>
</tr>
<tr class="productsModal1" data-index=1>
  <td class="ui-state-highlight" style="border-right:none">EMS 234</td>
  <td class="ui-state-highlight" style="border-left:none">12346</td>
</tr>
  • 不知道为什么 $(window).load 函数不起作用..但是使用 document.ready 函数修复了排序不起作用的问题..

  • 然后您可以使用 jQuery 显示/隐藏相关的 div

JS

$('#sortable2 tbody tr').on({
    mouseenter: function () {
      $('div.' + $(this).attr('class')) // <-- this gets the div.. though the share the same class so can probably just hardcode
            .eq($(this).data('index')) // <-- gets the correct one according to data-index
            .show();
    },
    mouseleave: function () {
      $('div.' + $(this).attr('class'))
            .eq($(this).data('index'))
            .hide();
    }
});

FIDDLE

另一件事是,如果您不想进入并硬编码数据属性。您可以在 document.ready 函数中编写 use jQuery 并动态添加它。

$('#sortable2 tbody tr').each(function(i,v){
     $(this).data('index',i);
});

【讨论】:

  • 完美修复,再次@Wirey。
  • 好的。所以我以前可以使用我不能的新代码向可排序表添加新行。任何想法如何实施?有用户输入框,他们输入信息并单击按钮。该信息添加到具有相同悬停模式功能的可排序表中。当我之前使用它时,在视觉上它看起来是正确的,但它没有模态,并且它阻止了表格的其余部分进行排序。也许是因为我使用的是 innerHTML 而不是 jQuery?两者都行。
  • 所以你也在动态创建 div 模态 div?
  • @triplethreat77 让我知道这是否可以帮助您解决问题jsfiddle.net/5f5xy
  • @triplethreat77 嗯.. 你的新行有悬停模式吗?您是否如小提琴所示委托悬停事件?因为您是动态添加元素,所以我对其进行了更改。对于选择,您也可以做同样的事情,只需像其他人一样修改文本jsfiddle.net/AGEt5
猜你喜欢
  • 2015-01-02
  • 1970-01-01
  • 2012-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 2011-04-24
相关资源
最近更新 更多