2

如何在 jQuery Datatables 中添加对数字逗号排序的默认支持?

...无需使用 sType 指定列。

4

2 回答 2

2

步骤1:

使用以下代码扩展 dataTableExt.oSort:

$.extend($.fn.dataTableExt.oSort, {
  "numeric-pre": function(a) {
    a = (a === "-" || a === "") ? 0 : a.replace(/[^\d\-\.]/g, "");
    return parseFloat(a);
  },
  "numeric-asc": function(a, b) {
    return a - b;
  },
  "numeric-desc": function(a, b) {
    return b - a;
  }
});

第 2 步: 扩展 dataTableExt.aTypes,在 sValidChars 变量中添加逗号:

 # Added comma to sValidChars
 $.extend $.fn.dataTableExt.aTypes, [
        /*
         * Function: -
         * Purpose:  Check to see if a string is numeric
         * Returns:  string:'numeric' or null
         * Inputs:   mixed:sText - string to check
         */
        function (sData) {
            /* Allow zero length strings as a number */
            if (typeof sData === 'number') {
                return 'numeric';
            }
            else if (typeof sData !== 'string') {
                return null;
            }

            var sValidFirstChars = "0123456789-";
            var sValidChars = "0123456789.,";
            var Char;
            var bDecimal = false;

            /* Check for a valid first char (no period and allow negatives) */
            Char = sData.charAt(0);
            if (sValidFirstChars.indexOf(Char) == -1) {
                return null;
            }

            /* Check all the other characters are valid */
            for (var i = 1; i < sData.length; i++) {
                Char = sData.charAt(i);
                if (sValidChars.indexOf(Char) == -1) {
                    return null;
                }

                /* Only allowed one decimal place... */
                if (Char == ".") {
                    if (bDecimal) {
                        return null;
                    }
                    bDecimal = true;
                }
            }

            return 'numeric';
        },

        /*
         * Function: -
         * Purpose:  Check to see if a string is actually a formatted date
         * Returns:  string:'date' or null
         * Inputs:   string:sText - string to check
         */
        function (sData) {
            var iParse = Date.parse(sData);
            if ((iParse !== null && !isNaN(iParse)) || (typeof sData === 'string' && sData.length === 0)) {
                return 'date';
            }
            return null;
        },

        /*
         * Function: -
         * Purpose:  Check to see if a string should be treated as an HTML string
         * Returns:  string:'html' or null
         * Inputs:   string:sText - string to check
         */
        function (sData) {
            if (typeof sData === 'string' && sData.indexOf('<') != -1 && sData.indexOf('>') != -1) {
                return 'html';
            }
            return null;
        }
    ]);
于 2013-04-21T09:41:03.833 回答
0

最新的dataTables版本(1.10.4)处理格式化数字排序,没有任何扩展名,即降序排序时puts $1,000.00before ,升序排序时puts before 。$900.00$900.00$1,000.00

请参阅http://datatables.net/reference/option/columns.typenum-fmt

于 2014-12-13T20:54:21.077 回答