【问题标题】:jquery/tablesorter header disabled if date column如果日期列禁用 jquery/tablesorter 标题
【发布时间】:2011-01-03 08:45:27
【问题描述】:

我正在为我的表使用 jquery/tablesorter,其列是文本和数字的混合。一切正常。接下来,我添加了一个新的日期列,现在标题已完全禁用。即,我无法对任何列进行排序。我启动了萤火虫,当页面加载时我看到一个错误:

s.replace 不是下面代码中的函数

this.formatFloat = function (s) {
  var i = parseFloat(s.replace(/,/g, ''));
  return (isNaN(i)) ? 0 : i;
};

当我放置一个断点并查看 s 的值时,对于日期列,它是某个数字,例如 736283783,而对于所有其他数字,它是一个类似“1”或“123”的字符串。我尝试了不同的日期格式,例如“dd/mm/yy”、“dd-mm-yyyy”、“2011 年 1 月 1 日”等。

更新:上面的代码在 jquery.tablesorter.js 中。这不是我写的代码。

我的表源如下所示:

<table id="historyTable" class="fullwidth sortable">
        <thead>
            <tr>
                <th>Status</th>
                <th>Start</th>
                <th>End</th>
                <th>Type</th>
                <th>Keyword</th>
             </tr>
        </thead>
             <tr>
                <td>Active</td>
                <td>2008-09-18</td>
                <td></td>
                <td>Info</td>
                <td>TF</td>
             </tr>
             <tr>
                <td>Inactive</td>
                <td>2010-09-18</td>
                <td></td>
                <td>Info</td>
                <td>ZX</td>
             </tr>
       </table>

我的 JS 代码只是像这样调用 tablesorter: jQuery("table.sortable").tablesorter(); 我做错了什么?

【问题讨论】:

  • 能否贴出 HTML 和 JS 以提供更好的视角?

标签: jquery tablesorter


【解决方案1】:

试试这个:

this.formatFloat = function (s) {
  // add .toString() to convert it to a string
  var i = parseFloat(s.toString().replace(/,/g, ''));
  return (isNaN(i)) ? 0 : i;
};

可能因为s 不是string 而无法正常工作?

更新; 所以是这样的:

this.formatFloat = function (s) {
  // add .toString() to convert it to a string<br/>
  var strDate = new Date(s.toString().replace(/,/g, ''));   
  var i = parseFloat(strDate);

  return (isNaN(i)) ? 0 : i;
};

【讨论】:

  • 更新了问题。该代码在 jquery.tablesorter.js 中。嗯你是对的。 s 不是导致问题的字符串,但不确定在哪里/如何解决它。
  • 只需转换it back to a date
  • 他们想返回一个浮点数,看我的更新......首先将其转换为字符串,然后将其转换回日期,然后将其解析为浮点数
  • @Mahesh:你试过 Tricker 的建议了吗,我试过了,它的工作原理。您遇到的其他错误/问题是什么?
  • 我想我快到了。当我传入一个日期对象时,它似乎工作正常。虽然,我的表格内容看起来像这样 Wed Dec 31 1969 18:00:01 GMT-0600 (CST)。如果我将其格式化并显示为“mm/dd/yy”,则它不起作用。为了清楚起见,我正在动态获取日期值。这是我与 js 混合的 rails 代码: start = new Date()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多