如何在bootstrap表中对文件大小进行排序?

时间:2018-02-12 17:54:23

标签: javascript jquery bootstrap-table

我已经构建了这个网格:

enter image description here

到目前为止一切顺利,但是当我将文件大小标记为可排序列时,它并没有很好地排序。如何使文件大小列可排序?

感谢。

2 个答案:

答案 0 :(得分:0)

您的数据与列的格式有问题吗? Here is a JSFIDDLE example已修改from the bootstrap-table examples,其中显示了可排序数据的装饰。请注意,数据没有“MB” - 这会使数据与数据的显示分开。数据格式化程序装饰允许您指定打扮数据的功能(或创建按钮或其他HTML)。

$(function(){         $( '#表')。bootstrapTable({             数据:数据         });     });

function formatMegaBytes(value,row,index){
return value + ' MB';

}

enter image description here

答案 1 :(得分:0)

我设法手工完成:

HTML:

 <table id="table" class="table borderless">
                    <thead>
                        <tr>
                            <th data-field="Type" data-sortable="false" data-formatter="typeFormatter" data-cell-style="typeStyle" class="text-center">Type</th>
                            <th data-field="Content Type" data-sortable="true">Content Type</th>
                            <th data-field="Name" data-sortable="true">Name</th>
                            <th data-field="Received" data-sortable="true">Received</th>
                            <th data-field="File Size" data-sortable="true" data-sorter="filesizeSorter">File Size</th>
                            <th data-field="" data-formatter="operateFormatter" data-events="operateEvents"></th>
                        </tr>
                    </thead>
                </table>

Javascript:

 //filesize column custom formatter
    function filesizeSorter(a, b) {

        var a_number = retnum(a);
        var b_number = retnum(b);

        a = a_number;
        b = b_number; 
        if (a > b) return 1;
        if (a < b) return -1;
        return 0;
    }

    //return bytes of filesize
    function retnum(number) {
        var num = number.replace(/[^0-9]/g, '');
        var filesizename = number.replace(/[^a-zA-Z]+/g, '').toUpperCase();

        num = parseInt(num, 10);

        switch (filesizename) {
            case "KB":
                num = num * 1024;
                break;
            case "MB":
                num = num * Math.pow(1024, 2);
                break;
            case "GB":
                num = num * Math.pow(1024, 3);
                break;
            case "TB":
                num = num * Math.pow(1024, 4);
                break;
        }

        return num;
    }