显示jQuery datatable aLengthMenu作为链接,而不是下拉

时间:2022-05-09 14:23:49

I'm trying to work with the jQuery Datatables plugin. It has everything I want, except there isn't any flexibility with how one shows the aLengthMenu variables. It displays it in a <select> dropdown which is fine, but I have a design that isn't flexible and wants the variables as just links.

我正在尝试使用jQuery Datatables插件。它拥有我想要的所有东西,除了没有任何灵活性,如何显示aLengthMenu变量。它在

This is how it currently displays with:

这是它目前的显示方式:

"aLengthMenu": [[5, 15, 30, 60, -1], [5, 15, 30, 60, "All"]]

显示jQuery datatable aLengthMenu作为链接,而不是下拉

I would love for it to just display links like this so one can just click a link and it would show the amount specified like this:

我希望它只显示像这样的链接这样一个人可以点击一个链接它会显示像这样指定的数量:

显示jQuery datatable aLengthMenu作为链接,而不是下拉

I know I'm messing with the core of the Datatables plugin and more specifically the _fnFeatureHtmlLength function but it would be so awesome if I could get some help on this.

我知道我搞砸了Datatables插件的核心,特别是_fnFeatureHtmlLength函数,但是如果我能得到一些帮助,那就太棒了。

1 个解决方案

#1


3  

There is no easy way to do this without opening up the jquery.dataTables.js file and editing it. Yes, you are correct... you will have to edit the '_fnFeatureHtmlLength' function.

如果不打开jquery.dataTables,要做到这一点并不容易。js文件并进行编辑。是的,你是正确的…您将不得不编辑“_fnFeatureHtmlLength”函数。

I am working with jquery.dataTables.js version 1.9.1 Goto the '_fnFeatureHtmlLength' function (do a search in the file for 'function _fnFeatureHtmlLength(oSettings)' and you should find it, mines on line 2296)

我正在使用jquery.dataTables。js版本1.9.1使用“_fnFeatureHtmlLength”函数(在文件中搜索“function _fnFeatureHtmlLength(oSettings)”),您应该找到它,挖掘到第2296行)

Since you are editing this file I'd make a backup first. Also, comment out the lines you are replacing so you can always refer back to them.

既然你在编辑这个文件,我先做个备份。另外,注释掉要替换的行,这样就可以随时引用它们。

EDITS

编辑

function _fnFeatureHtmlLength(oSettings) {
        if (oSettings.oScroll.bInfinite) {
            return null;
        }

        /* This can be overruled by not using the _MENU_ var/macro in the language variable */
        var sName = 'name="' + oSettings.sTableId + '_length"';
        //var sStdMenu = '<select size="1" '+sName+'>';
        var sStdMenu = '';
        var i, iLen;
        var aLengthMenu = oSettings.aLengthMenu;

        if (aLengthMenu.length == 2 && typeof aLengthMenu[0] === 'object' &&
                typeof aLengthMenu[1] === 'object') {
            for (i = 0, iLen = aLengthMenu[0].length; i < iLen; i++) {
                //sStdMenu += '<option value="' + aLengthMenu[0][i] + '">' + aLengthMenu[1][i] + '</option>';
                sStdMenu += '<a href="#" value="' + aLengthMenu[0][i] + '">' + aLengthMenu[1][i] + '</a>';
            }
        }
        else {
            for (i = 0, iLen = aLengthMenu.length; i < iLen; i++) {
                //sStdMenu += '<option value="' + aLengthMenu[i] + '">' + aLengthMenu[i] + '</option>';
                sStdMenu += '<a href="#" value="' + aLengthMenu[i] + '">' + aLengthMenu[i] + '</a>';
            }
        }
        //sStdMenu += '</select>';

        var nLength = document.createElement('div');
        if (!oSettings.aanFeatures.l) {
            nLength.id = oSettings.sTableId + '_length';
        }
        nLength.className = oSettings.oClasses.sLength;
        nLength.innerHTML = '<label>' + oSettings.oLanguage.sLengthMenu.replace('_MENU_', sStdMenu) + '</label>';

        /*
        * Set the length to the current display length - thanks to Andrea Pavlovic for this fix,
        * and Stefan Skopnik for fixing the fix!
        */
        //$('select option[value="' + oSettings._iDisplayLength + '"]', nLength).attr("selected", true);

        //$('select', nLength).bind('change.DT', function (e) {
        $('a', nLength).bind('click', function (e) {
            //var iVal = $(this).val();
            e.preventDefault();
            var iVal = $(this).attr('value');

            /* Update all other length options for the new display */
            var n = oSettings.aanFeatures.l;
            for (i = 0, iLen = n.length; i < iLen; i++) {
                if (n[i] != this.parentNode) {
                    //$('select', n[i]).val(iVal);
                }
            }

            /* Redraw the table */
            oSettings._iDisplayLength = parseInt(iVal, 10);
            _fnCalculateEnd(oSettings);

            /* If we have space to show extra rows (backing up from the end point - then do so */
            if (oSettings.fnDisplayEnd() == oSettings.fnRecordsDisplay()) {
                oSettings._iDisplayStart = oSettings.fnDisplayEnd() - oSettings._iDisplayLength;
                if (oSettings._iDisplayStart < 0) {
                    oSettings._iDisplayStart = 0;
                }
            }

            if (oSettings._iDisplayLength == -1) {
                oSettings._iDisplayStart = 0;
            }

            _fnDraw(oSettings);
        });


        //$('select', nLength).attr('aria-controls', oSettings.sTableId);

        return nLength;
    }

Also I made changes to the datatables.css file:

我还对datatables进行了更改。css文件:

.dataTables_length a{
margin-right:6px;
}

But you can style it however. Also in the click event in the function I edited you maybe want to add a 'active' class to the selected 'a' tag and style however you want. Just remember to remove the active class from all other 'a'.

但是你可以给它设计样式。同样,在我编辑的函数中的click事件中,您可能希望向所选的“a”标记和样式添加一个“active”类。只需记住从所有其他的“a”中删除活动类。

I have not fully tested these changes, and since I did not write datatables I don't know if these changes will have any side effects. Also I did not test with any plug-ins... so use at your own risk!

我还没有完全测试这些更改,因为我没有编写datatables,我不知道这些更改是否会产生任何副作用。我也没有测试任何插件…所以,你可以自作自受!

#1


3  

There is no easy way to do this without opening up the jquery.dataTables.js file and editing it. Yes, you are correct... you will have to edit the '_fnFeatureHtmlLength' function.

如果不打开jquery.dataTables,要做到这一点并不容易。js文件并进行编辑。是的,你是正确的…您将不得不编辑“_fnFeatureHtmlLength”函数。

I am working with jquery.dataTables.js version 1.9.1 Goto the '_fnFeatureHtmlLength' function (do a search in the file for 'function _fnFeatureHtmlLength(oSettings)' and you should find it, mines on line 2296)

我正在使用jquery.dataTables。js版本1.9.1使用“_fnFeatureHtmlLength”函数(在文件中搜索“function _fnFeatureHtmlLength(oSettings)”),您应该找到它,挖掘到第2296行)

Since you are editing this file I'd make a backup first. Also, comment out the lines you are replacing so you can always refer back to them.

既然你在编辑这个文件,我先做个备份。另外,注释掉要替换的行,这样就可以随时引用它们。

EDITS

编辑

function _fnFeatureHtmlLength(oSettings) {
        if (oSettings.oScroll.bInfinite) {
            return null;
        }

        /* This can be overruled by not using the _MENU_ var/macro in the language variable */
        var sName = 'name="' + oSettings.sTableId + '_length"';
        //var sStdMenu = '<select size="1" '+sName+'>';
        var sStdMenu = '';
        var i, iLen;
        var aLengthMenu = oSettings.aLengthMenu;

        if (aLengthMenu.length == 2 && typeof aLengthMenu[0] === 'object' &&
                typeof aLengthMenu[1] === 'object') {
            for (i = 0, iLen = aLengthMenu[0].length; i < iLen; i++) {
                //sStdMenu += '<option value="' + aLengthMenu[0][i] + '">' + aLengthMenu[1][i] + '</option>';
                sStdMenu += '<a href="#" value="' + aLengthMenu[0][i] + '">' + aLengthMenu[1][i] + '</a>';
            }
        }
        else {
            for (i = 0, iLen = aLengthMenu.length; i < iLen; i++) {
                //sStdMenu += '<option value="' + aLengthMenu[i] + '">' + aLengthMenu[i] + '</option>';
                sStdMenu += '<a href="#" value="' + aLengthMenu[i] + '">' + aLengthMenu[i] + '</a>';
            }
        }
        //sStdMenu += '</select>';

        var nLength = document.createElement('div');
        if (!oSettings.aanFeatures.l) {
            nLength.id = oSettings.sTableId + '_length';
        }
        nLength.className = oSettings.oClasses.sLength;
        nLength.innerHTML = '<label>' + oSettings.oLanguage.sLengthMenu.replace('_MENU_', sStdMenu) + '</label>';

        /*
        * Set the length to the current display length - thanks to Andrea Pavlovic for this fix,
        * and Stefan Skopnik for fixing the fix!
        */
        //$('select option[value="' + oSettings._iDisplayLength + '"]', nLength).attr("selected", true);

        //$('select', nLength).bind('change.DT', function (e) {
        $('a', nLength).bind('click', function (e) {
            //var iVal = $(this).val();
            e.preventDefault();
            var iVal = $(this).attr('value');

            /* Update all other length options for the new display */
            var n = oSettings.aanFeatures.l;
            for (i = 0, iLen = n.length; i < iLen; i++) {
                if (n[i] != this.parentNode) {
                    //$('select', n[i]).val(iVal);
                }
            }

            /* Redraw the table */
            oSettings._iDisplayLength = parseInt(iVal, 10);
            _fnCalculateEnd(oSettings);

            /* If we have space to show extra rows (backing up from the end point - then do so */
            if (oSettings.fnDisplayEnd() == oSettings.fnRecordsDisplay()) {
                oSettings._iDisplayStart = oSettings.fnDisplayEnd() - oSettings._iDisplayLength;
                if (oSettings._iDisplayStart < 0) {
                    oSettings._iDisplayStart = 0;
                }
            }

            if (oSettings._iDisplayLength == -1) {
                oSettings._iDisplayStart = 0;
            }

            _fnDraw(oSettings);
        });


        //$('select', nLength).attr('aria-controls', oSettings.sTableId);

        return nLength;
    }

Also I made changes to the datatables.css file:

我还对datatables进行了更改。css文件:

.dataTables_length a{
margin-right:6px;
}

But you can style it however. Also in the click event in the function I edited you maybe want to add a 'active' class to the selected 'a' tag and style however you want. Just remember to remove the active class from all other 'a'.

但是你可以给它设计样式。同样,在我编辑的函数中的click事件中,您可能希望向所选的“a”标记和样式添加一个“active”类。只需记住从所有其他的“a”中删除活动类。

I have not fully tested these changes, and since I did not write datatables I don't know if these changes will have any side effects. Also I did not test with any plug-ins... so use at your own risk!

我还没有完全测试这些更改,因为我没有编写datatables,我不知道这些更改是否会产生任何副作用。我也没有测试任何插件…所以,你可以自作自受!