使动态表格显示有限的结果并提供扩展选项

发布于 2024-12-09 19:42:04 字数 2780 浏览 2 评论 0原文

我有以下 HTML,我无法直接编辑它,因为它是动态生成的:

<table class="v65-productDisplay" width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
</tbody>
</table>

TR 内的内容并不重要(我不认为),所以我将所有这些内容都保留了。 此代码块包含 1 个产品:

<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>

所以基本上我想使用 jQuery 来仅显示其中 4 个产品,并有一个按钮来展开/显示其余产品。我完全困惑了,不知道从哪里开始。我尝试找到每 4 个 TR,将它们包装在 div 中,然后尝试显示/隐藏它们,但它搞乱了我的布局,根本不起作用。如果有人可以提供帮助,将不胜感激!

编辑:现在使用这个:

<script type="text/javascript">
$(document).ready(function(){
var hiddenElements = $('.v65-productDisplay tr:gt(7)').hide();

if (hiddenElements.size() > 0) {
  var showCaption = '<b>View </b>' + hiddenElements.size() + '<b> More Products</b>';
  $('.v65-productDisplay').append(
      $('<li id="toggler">' + showCaption + '</li>')
          .toggle(
              function() { 
                  hiddenElements.show();
                  $(this).html('<b>Collapse</b>');
              }, 
              function() { 
                  hiddenElements.hide();
                  $(this).text(showCaption);
              }
          )
  );
 }
 });
</script>

工作完美,除了在“显示更多产品”的显示文本中它正在计算每个TR。因此,与其说“更多产品”,不如说“更多产品”。我如何编辑它以正确显示该文本?

I have the following HTML that I can't directly edit as it is being dynamically generated:

<table class="v65-productDisplay" width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>
</tbody>
</table>

It shouldn't matter what's inside the TR (I don't think) so I left all that stuff out.
This code block comprises 1 product:

<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr class="v65-productDisplay-row">
<tr>

So basically I would like to use jQuery to display only 4 of these products and have a button to expand/show the rest of them. I am thoroughly confused and don't know where to start. I tried finding each 4 of the TRs, wrapping them in a div, then trying to show/hide them but it screwed up my layout and didn't work at all. If anyone can help it would be much appreciated!

EDIT: Using this now:

<script type="text/javascript">
$(document).ready(function(){
var hiddenElements = $('.v65-productDisplay tr:gt(7)').hide();

if (hiddenElements.size() > 0) {
  var showCaption = '<b>View </b>' + hiddenElements.size() + '<b> More Products</b>';
  $('.v65-productDisplay').append(
      $('<li id="toggler">' + showCaption + '</li>')
          .toggle(
              function() { 
                  hiddenElements.show();
                  $(this).html('<b>Collapse</b>');
              }, 
              function() { 
                  hiddenElements.hide();
                  $(this).text(showCaption);
              }
          )
  );
 }
 });
</script>

Works perfectly except in the display text of "Show x more Products" it is counting every TR. So instead of saying "1 More Product" it is saying "4 More Products". How can I edit this to display that text correctly?

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

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

发布评论

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

评论(2

掌心的温暖 2024-12-16 19:42:04

您忘记了 结束标签,我认为这会导致布局破坏。

那么如果您确实需要使用 我建议您首先放置 4 我喜欢使用的同一个类,例如 '' 然后将新类添加到其他类
在 css 文件中创建

添加 tr.toggle{display:none;}

然后在 js 中执行以下操作:

$('a#toggle').live('click',function(){
$('tr.toggle').slideToggle('slow');// you can try also toggle() instead of slideToggle(), i personally preferr slideToggle() ... as you wish :)
});

you forgot about </tr> ending tags i think that is causing layout broking.

then if you really need to use a <table> i suggest you to put at first 4 <tr> a same class i like to use like '<tr class="show">' then add new class to the others <tr class="toggle">,
create a <a id="toggle"></a>

in css file add tr.toggle{display:none;}

then in js do:

$('a#toggle').live('click',function(){
$('tr.toggle').slideToggle('slow');// you can try also toggle() instead of slideToggle(), i personally preferr slideToggle() ... as you wish :)
});
我做我的改变 2024-12-16 19:42:04
function hideProducts(numberToRemain){  
    var rows = $('.v65-productDisplay tr');
    rows.hide();
    for(int i = 0; i < numberToRemain; i++){
        $(rows[i]).show();
    }
}
function showProducts(){
    $('.v65-productDisplay tr').show();
}

然后在文档就绪事件上调用 hideproducts。

function hideProducts(numberToRemain){  
    var rows = $('.v65-productDisplay tr');
    rows.hide();
    for(int i = 0; i < numberToRemain; i++){
        $(rows[i]).show();
    }
}
function showProducts(){
    $('.v65-productDisplay tr').show();
}

Then call hideproducts on the document ready event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文