这是您需要的大部分内容,至少您可以大致了解...
您的 xhtml 应如下所示:
<p:dataTable var="myLittleWrapper" value="#{myBean.modified}">
<p:column headerText="PATH">
<h:outputText value="#{myLittleWrapper.myObject.path}" styleClass="#{(myLittleWrapper.colorMe)?'equalMembers':''}"/>
</p:column>
...
创建以下方法(从您那里调用@PostConstruct
)
List<MyObject> original = new ArrayList<MyObject>();//fill it with data
List<MyObjectWrapper> modified = new ArrayList<MyObjectWrapper>();// + getter/setter
public void prepareModifiedList() {
ListIterator<MyObject> myListIter = original.listIterator();
while (myListIter.hasNext()) {
MyObject myObj = myListIter.next();
MyObject nextMyObj = null;
boolean colorMe = false;
try {
nextMyObj = peek(myListIter);
} catch (NoSuchElementException e) {
}
if (nextMyObj != null && myObj.getPath().equals(nextMyObj.getPath())) {
colorMe = true;
}
MyObjectWrapper myObjectWrapper = new MyObjectWrapper(myObj, colorMe);
modified.add(myObjectWrapper);
}
}
方法peek
如下所示:
public <T> T peek(ListIterator<T> iter) throws NoSuchElementException {
T obj = iter.next();
iter.previous();
return obj;
}
在哪里MyObject
和MyObjectWrapper
看起来像这样
public class MyObject {
public MyObject(String path) {
super();
this.path = path;
}
private String path;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
和
public class MyObjectWrapper {
MyObject myObject;
boolean colorMe;
public MyObjectWrapper(MyObject myObject, boolean colorMe) {
super();
this.myObject = myObject;
this.colorMe = colorMe;
}
public MyObject getMyObject() {
return myObject;
}
public void setMyObject(MyObject myObject) {
this.myObject = myObject;
}
public boolean isColorMe() {
return colorMe;
}
public void setColorMe(boolean colorMe) {
this.colorMe = colorMe;
}
}
如果我理解正确,您可以执行以下操作:
<h:outputText value="#{someclass.somemember}"
styleClass="#{(someclass.someMember eq someclass.someOtherMember)?'equalMembers':''}"/>
在你的 CSS 添加
.equalMembers {
color:red;
}