【问题标题】:How to define a custom date parser for tablesorter jquery plugin?如何为 tablesorter jquery 插件定义自定义日期解析器?
【发布时间】:2014-06-22 15:37:13
【问题描述】:

我尝试为 jquery 插件定义一个自定义日期解析器。 这是我的桌子:

我尝试在我的 jQuery 中定义一个自定义日期解析器,如下所示:

<script type="text/javascript" src="{% static 'js/jquery.tablesorter.min.js' %}"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('.tablesorter').tablesorter();

         // add parser through the tablesorter addParser method 
        $.tablesorter.addParser({
            id: "customDate",
            is: function(s) {
                return /[0-9]{1,2} [a-zA-Z]+, [0-9]{1,2}:[0-9]{1,2}/.test(s);
                //return false
            },
            format: function(s) {
                s = s.replace(/,/,"").replace(/:/," ");
                s = s.replace(/January/,1).replace(/February/,2).replace(/March/,3).replace(/April/,4).replace(/May/,5).replace(/June/,6).replace(/Jully/,7).replace(/August/,8).replace(/September/,9).replace(/October/,10).replace(/November/,11).replace(/December/,12);
                s = s.split(" ");
                s = s[1]*1000000+s[0]*10000+s[2]*100+s[3]
                return s
            },
            type: "numeric"
        });
    });

</script>

但它不起作用。看起来 customDate 解析器根本没有被调用。 日期字段仅按第一个数字(日期的日期)排序。

这是我第一次定义自定义解析器。我已经阅读了这些主题:

http://tablesorter.com/docs/example-parsers.html

date Sorting Problem with Jquery Tablesorter

【问题讨论】:

    标签: jquery python django django-templates tablesorter


    【解决方案1】:

    我修好了。 我的错误是在$(document).ready(function() 函数中使用了$.tablesorter.addParser...

    我就是这样做的:

            $.tablesorter.addParser({
            id: "customDate",
            is: function(s) {
                return /^[0-9]{1,2} [a-zA-Z]+, [0-9]{1,2}:[0-9]{1,2}$/.test(s);
            },
            format: function(s) {
                s = s.replace(/,/,"").replace(/:/," ");
                s = s.replace(/January/,1).replace(/February/,2).replace(/March/,3).replace(/April/,4).replace(/May/,5).replace(/June/,6).replace(/Jully/,7).replace(/August/,8).replace(/September/,9).replace(/October/,10).replace(/November/,11).replace(/December/,12);
                s = s.split(" ");
                s = s[1]*1000000+s[0]*10000+s[2]*100+s[3];
                return s;
            },
            type: "numeric"
        });
    
        $(document).ready(function() {
            $('.tablesorter').tablesorter({ 
                headers: { 
                    1: { 
                        sorter:'customDate' 
                    } 
                } 
            }); 
    
        });
    

    现在它可以正常工作了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多