【问题标题】:Jquery datatable sort date in French法语中的Jquery数据表排序日期
【发布时间】:2015-12-23 19:34:08
【问题描述】:

我以这个链接为例。 http://datatables.net/blog/2014-12-18

我尝试用法语对日期列进行排序,但它没有正确排序。 看起来它不理解法语格式。如果我更改为英文日期并使用默认语言环境(英文),那么它可以正常工作。

以下是我尝试过的。请让我知道我做错了什么。

$(document).ready(function () {

moment.locale('fr', {
        months : "janvier_février_mars_avril_mai_juin_juillet_août_septembre_octobre_novembre_décembre".split("_"),
        monthsShort : "janv._févr._mars_avr._mai_juin_juil._août_sept._oct._nov._déc.".split("_"),
        weekdays : "dimanche_lundi_mardi_mercredi_jeudi_vendredi_samedi".split("_"),
        weekdaysShort : "dim._lun._mar._mer._jeu._ven._sam.".split("_"),
        weekdaysMin : "Di_Lu_Ma_Me_Je_Ve_Sa".split("_"),
        longDateFormat : {
            LT : "HH:mm",
            LTS : "HH:mm:ss",
            L : "DD/MM/YYYY",
            LL : "D MMMM YYYY",
            LLL : "D MMMM YYYY LT",
            LLLL : "dddd D MMMM YYYY LT"
        },calendar : {
            sameDay: "[Aujourd'hui à] LT",
            nextDay: '[Demain à] LT',
            nextWeek: 'dddd [à] LT',
            lastDay: '[Hier à] LT',
            lastWeek: 'dddd [dernier à] LT',
            sameElse: 'L'
        },
        relativeTime : {
            future : "dans %s",
            past : "il y a %s",
            s : "quelques secondes",
            m : "une minute",
            mm : "%d minutes",
            h : "une heure",
            hh : "%d heures",
            d : "un jour",
            dd : "%d jours",
            M : "un mois",
            MM : "%d mois",
            y : "une année",
            yy : "%d années"
        },
        ordinalParse : /\d{1,2}(er|ème)/,
        ordinal : function (number) {
            return number + (number === 1 ? 'er' : 'ème');
        },
        meridiemParse: /PD|MD/,
        isPM: function (input) {
            return input.charAt(0) === 'M';
        },
        // in case the meridiem units are not separated around 12, then implement
        // this function (look at locale/id.js for an example)
        // meridiemHour : function (hour, meridiem) {
        //     return /* 0-23 hour, given meridiem token and hour 1-12 */
        // },
        meridiem : function (hours, minutes, isLower) {
            return hours < 12 ? 'PD' : 'MD';
        },
        week : {
            dow : 1, // Monday is the first day of the week.
            doy : 4  // The week that contains Jan 4th is the first week of the year.
        }
    });



      moment.locale('fr');

    $.fn.dataTable.moment('d MMM yyyy','fr');
    $('.testtable').dataTable();
});

这里是完整的例子:http://jsfiddle.net/9gohzd9t/71/

【问题讨论】:

    标签: jquery datatables momentjs datatables-1.10


    【解决方案1】:

    数据表中的日期未通过isValid() 检查。您当前的格式是D MMM yyyy。在这种格式中,yyyy 应替换为 YYYY。大写是正确的符号。

    其次,您拥有的月份符号没有通过严格的isValid() 测试。两种可能的解决方案:

    1. 去掉短月份缩写后的点。例如janv 而不是janv.

      你的桌子是:

      <table class="testtable">
          <thead>
              <tr>
                  <th>Date</th>
              </tr>
          </thead>
          <tbody>
              <tr>
                  <td>12 mars 2001</td>
              </tr>
              <tr>
                  <td>1 janv 2015</td>
              </tr>
              <tr>
                  <td>1 févr 2014</td>
              </tr>
          </tbody>
      </table> 
      

      有关最小示例,请参阅 this JSFiddle

    2. 修改插件以删除严格的标准,并将点添加到您的格式中。

      插件修改部分:

      // Removed true as the last parameter of the following moment
      return moment( d, format, locale ).isValid() ?
          'moment-'+format :
      null;
      

      你的新格式:

      $.fn.dataTable.moment('D MMM. YYYY','fr');
      

      有关此示例,请参阅 this JSFiddle

    【讨论】:

      猜你喜欢
      • 2016-12-11
      • 2015-03-03
      • 1970-01-01
      • 2011-09-09
      • 2018-11-22
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多