jQuery 类型错误:$.fn.dataTable.moment 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37118273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: $.fn.dataTable.moment is not a function
提问by Lucky
I'm using jquery DataTable plugin and in one of my tables, I wanted to sort the result based on the date time column. So, I included moment.js version 2.13.0 and my dataTable version is 1.10.10 and my jQuery version is 1.9.1.
我正在使用 jquery DataTable 插件,并且在我的一个表中,我想根据日期时间列对结果进行排序。所以,我包含了 moment.js 版本 2.13.0,我的 dataTable 版本是 1.10.10,我的 jQuery 版本是 1.9.1。
As suggested in this latest dataTable date time sorting plugin article https://datatables.net/blog/2014-12-18, I tried the following but in the console I have
正如最新的 dataTable 日期时间排序插件文章https://datatables.net/blog/2014-12-18 中所建议的,我尝试了以下操作,但在控制台中我有
TypeError: $.fn.dataTable.moment is not a function
$.fn.dataTable.moment('DD-MMM-Y HH:mm:ss');
in my html page,
在我的 html 页面中,
$(document).ready(function() {
$.fn.dataTable.moment('DD-MMM-Y HH:mm:ss');
$('#myTable').DataTable();
} );
My Date column data has date in this format, 09-May-2016 19:38:00
. And I have interchanged the order in which the dataTable and moment.js plugin source is included in my html page. But I still get the same error. What could be the issue?
我的日期列数据具有这种格式的日期,09-May-2016 19:38:00
. 我已经交换了 dataTable 和 moment.js 插件源包含在我的 html 页面中的顺序。但我仍然得到同样的错误。可能是什么问题?
采纳答案by Lucky
Browser cache issue. The JS files that I included are not properly downloaded in the browser causing the issue. I cleared the cache and used the CDN version. And it worked fine. Here's my working code if anyone needs in future,
浏览器缓存问题。我包含的 JS 文件没有正确下载到浏览器中,导致了这个问题。我清除了缓存并使用了CDN版本。它工作得很好。如果将来有人需要,这是我的工作代码,
JS imports:
JS进口:
<script th:src="@{/js/jquery-1.8.3.js}" />
<script th:src="@{/js/jquery.datatables.min.js}" />
<script th:src="@{//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js}" />
<script th:src="@{//cdn.datatables.net/plug-ins/1.10.11/sorting/datetime-moment.js}" />
DataTable init:
数据表初始化:
$(document).ready( function () {
$.fn.dataTable.moment('DD-MMM-Y HH:mm:ss');
$('#jobcardsTable').DataTable({
responsive: true,
"order": [[ 2, "desc" ]]
});
});
So when initially the datatable loads the custom formatted date is parsed from the third column using moment.js and sorted in descending order.
因此,当最初数据表加载自定义格式的日期时,使用 moment.js 从第三列解析并按降序排序。
回答by Ossipon
The order of importing the scripts is important. You have to include datatables before the sorting plugin. The following works for me:
导入脚本的顺序很重要。您必须在排序插件之前包含数据表。以下对我有用:
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
<script src="//cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
<script src="//cdn.datatables.net/plug-ins/1.10.12/sorting/datetime-moment.js"></script>
回答by Ravichandra Sellappan
The two files below are required;
需要以下两个文件;
moment.min.js
, anddatetime-moment.js
moment.min.js
, 和datetime-moment.js
Do load all files in the following order:
请按以下顺序加载所有文件:
- Moment,
- DateTables,
- DataTables + Moment plug-in.
- 片刻,
- 日期表,
- DataTables + Moment 插件。