使用Tablesorter,我正在尝试为dd / mm / yy格式编写日期解析器。它看起来像表格到达解析器,但我的字符串总是未定义。
有什么想法吗?我对tablesorter不熟悉
这是我的代码:
$.tablesorter.addParser(
{
id: "my_date_sorter",
is: function(s) {
return false;
// return /\d{1,4}-\d{1,2}-\d{1,2}/;
},
format: function(s)
{
console.log(s);
if (s)
{
console.log("In myDateSorter s = " + s);
s = s.replace(/\-/g,"/");
s = s.replace(/(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{2})/, "$3/$2/$1");
return $.tablesorter.formatFloat(new Date(s).getTime());
}
else
{
return;
}
},
type: "numeric"
}); // end addParser
$(document).ready(function() {
$( '.tablesorter' ).each( function()
{
// Allow default sort key override by data-sort-key attr per table.
var SortKey = $(this).attr('data-sort-key');
if( SortKey == undefined )
{
SortKey = 3;
}
$(this).tablesorter (
{
dateFormat: 'dd/mm/yy',
headers:
{
2:
{
sorter:'my_date_sorter'
},
6:
{ // don't sort the image column
sorter:false
}
},
sortList: [ [ SortKey, 0 ] ],
textExtraction: function (node)
{
var strippedNodeText = $(node).text().toLowerCase().replace(/\s+/g, "");
if ( ( $(node).index() == 2 ) && ( strippedNodeText == 'tbd' ) )
{
// $(node).parent().addClass('jsnamark');
}
// return $(node).text();
} // end textExtraction
})
.bind('sortEnd', function ()
{
$(this).append( $(this).find('.jsnamark') );
$(this).trigger("update");
});
}); // end $( '.tablesorter' ).each( function()
答案 0 :(得分:0)
问题与添加的textExtraction函数有关。出于某种原因,这导致解析器无法正常工作。
通过自定义解析器,我能够使代码正常工作,同时仍然可以“退出”#34;我的日期列中的错误字符串(例如" TBD")新代码如下:
/* my_date_sorter custom parser that will parse and sort the given column by mm/dd/yy format.
// anything that is not in that format will go to the bottom of the table (by default).
*/
$.tablesorter.addParser(
{
id: "my_date_sorter",
is: function(s)
{
return false;
},
format: function(s)
{
if (s)
{
var dateSplit = s.split('/');
if (3 !== dateSplit.length)
{
return 0;
}
return new Date(dateSplit[2], dateSplit[1], dateSplit[0], 1);
}
else
{
return 0;
}
},
type: "numeric"
}); // end addParser
$(document).ready(function()
{
$( '.tablesorter' ).each( function()
{
// Allow default sort key override by data-sort-key attr per table.
var SortKey = $(this).attr('data-sort-key');
if( SortKey == undefined )
{
SortKey = 3;
}
$(this).tablesorter (
{
dateFormat: 'dd/mm/yy',
headers:
{
2:
{
sorter: 'my_date_sorter' // use custom parser
},
6:
{ // don't sort the image column
sorter: false
}
},
sortList: [ [ SortKey, 0 ] ]
});
}); // end $( '.tablesorter' ).each( function()
});