gpt4 book ai didi

javascript - 如何使用 dd-MMM-yyyy hh :mm tt format in datatable 对日期进行排序

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:54:42 24 4
gpt4 key购买 nike

我目前正在处理数据表,我发现我对日期列的排序不起作用。这是我的截图

datatable view

这是我的代码

<table id="tbl" class="table table-small-font table-bordered table-striped">
<thead>
<tr>
<th>&nbsp;</th>
<th class="text-left">Dated</th>
<th class="text-left">Day</th>
<th class="text-center">Remarks</th>
<th class="text-center">State</th>
<th class="text-center"></th>
</tr>
</thead>
<tbody>
@{ IEnumerable
<PublicHoliday> PublicHolidays = (IEnumerable
<PublicHoliday>)ViewData["PublicHolidays"]; int Idx = 1; } @foreach (var i in PublicHolidays) {
<tr>
<td>@Idx</td>
<td>@i.Dated.ToString("dd-MMM-yyyy hh:mm tt")</td>
<td>@i.Dated.ToString("ddd")</td>
<td>@Html.DisplayFor(x => i.Remarks)</td>
<td>@i.ForStateName</td>
<td><a data-toggle="tooltip" title="Delete" onclick="DeleteRecord(@i.Id);"><span class="glyphicon glyphicon-remove"></span></a></td>
</tr>
Idx++; }
</tbody>
</table>
<script src="~/lib/jquery-ui-1.12.1/jquery-ui.min.js"></script>
<script src="~/js/jquery.dataTables.min.js"></script>
<script src="~/js/dataTables.bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var tbl = $('#tbl').DataTable({
dom: "<'row'<'col-sm-6 col-xs-7'l><'col-sm-6 col-xs-7'f>>" + "rtip",
order: [
[0, "asc"]
],
pagingType: "numbers",
iDisplayLength: 50
});
});
</script>

排序列根本不起作用,而且我无法从数据表中找到任何插件来使用。任何人请帮助..提前致谢

最佳答案

我不确定是否有插件支持date-dd-MMM-yyyy hh:mm tt格式

所以我修改了插件以支持这种格式。

这里是它的代码。加载datatable插件后加载这段代码

   (function() {

var customDateDDMMMYYYYToOrd = function(date) {
var dateTime = date.split(' '),
dateObj = new Date(dateTime[0].replace(/-/g, ' ')),
time = "00:00",
type = "AM";
if (dateTime.length > 1) { // if time part and am/pm part is available
time = dateTime[1],
type = dateTime[2];
}

var splitTime = time.split(":"),
hours = type == "PM" ? Number(splitTime[0]) + 12 : Number(splitTime[0]),
minutes = Number(splitTime[1]),
seconds = 0,
milliseconds = 0;
return new Date(dateObj.getFullYear(), dateObj.getMonth(), dateObj.getDate(), hours, minutes, seconds, milliseconds);
};

// This will help DataTables magic detect the "dd-MMM-yyyy" format; Unshift
// so that it's the first data type (so it takes priority over existing)
jQuery.fn.dataTableExt.aTypes.unshift(
function(sData) {
"use strict"; //let's avoid tom-foolery in this function
if (/^([0-2]?\d|3[0-1])-(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)-\d{4}/i.test(sData)) {
return 'date-dd-mmm-yyyy';
}
return null;
}
);

// define the sorts
jQuery.fn.dataTableExt.oSort['date-dd-mmm-yyyy-asc'] = function(a, b) {
"use strict"; //let's avoid tom-foolery in this function
var ordA = customDateDDMMMYYYYToOrd(a),
ordB = customDateDDMMMYYYYToOrd(b);
return (ordA < ordB) ? -1 : ((ordA > ordB) ? 1 : 0);
};

jQuery.fn.dataTableExt.oSort['date-dd-mmm-yyyy-desc'] = function(a, b) {
"use strict"; //let's avoid tom-foolery in this function
var ordA = customDateDDMMMYYYYToOrd(a),
ordB = customDateDDMMMYYYYToOrd(b);
return (ordA < ordB) ? 1 : ((ordA > ordB) ? -1 : 0);
};

})();

以上代码是对date sort plugin(dd-mmm-yyyy)的修改.我修改了 customDateDDMMMYYYYToOrd 函数以适应这个特定示例

添加这个以便插件知道在日期排序时使用什么

   var tbl = $('#tbl').DataTable({
dom: "<'row'<'col-sm-6 col-xs-7'l><'col-sm-6 col-xs-7'f>>" + "rtip",
order: [[0, "asc"]],
pagingType: "numbers",
pageLength: 50,
columnDefs: [
{ type: 'date-dd-mmm-yyyy', targets: 1 } //add this part
]
});

您可以找到可用于datatable 的排序插件列表here

注意:

Please note that this plug-in is **deprecated*. The datetime plug-in provides enhanced functionality and flexibility

关于javascript - 如何使用 dd-MMM-yyyy hh :mm tt format in datatable 对日期进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47765539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com