简体   繁体   English

如何使用 jQuery jScroll 滚动表格行?

[英]How to scroll table rows with jQuery jScroll?

There is not many useful examples with jQuery jscroll and it is a fact it doesn't work with table rows by default because it puts the results into a div element which screws up the table. jQuery jscroll的有用示例并不多,事实上它默认情况下不适用于表格行,因为它将结果放入一个 div 元素中,这会破坏表格。

I had this HTML template我有这个 HTML 模板

<table class="table responsive-table-list">
    <thead>
        <tr>
            <th>label 1</th>
            <th>label 2</th>
            <th>label 3</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>data 1.1</td>
            <td>data 1.2</td>
            <td>data 1.3</td>
        </tr>
        <tr>
            <td>data 2.1</td>
            <td>data 2.2</td>
            <td>data 2.3</td>
        </tr>
    <tbody>
</table>

How can I make it jScrollable?我怎样才能让它jScrollable?

You need your own controller which provides the data.您需要自己的 controller 来提供数据。 This is not part of this solution.这不是此解决方案的一部分。 The output in this case has to be HTML and has to contain the next X rows of the table.在这种情况下,output 必须是 HTML 并且必须包含表的下 X 行。

The HTML template wrapped with an extra div. HTML 模板用一个额外的 div 包裹。 The next page link (pointing to the controller) is added as a new row.下一页链接(指向控制器)被添加为新行。 jscroll will remove the A and the TD tags but will not remove the TR as it is not aware of it. jscroll将删除ATD标签,但不会删除TR ,因为它不知道它。 We have to do it later otherwise the empty TR tags break the HTML .我们必须稍后再做,否则空的TR标签会破坏HTML I pass the auto-trigger and the loading-html in the data argument which I can access from the javascript code.我在data参数中传递了auto-triggerloading-html ,我可以从 javascript 代码访问它们。 I use nextPageLoad class in the A tag which helps me to recognize the jscroll related link.我在A标签中使用nextPageLoad class 可以帮助我识别与jscroll相关的链接。

<div class="jscroll" data-auto-trigger="true" data-loading-html="Loading..">
    <table class="table responsive-table-list">
        <thead>
        <tr>
            <th>label 1</th>
            <th>label 2</th>
            <th>label 3</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>data 1.1</td>
            <td>data 1.2</td>
            <td>data 1.3</td>
        </tr>
        <tr>
            <td>data 2.1</td>
            <td>data 2.2</td>
            <td>data 2.3</td>
        </tr>
        <tr>
            <td colspan="3">
                <a href="/link-to-action-for-the-next-rows" class="nextPageLoad">Next page</a>
            </td>
        </tr>
        <tbody>
    </table>
</div>

the javascript code. javascript 代码。 The debug is activated!调试已激活!

var EndlessScroll = (function ($) {

    function init() {
        var $scrollEl = $('.jscroll');
        var autoTrigger = $scrollEl.data('auto-trigger');
        var loadingHtml = $('<span>');
        loadingHtml.text($scrollEl.data('loading-html'));

        $scrollEl.jscroll({
            loadingHtml: loadingHtml[0].outerHTML,
            padding: 20,
            autoTrigger: autoTrigger,
            nextSelector: '.nextPageLoad',
            callback: function (data) {
                var $table = $('.jscroll table tbody');

                // moves the new elements from the jscroll div to the table
                $('.jscroll-added tr').appendTo($table);

                // removes the empty tr encapsulated the jscroll pagination
                var rows = $table.find('tr');
                rows.each(function(idx, el){
                    if ($(el).children().length === 0) {
                        $(el).remove();
                    }
                });
            },
            debug: true
        });
    }


    return {
        init: init
    };
})(jQuery);

$(EndlessScroll.init);

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

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