简体   繁体   English

tablesorter按日期期间/日期格式排序dd.mm.yyyy-dd.mm.yyyy

[英]tablesorter sort by date period / date format dd.mm.yyyy - dd.mm.yyyy

I have the following table: 我有下表:

    <?php 
    $reqSQL = "SELECT * FROM data_cursanti , clients WHERE  start_course > DATE_SUB(now(), INTERVAL 12 MONTH) ORDER BY data_cursanti.name ASC ";
    $result = mysqli_query($conexiune, $reqSQL) or die("Error: " . mysqli_error($conexiune)); // sql query for results than the table
    $counter = 1; // for rows counting
    echo '<table id="users" class="tablesorter">
    <thead>
    <tr>
    <th width="3%">No.</th>
    <th width="15%">Name</th>
    <th width="10%">Company</th>
    <th width="7%">Course</th>
    <th width="10%">Course Period</th>
    </tr>
    </thead>
    <tbody>'
    while ($row = mysqli_fetch_array($result)) {
    $date1 = date('d.m.Y', strtotime($row['start_course']));
    $date2 = date('d.m.Y', strtotime($rand['end_course']));
    echo
    '<tr>
    <td width="3%">'.$counter.'</td>
    <td width="15%">'.$row['name'].'</td>
    <td width="10%">'.$row['company_name'].'</td>
    <td width="7%">'.$row['course_type'].'</td>
    <td width="10%">'.$date1.' - '.$date2.'</td>
    </tr>'
    $counter++;
    } //end while loop
    echo '</tbody>
    </table>'; //close table

and using the following parser (slightly modified from here: How to sort date with jquery tablesorter which is in format 'dd/mm/yyyy - dd/mm/yyyy' ) for tablesorter to sort by my course period column (the column uses date format dd.mm.yyyy - dd.mm.yyyy): 并使用以下解析器(从此处稍作修改: 如何使用jquery tablesorter对日期进行排序,其格式为“ dd / mm / yyyy-dd / mm / yyyy” ),以便tablesorter按课程期间列进行排序(该列使用日期格式dd.mm.yyyy-dd.mm.yyyy):

$(function() {

    $.tablesorter.addParser({
        id: "date-range",
        is: function(){
            return false;
        },
        format: function(s, table, cell) {

            var dates = s.replace(/(\d{1,2})[\/\.](\d{1,2})[\/\.](\d{4})/g, "$2/$1/$3").split(' - '),
                parsed = [];
            if (dates.length) {
                $.each(dates, function(i,d){
                    var v = new Date(d);
                    parsed.push($.type(v) === 'date' ? v.getTime() : d);
                });
            }
            return parsed.length ? parsed.join('') : s;
        },
        parsed : true,
        type: "text"
    });

// and calling the parser after this script like this: 


    $(function() {
$("#users")
.tablesorter({widthFixed: true, widgets: ['zebra'],  headers: {0: {sorter: false}, 4 : { sorter: 'date-range' }, {sortForce: [[0,0]]})
.tablesorterPager({container: $("#pager"), size: 20});
});

the problem is that the table gets sorted by dd (day) part of the first date, instead of month as I would like it to be!. 问题是表格在第一个日期的dd(天)部分而不是我希望的月份中被排序! Any ideas? 有任何想法吗? What am I doing wrong? 我究竟做错了什么? Could somebody please modify my code to sort the table data by the mm (month) value of the first date? 有人可以修改我的代码,以按第一个日期的mm(月)值对表格数据进行排序吗? I have like almost zero knowledge of javascript ... Thankyou! 我对JavaScript的了解几乎为零...谢谢!

LE: Also I tried to use the solution presented by mottie in the first answer but the table just breaks down and won't sort anything when I add the 4 : { sorter: 'date-range' } piece of code in the script. LE:我也尝试在第一个答案中使用mottie提出的解决方案,但是当我在脚本中添加4:{sorter:'date-range'}这段代码时,表只是崩溃了,什么也不排序。

The problem is that the dates in this question are separated by periods and the regexp for the other question was using regex which only looked for forward slashes or spaces ( demo ). 问题在于该问题中的日期由句点分隔,而另一个问题的正则表达式使用正则表达式,该正则表达式仅查找正斜杠或空格( demo )。

This regexp will work with mm-dd-yyyy dates separated by " - ", " / ", " . " or " 此正则表达式将与mm-dd-yyyy日期一起使用,这些日期之间用“ - ”,“ / ”,“ . ”或“ " (spaces). ”(空格)。

var dates = s.replace(/(\d{1,2})[-./\s](\d{1,2})[-./\s](\d{4})/g, "$2/$1/$3").split(' - ')

Also, the sorter setting needs to target the last row: 另外, sorter设置需要定位到最后一行:

$("table").tablesorter({
    headers: {
        4: {
            sorter: 'date-range'
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM