Java PrimeFaces如何在p:datatable单元格中表示值的标签
大家好,我有这样一个场景: 我正在使用primefaces数据表Java PrimeFaces如何在p:datatable单元格中表示值的标签,java,jsf,primefaces,primefaces-extensions,Java,Jsf,Primefaces,Primefaces Extensions,大家好,我有这样一个场景: 我正在使用primefaces数据表p:datatable,它从托管bean接收一个列表。。。 请查看此示例:并注意标题“状态” 我的问题是:代表1,2,5是一种耻辱。所以我更喜欢像这样查看状态 1:新的 2:拒绝 5:取消了 我要求的结果是这样的 Request No. | Request Date | Status | 1 | 2013-02-02 | New | 2 | 2013-01-01
p:datatable
,它从托管bean接收一个列表。。。
请查看此示例:并注意标题“状态”
我的问题是:代表1,2,5是一种耻辱。所以我更喜欢像这样查看状态
- 1:新的
- 2:拒绝
- 5:取消了
Request No. | Request Date | Status |
1 | 2013-02-02 | New |
2 | 2013-01-01 | Rejected |
3 | 2013-4-23 | Canceled |
有什么想法吗????
谢谢……为什么不在ManagedBean中创建一个方法,将其转换为所需的标签呢
<p:column headerText="status">
<h:outputText value="#{testBean.getStatusLabel(t.part_status)}"/>
</p:column>
根据维护状态代码和相关描述之间映射的方式和位置,有几种方法
@ManagedBean
@ApplicationScoped
public class Data {
private static final Map<Long, String> STATUSES = createStatuses();
private static Map<Long, String> createStatuses() {
Map<Long, String> statuses = new HashMap<Long, String>();
statuses.put(1L, "New");
statuses.put(2L, "Rejected");
statuses.put(5L, "Cancelled");
return Collections.unmodifiableMap(statuses);
}
public Map<Long, String> getStatuses() {
return STATUSES;
}
}
与
...
public enum Status {
New(1), Rejected(2), Cancelled(5);
private int code;
private Status(int code) {
this.code = code;
}
public static Status of(int code) {
for (Status status : values()) {
if (status.code == code) {
return status;
}
}
throw new IllegalArgumentException();
}
public int getCode() {
return code;
}
}
在填充模型的过程中
t.setPart_status(Status.of(statusCode));
然后就在眼前
与具体问题无关,我会努力解决你的问题<代码>零件状态不是有效的属性名称。它应该是
partStatus
显示带有名称的字段?我们应该如何知道您的对象拥有什么?我认为这种转换不应该在应用程序的表示层中进行,而应该在业务逻辑中进行。使用eunum
或常量或创建数据库表来保存此映射。aha。。。。我知道如何使用数据库和托管bean,但我认为primefaces中有解决方案……谢谢你的回答,帕洛夫……:)
@ManagedBean
@ApplicationScoped
public class Data {
private static final Map<Long, String> STATUSES = createStatuses();
private static Map<Long, String> createStatuses() {
Map<Long, String> statuses = new HashMap<Long, String>();
statuses.put(1L, "New");
statuses.put(2L, "Rejected");
statuses.put(5L, "Cancelled");
return Collections.unmodifiableMap(statuses);
}
public Map<Long, String> getStatuses() {
return STATUSES;
}
}
public enum Status {
New(1), Rejected(2), Cancelled(5);
private int code;
private Status(int code) {
this.code = code;
}
public static Status of(int code) {
for (Status status : values()) {
if (status.code == code) {
return status;
}
}
throw new IllegalArgumentException();
}
public int getCode() {
return code;
}
}
t.setPart_status(Status.of(statusCode));