2016-05-06 3 views
1

私のシナリオ:並べ替えのDataTableのjQueryを除く空白

私はDataTable jQueryプラグインを使用して、テーブルの日付列をソートしようとしていると私はなく、テキストフィールドが白 - 持つテーブルをソートしていますスペース。したがって、私が "ASC"を並べ替えると、空のテキストフィールドが最初に占有されます。これは起こらないはずです。空のテキストボックスを除いてテーブルを並べ替える必要があります。あなたが"customdatesort-pre": function (formElement)を見ることができ、上記のコードで

:私はこのコードを見つけ

jQuery.extend(jQuery.fn.dataTableExt.oSort, { 
"customdatesort-pre": function (formElement) { 
    // returns the "weight" of a cell value 
    var r, x; 
    var a = $(formElement).val(); 

    if (a === null || a === "") { 
     // for empty cells: weight is a "special" value which needs special handling 
     r = false; 
    } else { 
     // otherwise: weight is the "time value" of the date 
     x = a.split("/"); 
     r = +new Date(+x[2], +x[1] - 1, +x[0]); 
    } 
    //console.log("[PRECALC] " + a + " becomes " + r); 
    return r; 
}, 
"customdatesort-asc": function (a, b) { 
    // return values are explained in Array.prototype.sort documentation 
    if (a === false && b === false) { 
     // if both are empty cells then order does not matter 
     return 0; 
    } else if (a === false) { 
     // if a is an empty cell then consider a greater than b 
     return 1; 
    } else if (b === false) { 
     // if b is an empty cell then consider a less than b 
     return -1; 
    } else { 
     // common sense 
     return a - b; 
    } 
}, 
"customdatesort-desc": function (a, b) { 
    if (a === false && b === false) { 
     return 0; 
    } else if (a === false) { 
     return 1; 
    } else if (b === false) { 
     return -1; 
    } else { 
     return b - a; 
    } 
} 
}); 

問題:

は、私は、次のコードを試してみました。パラメータformElementは空の値のみを取りますが、日付値のあるテキストボックスは受け取りません。私は必要なもの

私は空のテキストボックスを除く日付の列をソートする必要があります。

答えて

1

カスタムソートの場合pre obsoletes ascおよびdescpreを定義した場合、ascdescメソッドは決して呼び出されません。 preは最適化機能を意味し、セルごとに1回呼び出され、内部ソーターはソートにその結果を使用します。 this thread on datatables.netを参照してください。

代わりに、ソートリテラルカスタム外pre機能のコードを配置し、ascdesc方法の中からそれを呼び出すことができます。

customdatesortPre = function (formElement) { 
    //customdatesort-pre code 
} 
jQuery.extend(jQuery.fn.dataTableExt.oSort, { 
    "customdatesort-asc": function (a, b) { 
    a = customdatesortPre(a), b = customdatesortPre(b); 
    ... 
    }, 
    "customdatesort-desc": function (a, b) { 
    a = customdatesortPre(a), b = customdatesortPre(b); 
    ... 
    } 
}); 

デモ - これが私の作品>http://jsfiddle.net/9j9gpsrn/

+0

。ありがとう、たくさんの友達。 – Vikash

関連する問題