Bin*_*xus 2 javascript ajax jquery jquery-datatables
我正在处理来自 DataTables.net 的这个示例,但我进行了修改,以便它可以与我对 API 的 ajax 调用一起使用。
我的问题是我的 API 返回这样的 DateTime 值......
创建=2015-02-13T00:00:00
我需要能够将它转换为只是日期而不是我的表的时间(希望不更改 API)。我已经尝试了所有我知道的尝试。我仍然是这个高级 javascript 东西的初学者。我试图做一个简单的子字符串,但我认为这行不通。好吧,至少我是如何尝试的。
感谢任何帮助!
数据表 v1.10.5
jquery v1.11.2(因需要支持IE8)
原问题代码:
$(document).ready(function () {
var table = $('#AllHuddleRecords').DataTable({
"ajax": "../api/huddle/posts",
"columns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data" : "EmpName" },
{ "data": "Created" },
{ "data" : "PriorityName" },
{ "data" : "TopicName" }
]
});
Run Code Online (Sandbox Code Playgroud)
感谢cmxl的指导……这里是工作代码……
var table = $('#AllHuddleRecords').DataTable({
"ajax": "../api/huddle/posts",
"columns": [
{
"className": 'details-control',
"orderable": false,
"data": null,
"defaultContent": ''
},
{ "data" : "EmpName" },
{ "data": "Created" },
{ "data" : "PriorityName" },
{ "data" : "TopicName" }
],
"columnDefs": [
{
"render" : function (data, type, row) {
return new Date(data).toLocaleString();
},
"targets": 2
}
]
});
Run Code Online (Sandbox Code Playgroud)
您可以连接到列渲染事件。请参阅此处的文档:https : //datatables.net/examples/advanced_init/column_render.html
$(document).ready(function() {
$('#example').dataTable( {
"columnDefs": [
{
// The `data` parameter refers to the data for the cell (defined by the
// `data` option, which defaults to the column being worked with, in
// this case `data: 0`.
"render": function ( data, type, row ) {
return data.slice(0, data.indexOf('T'));
},
"targets": 0
},
{ "visible": false, "targets": [ 3 ] }
]
} );
} );
Run Code Online (Sandbox Code Playgroud)
或者,如果您想将字符串解析为日期,您可以在此处参考此答案: Converting string to date in js
//...
"render": function ( data, type, row ) {
return new Date(data).toString();
}
//...
Run Code Online (Sandbox Code Playgroud)
在这里,您可以更深入地了解 Javascript 中的 Date 对象:https : //developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date
| 归档时间: |
|
| 查看次数: |
5140 次 |
| 最近记录: |