简体   繁体   English

jQuery datatable问题与Expand-collapse

[英]Jquery datatable issue with expand-collapse

I have a datatable with only one td , every td has a collapse element in it, on the first page everything is ok but on all other pages the element is displayed expanded and cannot be collapsed. 我有一个只有一个td ,每个td都有一个collapse元素,在第一页上一切正常,但在所有其他页面上,该元素显示为展开状态且无法折叠。 As far as i can see the problem is that the second/third pages are already hidden and when i call them via the datatable it makes all the elements visible, I could not find a way around it and any help is much appreciated. 据我所知,问题是第二/第三页已经被隐藏,当我通过数据表调用它们时,它使所有元素可见,我找不到解决的办法,任何帮助将不胜感激。 this is the link http://86.107.56.8/ and the code is: 这是链接http://86.107.56.8/和代码是:

                $(document).ready(function() {
                oTable = $("#example").dataTable({
                    "bJQueryUI": true,
                    "sPaginationType": "full_numbers",
                    "bFilter": true,
                    "bInfo": false,
                    "aaSorting": [[ 2, "asc" ]],
                    "iDisplayLength": 2,
                    "aLengthMenu": [[2, 4, 6], [2, 4, 6]]
                });
            } );

        jQuery(document).ready(function() {
          jQuery(".content").hide();
          //toggle the componenet with class msg_body
          jQuery(".heading").click(function()
          {
            jQuery(this).next(".content").slideToggle(500);
          });
        });

    <div style="width:704px">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">

    <thead>
        <tr>
            <th width="100px">escala</th>
            <th width="520px">detalii</th>
            <th width="40px">pret</th>
            <th width="4px">valuta</th>
            <th width="40px">buton</th>

        </tr>
    </thead>
    <tbody>
        <tr>
            <td width="60px" align="center"><p>Fara<br />escala</p></td>
            <td width="520px">
                <div class="layer1">
                    <p class="heading"><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td align="left" style="background-color:#2f578b;color:fff;">Bucuresti - Baneasa BBU<br /> 24 Dec 06:05</td><td align="right" style="background-color:#2f578b;color:fff;">Roma - Ciampino CIA<br /> 24 Dec 07:10</td></tr></table></p>
                    <div class="content"><p>Detalii zbor</p></div>
                </div>
            </td>
            <td width="40px" align="right">10</td>
            <td width="4px" align="left">&euro;</td>
            <td width="40px" align="right">buton</td>
        </tr>
        <tr>
            <td width="60px" align="center"><p>Fara<br />escala</p></td>
            <td width="520px">
                <div class="layer1">
                    <p class="heading"><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td align="left" style="background-color:#2f578b;color:fff;">Bucuresti - Baneasa BBU<br /> 24 Dec 06:05</td><td align="right" style="background-color:#2f578b;color:fff;">Roma - Ciampino CIA<br /> 24 Dec 07:10</td></tr></table></p>
                    <div class="content"><p>Detalii zbor</p></div>
                </div>
            </td>
            <td width="40px" align="right">10</td>
            <td width="4px" align="left">&euro;</td>
            <td width="40px" align="right">buton</td>
        </tr>
        <tr>
            <td width="60px" align="center"><p>Fara<br />escala</p></td>
            <td width="520px">
                <div class="layer1">
                    <p class="heading"><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td align="left" style="background-color:#2f578b;color:fff;">Bucuresti - Baneasa BBU<br /> 24 Dec 06:05</td><td align="right" style="background-color:#2f578b;color:fff;">Roma - Ciampino CIA<br /> 24 Dec 07:10</td></tr></table></p>
                    <div class="content"><p>Detalii zbor</p></div>
                </div>
            </td>
            <td width="40px" align="right">10</td>
            <td width="4px" align="left">&euro;</td>
            <td width="40px" align="right">buton</td>
        </tr>
        <tr>
            <td width="60px" align="center"><p>Fara<br />escala</p></td>
            <td width="520px">
                <div class="layer1">
                    <p class="heading"><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td align="left" style="background-color:#2f578b;color:fff;">Bucuresti - Baneasa BBU<br /> 24 Dec 06:05</td><td align="right" style="background-color:#2f578b;color:fff;">Roma - Ciampino CIA<br /> 24 Dec 07:10</td></tr></table></p>
                    <div class="content"><p>Detalii zbor</p></div>
                </div>
            </td>
            <td width="40px" align="right">10</td>
            <td width="4px" align="left">&euro;</td>
            <td width="40px" align="right">buton</td>
        </tr>
        <tr>
            <td width="60px" align="center"><p>Fara<br />escala</p></td>
            <td width="520px">
                <div class="layer1">
                    <p class="heading"><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td align="left" style="background-color:#2f578b;color:fff;">Bucuresti - Baneasa BBU<br /> 24 Dec 06:05</td><td align="right" style="background-color:#2f578b;color:fff;">Roma - Ciampino CIA<br /> 24 Dec 07:10</td></tr></table></p>
                    <div class="content"><p>Detalii zbor</p></div>
                </div>
            </td>
            <td width="40px" align="right">10</td>
            <td width="4px" align="left">&euro;</td>
            <td width="40px" align="right">buton</td>
        </tr>
        <tr>
            <td width="60px" align="center"><p>Fara<br />escala</p></td>
            <td width="520px">
                <div class="layer1">
                    <p class="heading"><table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td align="left" style="background-color:#2f578b;color:fff;">Bucuresti - Baneasa BBU<br /> 24 Dec 06:05</td><td align="right" style="background-color:#2f578b;color:fff;">Roma - Ciampino CIA<br /> 24 Dec 07:10</td></tr></table></p>
                    <div class="content"><p>Detalii zbor</p></div>
                </div>
            </td>
            <td width="40px" align="right">10</td>
            <td width="4px" align="left">&euro;</td>
            <td width="40px" align="right">buton</td>
        </tr>
    </tbody>
</table>
</div>

Try this code $(this).next().slideToggle(500); 试试下面的代码$(this).next().slideToggle(500); (without selector in next function) (下一个功能中没有选择器)

I can't tell for sure, since the link provided doesn't go to a DataTable page anymore. 我不能肯定地说,因为提供的链接不再转到DataTable页面。

My guess is that the problem is simply this: you are both hiding and binding click in the document ready function. 我的猜测是问题很简单:隐藏和绑定单击文档准备功能中的键。 That means it fires ONCE, when the document is ready. 这意味着当文档准备好时,它将一次触发。 Those functions are not invoked again. 这些功能不会再次调用。

I'd have to take more time to look at the code to get a better feel for which elements to hide, but in general what you should be doing is sticking the hiding function into the plug-in's own fnDrawCallback initialization callback function. 我不得不花更多的时间查看代码,以更好地了解要隐藏的元素,但是通常,您应该做的是将隐藏函数粘贴到插件自己的fnDrawCallback初始化回调函数中。 However, I'm actually not sure that hiding it with jQuery is the best way to go. 但是,我实际上不确定用jQuery隐藏它是最好的方法。 Again, I'd have to dig more into the sample code. 同样,我将不得不深入研究示例代码。

Regarding the click, the table is "redrawn" when you go to the next page. 关于点击,当您进入下一页时,表格被“重绘”。 If you are using jQuery 1.7+, you should use the .on() event binding function instead of .click() . 如果您使用的是jQuery 1.7+,则应使用.on()事件绑定函数而不是.on() .click() This one can stay in the document ready function. 这一项可以保留在文档准备功能中。 However, you are going to select and assign a different listener than .heading() . 但是,您将选择和分配与.heading()不同的侦听器。 And to do this we are going to modify the HTML a bit, too: 为此,我们还将对HTML进行一些修改:

HTML: HTML:

<div class="tableWrapper" style="width:740px"> <!-- you should not have inline styles; you can add width to the new tableWrapper class -->
  <table> ... etc ... </table>
</div>

JS: JS:

jQuery(".tableWrapper").on('click', '.heading', function()
    {
      jQuery(this).next(".content").slideToggle(500);
    });
});

Side note: I think you can pass nothing (void) in .next() since it's the first of the next siblings that you need. 旁注:我认为您不能在.next()传递任何内容(无效),因为它是您需要的下一个同级对象中的第一个。 I'd have to check the docs. 我必须检查文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM