AdminLTE v2.3.8。数据表日期时间列排序
AdminLTE v2.3.8. Datatable DateTime column sort
考虑这段代码(我使用 razor ASP.NET C#,但这并不重要)。
我能够对所有列进行排序,但最后一列是按字符串排序的日期时间。
我只想知道是否可以使用 AdminLTE 数据表对日期时间列进行排序,添加或不添加(更好)插件。
<div class="table-responsive">
<table id="tableClienti" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Zona</th>
<th>Classe</th>
<th>Rag Soc</th>
<th>Indirizzo</th>
<th>Email/Telefono</th>
<th>Contatto</th>
<th>Email/Telefono</th>
<th>Ultimo Pass.</th>
</tr>
</thead>
<tbody>
@{
foreach (var r in Model)
{
<tr>
<td>@r.Zona</td>
<td>@r.ClasseCliente</td>
<td>@r.RagioneSociale</td>
<td><b>@r.Citta @r.CAP</b><br /> @r.Indirizzo</td>
<td>@r.Email<br /> @r.Telefono</td>
<td><b>@r.NomeContatto<br /> @r.CognomeContatto</b></td>
<td>@r.EmailContatto<br /> @r.TelefonoContatto</td>
<td>@r.DataUltimoPassaggio</td>
</tr>
}
}
</tbody>
</table>
</div>
$(function () {
var table = $("#tableClienti").DataTable({
paging: false,
"sDom": '<"top"i>rt<"bottom"><"clear">',
"scrollX": true,
"scrollY": "45vh",
"bSort": true
]
});
如果你偶然发现了这个问题,我在使用数据顺序遇到问题后解决了。
This link 展示了数据顺序的工作原理,但在同一页面上找到的这条评论对我来说是关键:
Its important to notethat the data-* attributes with search() will
only work if you use this on every COL!!! You can not mix it like:
< tr>< td>Value1< /td>< /tr> < tr>< td data-search='Value 2
XYZ'>Value2< /td>< /tr> It seems the Plugin-Code always check the first
row and then use this value for all search operations. Maybe for the
future would be very cool if you can mix it ;):
Best Regards Tom
以前,我尝试仅在 "DateTime variable" 不为空时才向
添加数据顺序。
多亏了这条评论,我设置了 data-order = "DateTime variable".Ticks when "DateTime variable" is not null 和 data-order = 0 when "DateTime variable" is null.
所以,我改了这段代码
<td>@r.DataUltimoPassaggio</td>
这样
@{
DateTime data;
if (DateTime.TryParse(r.DataUltimoPassaggio.ToString(), out data))
{
<td data-order="@(data.Ticks)">@(data.ToString())</td>
}
else
{
<td data-order="0"></td>
}
}
考虑这段代码(我使用 razor ASP.NET C#,但这并不重要)。 我能够对所有列进行排序,但最后一列是按字符串排序的日期时间。 我只想知道是否可以使用 AdminLTE 数据表对日期时间列进行排序,添加或不添加(更好)插件。
<div class="table-responsive">
<table id="tableClienti" class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Zona</th>
<th>Classe</th>
<th>Rag Soc</th>
<th>Indirizzo</th>
<th>Email/Telefono</th>
<th>Contatto</th>
<th>Email/Telefono</th>
<th>Ultimo Pass.</th>
</tr>
</thead>
<tbody>
@{
foreach (var r in Model)
{
<tr>
<td>@r.Zona</td>
<td>@r.ClasseCliente</td>
<td>@r.RagioneSociale</td>
<td><b>@r.Citta @r.CAP</b><br /> @r.Indirizzo</td>
<td>@r.Email<br /> @r.Telefono</td>
<td><b>@r.NomeContatto<br /> @r.CognomeContatto</b></td>
<td>@r.EmailContatto<br /> @r.TelefonoContatto</td>
<td>@r.DataUltimoPassaggio</td>
</tr>
}
}
</tbody>
</table>
</div>
$(function () {
var table = $("#tableClienti").DataTable({
paging: false,
"sDom": '<"top"i>rt<"bottom"><"clear">',
"scrollX": true,
"scrollY": "45vh",
"bSort": true
]
});
如果你偶然发现了这个问题,我在使用数据顺序遇到问题后解决了。
This link 展示了数据顺序的工作原理,但在同一页面上找到的这条评论对我来说是关键:
Its important to notethat the data-* attributes with search() will only work if you use this on every COL!!! You can not mix it like:
< tr>< td>Value1< /td>< /tr> < tr>< td data-search='Value 2 XYZ'>Value2< /td>< /tr> It seems the Plugin-Code always check the first row and then use this value for all search operations. Maybe for the future would be very cool if you can mix it ;):
Best Regards Tom
以前,我尝试仅在 "DateTime variable" 不为空时才向
所以,我改了这段代码
<td>@r.DataUltimoPassaggio</td>
这样
@{
DateTime data;
if (DateTime.TryParse(r.DataUltimoPassaggio.ToString(), out data))
{
<td data-order="@(data.Ticks)">@(data.ToString())</td>
}
else
{
<td data-order="0"></td>
}
}