1

我最近在我的 jQuery/tablesorter 表上获得了正确的日期格式。客户在表格中列出公司,并希望其中一列按日期排序,其中一些是投资组合中的“活跃”公司,而其他公司已经退出,那些是使用日期格式的公司。我遇到的问题是,因为这些都在同一个表中,并且日期列按日期排序,所以列为“活动”的公司在列表的开头或结尾分组在一起,但按时间顺序排列命令。

因此,这些公司目前的排序为“活跃”>“活跃”>“活跃”>2006 年 1 月 >2010 年 3 月 >2012 年 2 月等。

我想看看是否有办法将其添加到解析器中,以便将“活动”字符串转换为当前日期,以便公司排序为:“活动”>“活动”>“活动”> 2012 年 2 月 > 2010 年 3 月 > 2006 年 1 月。

这可能吗?有任何想法吗?我决定就此开始一个新线程,因为这是上一个问题的新问题,只是为了让正确的日期格式正常工作。我在下面发布我的 HTML 和代码。提前致谢。

<table id="myTable" class="tablesorter stripeMe sample" width="100%" cellpadding="0"  cellspacing="0">
<thead>
<th width="30%" align="left">COMPANY</th><th width="35%">DESCRIPTION</th><th width="17%"  align="left">INDUSTRY</th><th width="18%" align="left">EXIT DATE</th></tr></thead>
<tbody>
<tr><td width="30%">  Cartera Commerce, Inc. </td>
<td width="35%">Provides technology-enabled marketing and loyalty solutions 
</td><td width="17%">   Financials    </td> <td width="18%">Active</td>
</tr><tr><td width="30%">   Critical Information Network, LLC </td>
<td width="35%">Operates library of industrial professional training and certification materials 
</td><td width="17%">   Education    </td> <td width="18%">Active</td>
</tr><tr><td width="30%"> Cynergydata </td>
<td width="35%">Provides merchant payment processing services and related software products 
</td><td width="17%">   Merchant Processing    </td> <td width="18%">Active</td>
</tr><tr><td width="30%">    </td>
<td width="35%">Operates post-secondary schools  
</td><td width="17%">   Education    </td> <td width="18%">Active</td>
</tr><tr><td width="30%">  CorVu Corporation</td>
<td width="35%">Develops and distributes performance management software products and related services 
</td><td width="17%">   Information Technology    </td> <td width="18%">May 2007</td>
</tr><tr><td width="30%">    Fischer Imaging Corporation </td>
 <td width="35%">Manufactures and services specialty digital imaging systems and other  medical devices
</td><td width="17%">   Healthcare    </td> <td width="18%">Sep 2005</td>
</tr><tr><td width="30%">   Global Transportation Services, Inc.  </td>
<td width="35%">Provides air and ocean transportation and logistics services
</td><td width="17%">   Transportation     </td> <td width="18%">Mar 2010</td>
</tr><tr><td width="30%">  HMP/Rita  </td>
<td width="35%">Operates as a specialty medical device company that manufactures and markets vascular and spinal access systems
</td><td width="17%">   Healthcare    </td> <td width="18%">Dec 2006</td>
</tr>

</tbody>
</table>

和当前的 jQuery 解析器:

$.tablesorter.addParser({
id: 'monthYear',
is: function(s) {
    return false;
},
format: function(s) {
    var date = s.match(/^(\w{3})[ ](\d{4})$/);
    var m = date ? date[1] + ' 1 ' || '' : '';
    var y = date && date[2] ? date[2] || '' : '';
    return new Date(m + y).getTime() || '';
},
type: 'Numeric'
});

$('#myTable').tablesorter({

headers: {
    3: {
        sorter: 'monthYear'
    }
}

});
4

1 回答 1

0

这实际上只是一个简单的修改。将解析器更改为:

var today = new Date().getTime();

$.tablesorter.addParser({
    id: 'monthYear',
    is: function(s) {
        return false;
    },
    format: function(s) {
        // remove extra spacing
        s = $.trim(s.replace(/\s+/g, ' '));
        // process date
        var date = s.match(/^(\w{3})[ ](\d{4})$/),
            m = date ? date[1] + ' 1 ' || '' : '',
            y = date && date[2] ? date[2] || '' : '';
        return /active/i.test(s) ? today : new Date(m + y).getTime() || '';
    },
    type: 'Numeric'
});

表格单元格中的“活动”一词将不区分大小写。这是它工作的演示。请注意,未来的日期(在这种情况下为 2012 年 7 月)将排在“活动”之上。

于 2012-05-13T18:01:01.873 回答