如何排序字符串和日期与表排序jquery插件


How to sort both strings and dates with tablesorter jquery plugin?

当涉及到我的表(字符串数据)的一些数据排序时,我有一些麻烦要处理。我从数据库中获取日期,并像这样格式化它们:

    $change['ProposedDate'] = date('d/m/Y',strtotime($r['ProposedDate']));

(注意,我也试过:

)
    $change['ProposedDate'] = (string)date('d/m/Y',strtotime($r['ProposedDate']));

)

,我用它来转换为"None"

if($change['ProposedDate'] == "30/11/-0001")
{
    $change['ProposedDate'] ="None";
}

表是这样的(PHP在HTML源代码中将$c[' proposddate ']转换为"XX/YY/ZZZZ"之后):

<table>
 <thead>
  <tr>
   <th> ID </th>
   <th> Proposed Date</th>
   <th> Suggested Date</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>1</td>
   <td>01/10/2014</td>
   <td>None</td>
  </tr>
  <tr>
   <td>2</td>
   <td>14/10/2014</td>
   <td>10/12/2014</td>
  </tr>
  <tr>
   <td>3</td>
   <td>None</td>
   <td>09/09/2014</td>
  </tr>
 </tbody>
</table>

我不能理解的是,我能够排序字符串,如"14-0214-98","13-0101-01"answers"14-0102-40",或"当前","等待","新"answers"提交"。但是我不会排序日期。在包含日期的两行之间有"None"。我尝试了许多解决方案,例如使用空字符串"来替换"None",或者使用"a"来替换"None",但都不起作用。

谁有办法解决这个问题?

注:对我来说,将日期转换为字符串没有任何问题,稍后我将转换回它们(例如将"None"转换为"30/11/-0001")。所以如果你有一个解决字符串的方法,我会接受的!

谢谢大家,我真的很绝望

我猜第一列可能包含"None"而不是日期,这将导致插件检测常规文本而不是该列的日期。因此,您需要专门为日期设置解析器。

我不知道为什么你正在使用的版本表,所以这里是你可以做到的:

  1. 原始表分类器(demo):

    $('table').tablesorter({
        dateFormat : "uk",
        headers: {
            // set "sorter: false" (no quotes) to disable the column
            0: { sorter: "text" },
            1: { sorter: "shortDate" },
            2: { sorter: "shortDate" }
        }
    });
    
  2. My fork of tablesorter (demo):

    $('table').tablesorter({
        theme: 'blue',
        // other options: "mmddyyyy" & "yyyymmdd"
        dateFormat: "ddmmyyyy",
        headers: {
            // set "sorter: false" (no quotes) to disable the column
            0: { sorter: "text" },
            1: { sorter: "shortDate" },
            2: { sorter: "shortDate" }
        }
    });