【问题标题】:Modify existing jQuery table sort+filter functions (child rows)修改现有的jQuery表格排序+过滤功能(子行)
【发布时间】:2016-04-25 20:01:59
【问题描述】:

好的,所以我有这个 jQuery 表函数 (https://jsfiddle.net/51Le6o06/7/),它可以完美地与同一页面上的多个表一起使用。

我想修改表格和函数(新小提琴https://jsfiddle.net/51Le6o06/16/),以便每一行(.product-data-row)携带一个信息行(.product-information-row)[子行],如您所见在这个小提琴中。

<tr class="product-data-row">
              <td>data</td>
              <td>data</td>
              <td>data</td>
              <td><span class="handsetcost">£364.00 upfront</span>
                  <br><span class="amount">£10.10 per month</span></td>
          </tr>
          <tr class="product-information-row">
              <td colspan="100%">£364.00 upfront<br>£10.10 per month</td>
          </tr>

现在,每个 (.product-data-row) 在其下方都有一个 (.product-information-row),其中包含有关 (.product-data-row) 的信息,目前在上面的示例中,它只包含重复的信息。

从早期的表函数继承现有的 jQuery 函数,页面加载函数应该 - 通过搜索类 (.amount) 在页面加载时对表行进行排序,然后按升序重新排列行,以便最便宜的交易(每月)是第一位的。哪个不再起作用了。

还有一个过滤器通过仅显示具有类 (.free) 的行来过滤表行(从上一个表结转) - 这也是错误的,因为我希望信息行与包含 ( .free)。

每个 (.product-information-row) 都特定于其正上方的 (.product-data-row)。我如何修改 jQuery 以适应这一点。并使表格的功能与它的前身一样,使用额外的信息行。

我在这里要做的是隐藏表格行的数据,最终将在单击表格行时显示并最终向下滑动,但我似乎无法让它像 DataTables 的子行一样工作用途,如果有人有任何替代方法或想法,请在 cmets 中告诉我。

前身的 jQuery(需要修改才能工作)

    jQuery.fn.extend({
        sortPaging: function() {
            return this.each(function() {
                var container = $(this);
                var dataRows = [];

                /**
                 * Create an array of all rows with its value (this assumes that the amount is always a number.
                 * You should add error checking!!
                 * Also assumes that all rows are data rows, and that there are no header rows. Adjust selector appropriately.
                 */
                container.find('.internalActivities > tbody > tr').each(function(i, j) {
                    dataRows.push({
                        'amount': parseFloat($(this).find('.amount').text().replace(/£/, "")),
                        'row': $(this)
                    });
                })

                //Sort the data smallest to largest
                dataRows.sort(function(a, b) {
                    return a.amount - b.amount;
                });

                //Remove existing table rows.  This assumes that everything should be deleted, adjust selector if needed :).
                container.find('.internalActivities > tbody').empty();

                //Add rows back to table in the correct order.
                dataRows.forEach(function(ele) {
                    container.find('.internalActivities > tbody').append(ele.row);
                })


                var trs = container.find(".internalActivities tbody tr");
                var btnMore = container.find(".seeMoreRecords");
                var btnLess = container.find(".seeLessRecords");
                var trsLength = trs.length;
                var currentIndex = 4,
                    page = 4;

                trs.hide();
                trs.slice(0, currentIndex).show();
                checkButton();

                btnMore.click(function(e) {
                    e.preventDefault();
                    trs.slice(currentIndex, currentIndex + page).show();
                    currentIndex += page;
                    checkButton();
                });

                btnLess.click(function(e) {
                    e.preventDefault();
                    trs.slice(currentIndex - page, currentIndex).hide();
                    currentIndex -= page;
                    checkButton();
                });

                function checkButton() {
                    var currentLength = trs.filter("tr:visible").length;

                    if (currentLength >= trsLength) {
                        btnMore.hide();
                    } else {
                        btnMore.show();
                    }

                    if (trsLength > page && currentLength > page) {
                        btnLess.show();
                    } else {
                        btnLess.hide();
                    }

                }
                container.find('.filter-free').change(function() {
                    var $all = container.find(".internalActivities tbody tr").hide();
                    trs = this.checked ? $all.has('.free') : $all;
                    trsLength = trs.length;
                    trs.slice(0, page).show();
                    currentIndex = page;
                });
                container.find('.filter-free').click(function() {
                    container.find('.seeLessRecords').hide();
                });
            })
        }
    });

    $('.sort_paging').sortPaging();

HTML

    <h1>Table sorting on page load with paging</h1>

    <div class="sort_paging">

        <p>
            <input type="checkbox" class="filter-free" /> Free Handset
        </p>

        <table class="internalActivities">
            <thead>
                <tr>
                    <th>head1</th>
                    <th>head2</th>
                    <th>head3</th>
                    <th>head4</th>
                </tr>
            </thead>
            <tbody>

                  <tr class="product-data-row">
                      <td>data</td>
                      <td>data</td>
                      <td>data</td>
                      <td><span class="handsetcost">£364.00 upfront</span>
                          <br><span class="amount">£10.10 per month</span></td>
                  </tr>
                  <tr class="product-information-row">
                      <td colspan="100%">£364.00 upfront<br>£10.10 per month</td>
                  </tr>

                  <tr class="product-data-row">
                      <td>data</td>
                      <td>data</td>
                      <td>data</td>
                      <td><span class="free">No upfront cost</span>
                          <br><span class="amount">£40.40 per month</span></td>
                  </tr>
                  <tr class="product-information-row">
                      <td colspan="100%">No upfront cost
                          <br>£40.40 per month</td>
                  </tr>

                  <tr class="product-data-row">
                      <td>data</td>
                      <td>data</td>
                      <td>data</td>
                      <td><span class="free">No upfront cost</span>
                          <br><span class="amount">£30.30 per month</span></td>
                  </tr>
                  <tr class="product-information-row">
                      <td colspan="100%">No upfront cost
                          <br>£30.30 per month</td>
                  </tr>

                  <tr class="product-data-row">
                      <td>data</td>
                      <td>data</td>
                      <td>data</td>
                      <td><span class="free">No upfront cost</span>
                          <br><span class="amount">£16.04 per month</span></td>
                  </tr>
                  <tr class="product-information-row">
                      <td colspan="100%">No upfront cost
                          <br>£16.04 per month</td>
                  </tr>

                  <tr class="product-data-row">
                      <td>data</td>
                      <td>data</td>
                      <td>data</td>
                      <td><span class="handsetcost">£134.00 upfront</span>
                          <br><span class="amount">£12.19 per month</span></td>
                  </tr>
                  <tr class="product-information-row">
                      <td colspan="100%">£134.00 upfront
                          <br>£12.19 per month</td>
                  </tr>

                  <tr class="product-data-row">
                      <td>data</td>
                      <td>data</td>
                      <td>data</td>
                      <td><span class="handsetcost">£120.00 upfront</span>
                          <br><span class="amount">£14.22 per month</span></td>
                  </tr>
                  <tr class="product-information-row">
                      <td colspan="100%">£120.00 upfront
                          <br>£14.22 per month</td>
                  </tr>

                  <tr class="product-data-row">
                      <td>data</td>
                      <td>data</td>
                      <td>data</td>
                      <td><span class="free">No upfront cost</span>
                          <br><span class="amount">£50.22 per month</span></td>
                  </tr>
                  <tr class="product-information-row">
                      <td colspan="100%">No upfront cost
                          <br>£50.22 per month</td>
                  </tr>

                  <tr class="product-data-row">
                      <td>data</td>
                      <td>data</td>
                      <td>data</td>
                      <td><span class="free">No upfront cost</span>
                          <br><span class="amount">£10.33 per month</span></td>
                  </tr>
                  <tr class="product-information-row">
                      <td colspan="100%">No upfront cost
                          <br>£10.33 per month</td>
                  </tr>

                  <tr class="product-data-row">
                      <td>data</td>
                      <td>data</td>
                      <td>data</td>
                      <td><span class="free">No upfront cost</span>
                          <br><span class="amount">£40.45 per month</span></td>
                  </tr>
                  <tr class="product-information-row">
                      <td colspan="100%">No upfront cost
                          <br>£40.45 per month</td>
                  </tr>

                  <tr class="product-data-row">
                      <td>data</td>
                      <td>data</td>
                      <td>data</td>
                      <td><span class="handsetcost">£200.00 upfront</span>
                          <br><span class="amount">£30.84 per month</span></td>
                  </tr>
                  <tr class="product-information-row">
                      <td colspan="100%">£200.00 upfront
                          <br>£30.84 per month</td>
                  </tr>

                  <tr class="product-data-row">
                      <td>data</td>
                      <td>data</td>
                      <td>data</td>
                      <td><span class="free">No upfront cost</span>
                          <br><span class="amount">£16.14 per month</span></td>
                  </tr>
                  <tr class="product-information-row">
                      <td colspan="100%">No upfront cost
                          <br>£16.14 per month</td>
                  </tr>

                  <tr class="product-data-row">
                      <td>data</td>
                      <td>data</td>
                      <td>data</td>
                      <td><span class="free">No upfront cost</span>
                          <br><span class="amount">£12.10 per month</span></td>
                  </tr>
                  <tr class="product-information-row">
                      <td colspan="100%">No upfront cost
                          <br>£12.10 per month</td>
                  </tr>

                  <tr class="product-data-row">
                      <td>data</td>
                      <td>data</td>
                      <td>data</td>
                      <td><span class="free">No upfront cost</span>
                          <br><span class="amount">£14.02 per month</span></td>
                  </tr>
                  <tr class="product-information-row">
                      <td colspan="100%">No upfront cost
                          <br>£14.02 per month</td>
                  </tr>

                  <tr class="product-data-row">
                      <td>data</td>
                      <td>data</td>
                      <td>data</td>
                      <td><span class="free">No upfront cost</span>
                          <br><span class="amount">£50.88 per month</span></td>
                  </tr>
                  <tr class="product-information-row">
                      <td colspan="100%">No upfront cost
                          <br>£50.88 per month</td>
                  </tr>
            </tbody>
        </table>

        <input type="button" class="seeMoreRecords" value="More">
        <input type="button" class="seeLessRecords" value="Less">

    </div>

    <h2>Second table below</h2>

    <div class="sort_paging">

        <p>
            <input type="checkbox" class="filter-free" /> Free Handset
        </p>

        <table class="internalActivities">
            <thead>
                <tr>
                    <th>head1</th>
                    <th>head2</th>
                    <th>head3</th>
                    <th>head4</th>
                </tr>
            </thead>
            <tbody>

                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="handsetcost">£364.00 upfront</span>
                        <br><span class="amount">£10.10 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">£364.00 upfront<br>£10.10 per month</td>
                </tr>

                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£40.40 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£40.40 per month</td>
                </tr>

                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£30.30 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£30.30 per month</td>
                </tr>

                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£16.04 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£16.04 per month</td>
                </tr>

                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="handsetcost">£134.00 upfront</span>
                        <br><span class="amount">£12.19 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">£134.00 upfront
                        <br>£12.19 per month</td>
                </tr>

                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="handsetcost">£120.00 upfront</span>
                        <br><span class="amount">£14.22 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">£120.00 upfront
                        <br>£14.22 per month</td>
                </tr>

                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£50.22 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£50.22 per month</td>
                </tr>

                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£10.33 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£10.33 per month</td>
                </tr>

                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£40.45 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£40.45 per month</td>
                </tr>

                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="handsetcost">£200.00 upfront</span>
                        <br><span class="amount">£30.84 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">£200.00 upfront
                        <br>£30.84 per month</td>
                </tr>

                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£16.14 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£16.14 per month</td>
                </tr>

                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£12.10 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£12.10 per month</td>
                </tr>

                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£14.02 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£14.02 per month</td>
                </tr>

                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£50.88 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£50.88 per month</td>
                </tr>

            </tbody>
        </table>

        <input type="button" class="seeMoreRecords" value="More">
        <input type="button" class="seeLessRecords" value="Less">

    </div>

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    您的代码中有很多未解决的问题,因此快速修复需要一个多小时。试着看一遍下面的代码,希望你能得到一些想法(下次请尽量把问题做的更准确,可能会分成几个问题,以便得到快速简洁的答案):

    jQuery.fn.sortPaging = function(options) {
        var defaults = {
            pageRows: 2
        };
        var settings = $.extend(true, defaults, options);
        return this.each(function() {
            
    		var container = $(this);
    		var tableBody = container.find('.internalActivities > tbody');
    		var dataRows = [];
    		var currentPage = 1;
    		var maxPages = 1;
    		var buttonMore = container.find('.seeMoreRecords');
    		var buttonLess = container.find('.seeLessRecords');
    		var buttonFree = container.find('.filter-free');
    		var tableRows = [];
    		var maxFree = 0;
    		var filterFree = buttonFree.is(':checked');
    		function displayRows() {
    			tableBody.empty();
    			var displayed = 0;
    			$.each(dataRows, function(i, ele) {
    				if( !filterFree || (filterFree && ele.isFree) ) {
    					tableBody.append(ele.thisRow).append(ele.nextRow);
    					displayed++;
    					if( displayed >= currentPage*settings.pageRows ) {
    						return false;
    					};
    				};
    			});
    			tableBody.find('.product-data-row').on('click', function(e){
    				$(this).next().toggle();
    			});
    		};
    		function checkButtons() {
    			buttonLess.toggleClass('element_invisible', currentPage<=1);
    			buttonMore.toggleClass('element_invisible', filterFree ? currentPage>=maxFreePages : currentPage>=maxPages);
    		};
    		function showMore() {
    			currentPage++;
    			displayRows();
    			checkButtons();
    		};
    		function showLess() {
    			currentPage--;
    			displayRows();
    			checkButtons();
    		};
    		function changedFree() {
    			filterFree = buttonFree.is(':checked');
    			if( filterFree && currentPage>maxFreePages ) {
    				currentPage=maxFreePages;
    			};
    			displayRows();
    			checkButtons();
    		};
    		
            tableBody.find('.product-data-row').each(function(i, j) {
                var thisRow = $(this);
                var nextRow = thisRow.next();
    			var amount = parseFloat(thisRow.find('.amount').text().replace(/£/, ''));
    			var isFree = thisRow.find('.free').length;
    			maxFree += isFree;
                dataRows.push({
                    amount: amount,
                    thisRow: thisRow,
                    nextRow: nextRow,
    				isFree: isFree
                });
            })
    
            dataRows.sort(function(a, b) {
                return a.amount - b.amount;
            });
    		maxPages = Math.ceil(dataRows.length/settings.pageRows);
    		maxFreePages = Math.ceil(maxFree/settings.pageRows);
    
            tableRows = tableBody.find("tr");
    
            buttonMore.on('click', showMore);
    		buttonLess.on('click', showLess);
            buttonFree.on('change', changedFree);
    		
    		displayRows();
    		checkButtons();
            
        })
    
    };
    
    $('.sort_paging').sortPaging();
    table,
    tr,
    td {
        border: 1px solid black;
        padding: 8px;
    }
    
    h1 {
        font-size: 18px;
    }
    .element_invisible {
    	display: none;
    }
    .product-information-row {
    	display: none;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <h1>Table sorting on page load with paging</h1>
    
    <div class="sort_paging">
    
        <p>
            <input type="checkbox" class="filter-free" /> Free Handset
        </p>
    
        <table class="internalActivities">
            <thead>
                <tr>
                    <th>head1</th>
                    <th>head2</th>
                    <th>head3</th>
                    <th>head4</th>
                </tr>
            </thead>
            <tbody>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="handsetcost">£364.00 upfront</span>
                        <br><span class="amount">£10.10 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">£364.00 upfront
                        <br>£10.10 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£40.40 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£40.40 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£30.30 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£30.30 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£16.04 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£16.04 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="handsetcost">£134.00 upfront</span>
                        <br><span class="amount">£12.19 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">£134.00 upfront
                        <br>£12.19 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="handsetcost">£120.00 upfront</span>
                        <br><span class="amount">£14.22 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">£120.00 upfront
                        <br>£14.22 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£50.22 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£50.22 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£10.33 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£10.33 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£40.45 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£40.45 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="handsetcost">£200.00 upfront</span>
                        <br><span class="amount">£30.84 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">£200.00 upfront
                        <br>£30.84 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£16.14 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£16.14 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£12.10 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£12.10 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£14.02 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£14.02 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£50.88 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£50.88 per month</td>
                </tr>
            </tbody>
        </table>
    
        <input type="button" class="seeMoreRecords" value="More">
        <input type="button" class="seeLessRecords" value="Less">
    
    </div>
    
    <h2>Second table below</h2>
    
    <div class="sort_paging">
    
        <p>
            <input type="checkbox" class="filter-free" /> Free Handset
        </p>
    
        <table class="internalActivities">
            <thead>
                <tr>
                    <th>head1</th>
                    <th>head2</th>
                    <th>head3</th>
                    <th>head4</th>
                </tr>
            </thead>
            <tbody>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="handsetcost">£364.00 upfront</span>
                        <br><span class="amount">£10.10 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">£364.00 upfront
                        <br>£10.10 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£40.40 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£40.40 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£30.30 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£30.30 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£16.04 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£16.04 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="handsetcost">£134.00 upfront</span>
                        <br><span class="amount">£12.19 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">£134.00 upfront
                        <br>£12.19 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="handsetcost">£120.00 upfront</span>
                        <br><span class="amount">£14.22 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">£120.00 upfront
                        <br>£14.22 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£50.22 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£50.22 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£10.33 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£10.33 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£40.45 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£40.45 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="handsetcost">£200.00 upfront</span>
                        <br><span class="amount">£30.84 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">£200.00 upfront
                        <br>£30.84 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£16.14 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£16.14 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£12.10 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£12.10 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£14.02 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£14.02 per month</td>
                </tr>
    
                <tr class="product-data-row">
                    <td>data</td>
                    <td>data</td>
                    <td>data</td>
                    <td><span class="free">No upfront cost</span>
                        <br><span class="amount">£50.88 per month</span></td>
                </tr>
                <tr class="product-information-row">
                    <td colspan="100%">No upfront cost
                        <br>£50.88 per month</td>
                </tr>
    
            </tbody>
        </table>
    
        <input type="button" class="seeMoreRecords" value="More">
        <input type="button" class="seeLessRecords" value="Less">
    
    </div>

    也在updated Fiddle;

    【讨论】:

    • 太棒了,我自己确实想出了一个解决方案,但你的解决方案要好得多 - 谢谢,在这个主题上我还有一个问题,因为你已经编写了这个答案,我想你会知道最好的方法实现它,是否可以在每个表的免费手机复选框过滤器旁边添加第二个过滤器?仅在过滤器可用时显示(或者如果存在要过滤的数据)
    • (右侧)我想要一个下拉框过滤器,它根据产品信息行中的数据过滤行,以过滤哪些交易包含免费礼物,例如,如果孩子表一的第 2、5、8 行包含免费礼物“免费电视”&lt;p class="information"&gt;Free TV&lt;/p&gt; 和子行 1 包含“25 英镑现金返还&lt;p class="information"&gt;£25 cashback&lt;/p&gt; 是否有下拉框预先填充此数据,然后可用于按它过滤表?
    • 您开始构建这些表的方式与我不同。由于您可以实现一个过滤器,因此猜测您可以再实现 10 个,但您应该在创建代码之前考虑这一点。因此,在编写任何代码之前,您应该将您需要的东西写在纸上,而不是想最简单的方法来创建和管理您的小部件。另外,一件重要的事情是:不要发明已经发明的东西 - 就您而言,由于您使用表格数据,因此请检查现有插件 datatablesothers
    • 我只用 jQuery 搞砸了几个星期,我曾考虑过使用数据表,但我需要使用现有的 HTML,我找不到任何像我的表格那样分页的例子数据表,因为这仅适用于我网站的移动端,所以我认为定制它会更容易
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 2015-03-22
    • 2015-02-26
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    相关资源
    最近更新 更多