Sorting 数据表中具有3组值的字符串的自定义排序
我在我的一个html表上使用了数据库。其中当前有多个列。 其中一列可以包含以下三个值之一:Sorting 数据表中具有3组值的字符串的自定义排序,sorting,datatables,Sorting,Datatables,我在我的一个html表上使用了数据库。其中当前有多个列。 其中一列可以包含以下三个值之一: 琥珀色 未决 红色的 我希望实现排序,以便首先看到具有挂起值的所有行,然后是琥珀色,然后是红色。(无法使用默认的升序和降序排序,因为此时顺序将不正确) 代码段: JSP(表创建) 因此,我理想的想法是,当表格显示在页面上时,数据应该根据第一列的数据以及最后一列的数据按字母顺序排序(即挂起,琥珀色,然后是红色)。查看代码片段,您可以使用该选项高度自定义列的过滤/排序方式 我举的例子不是最聪明的,但它让你对你
因此,我理想的想法是,当表格显示在页面上时,数据应该根据第一列的数据以及最后一列的数据按字母顺序排序(即挂起,琥珀色,然后是红色)。查看代码片段,您可以使用该选项高度自定义列的过滤/排序方式 我举的例子不是最聪明的,但它让你对你能做什么有了一个想法。如果你选择这种方法,我建议你使用
var数据集=[
['Name1',3',Red'],
['Name2',2',琥珀色'],
['Name3',1',待定']
];
$(文档).ready(函数(){
$('#示例')。数据表({
数据:数据集,
栏目:[
{标题:“姓名”},
{标题:“价值”},
{
标题:“类型”,
“呈现”:函数(数据、类型、完整、元){
如果(类型=='sort'){
如果(数据==‘红色’)返回3;
else if(数据=='Pending')返回1;
否则如果(数据==‘琥珀色’)返回2;
}否则{
返回数据;
}
}
}
]
} );
} );代码>
答案取决于您使用的是客户端还是服务器端处理。请显示您的DataTables初始化代码。@Gyrocode.com,Servlet将对象数组传递给jsp,然后这些对象被迭代到表中创建的行中。感谢上面的代码,它确实有帮助。我也用jsp和servlet代码更新了这个问题。你能看一下并告诉我你的看法吗
<table class="tableContent nofx cell-border compact" id="violationTable">
<thead>
<tr>
<th class="col1"><i18n:message key="rule.name" /></th>
<th class="col2"><i18n:message key="rule.value" /></th>
<th class="col3"><i18n:message key="rule.isr.value" /></th>
<th class="col4"><i18n:message key="rule.status" /></th>
</tr>
</thead>
<tbody>
<c:forEach items="${ruleViolationList}" var="i" varStatus="loopStatus">
<tr data-rule-id="<c:out value="${i.id}" />" data-country-id="<c:out value="${i.countryId}" />"
>
<td class="col1">
<c:out value="${i.PolicyRule}" />
</td>
<td class="col2">
<c:out value="${i.RuleValue}" escapeXml="false" />
</td>
<td class="col3">
<c:out value="${i.isrValue}" />
</td>
<c:choose>
<c:when test="${i.violationTypeId == 1}">
<td class="red status" >
<i18n:message key="rule.violation.red" />
</td>
</c:when>
<c:when test="${i.violationTypeId == 2}">
<td class="amber status" >
<i18n:message key="rule.violation.amber" />
</td>
</c:when>
<c:when test="${i.violationTypeId == 4}">
<td class="blue status" >
<i18n:message key="rule.violation.dispensation.approval.pending" />
</td>
</c:when>
<c:when test="${i.violationTypeId == 5}">
<td class="amber status" >
<i18n:message key="rule.violation.amber" />
</td>
</c:when>
<c:when test="${i.violationTypeId == 6}">
<td class="red status" >
<i18n:message key="rule.violation.red" />
</td>
</c:when>
</c:choose>
</tr>
</c:forEach>
</tbody>
</table>
ArrayList<RuleViolation> ruleViolationList = daoFactory.getRuleViolationsDAO().list();
request.setAttribute("ruleViolationList", ruleViolationList);
$(document).ready(function() {
$('#violationTable').DataTable();
});