我正在使用cellEditors为用户的ip范围创建一个p:dataTable,其中包含p:inputMask。 dataTable由2个可编辑列组成。要验证我必须:
前两部分噪音不大。第三个要求我检查指定的范围是在先前输入的范围之前还是之后。您只需在每个其他IP范围之前或之后检查开始和结束。容易,对吗?
验证周期。
for (Groupip gip : searchResults) {
long ipend = ipToLong(InetAddress.getByName(gip.getIpend()));
long ipstart = ipToLong(InetAddress.getByName(gip.getIpstart()));
boolean validLeft = true, validRight = true;
validLeft = validLeft && ipstart < ipValidated;
validLeft = validLeft && ipend < ipValidated;
validLeft = validLeft && ipstart < ipValidating;
validLeft = validLeft && ipend < ipValidating;
validRight = validRight && ipstart > ipValidated;
validRight = validRight && ipend > ipValidated;
validRight = validRight && ipstart > ipValidating;
validRight = validRight && ipend > ipValidating;
if (validLeft || validRight) {
//OK
} else {
//ERROR
}
}
dataTable。
<p:dataTable id="ips1" widgetVar="ips1" var="ip" value="#{groupipController.searchResults}" editable="true"
rowKey="#{ip.groupcode}-#{ip.ipstart}-#{ip.ipend}" selection="#{groupipController.selected}" selectionMode="single"
>
<f:facet name="header">
Диапазон IP
</f:facet>
<p:ajax event="rowEditCancel" listener="#{groupipController.onRowCancel}" />
<p:ajax event="rowEditInit" listener="#{groupipController.onRowEditInit}" />
<p:ajax event="rowEdit" listener="#{groupipController.onRowEdit}" update=":form:msgs :form:ips1 :form:growlmsgs" process=":form:ips1"/>
<p:column headerText="От" style="min-height: 16px" id='from_col'>
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{ip.ipstart}" /></f:facet>
<f:facet name="input">
<p:inputMask mask="999.999.999.999" value="#{ip.ipstart}" id="fromip" autoClear="false" slotChar="0"
validator="#{groupipController.validate}"
class="ui-inputfield ui-inputmask ui-widget ui-state-default ui-corner-all p-0-1 w-100 h-100"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="До" style="min-height: 16px" id='to_col'>
<p:cellEditor>
<f:facet name="output"><h:outputText value="#{ip.ipend}" /></f:facet>
<f:facet name="input">
<p:inputMask mask="999.999.999.999" value="#{ip.ipend}" id="toip" autoClear="false" slotChar="0"
validator="#{groupipController.validate}"
class="ui-inputfield ui-inputmask ui-widget ui-state-default ui-corner-all p-0-1 w-100 h-100"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column width="10%" >
<p:rowEditor rendered="#{armgroupController.update}" />
</p:column>
</p:dataTable>
但还有更多。 searchResults
是一个变量,它是dataTable的源,包含一个List<Groupip>
。但它也包含当前正在编辑的行。所以我必须排除它,否则我将无法通过验证,将范围与自身进行比较。
我该怎么做?我能找到编辑行的行索引的唯一方法是使用以下代码获取cliendId的前一个值(在浏览器中看起来像这样:form:ips1:2:toip
):
if (gip == searchResults.get(Integer.parseInt(component.getClientId().split(":")[2]))) {
continue;
}
这是不合适的,因为命名容器可能会改变。所以,我想知道,还有另一种获取行索引的方法吗?
您当前组件的ID始终是完整clientID列表中的最后一个,并且您自己声明采用前一个值,但实际上您采用第三个值(索引2)
如果你真的得到最后一个但是一个索引(base-0)作为rowindex你(大多数)总是很好。
String[] fields = component.getClientId().split(":");
int index = Integer.parseInt(fields[fields.length-2]);
if (gip == searchResults.get(index)) {
continue;
}
如果有人将这个“当前”组件包装在命名容器中,那么索引将不是那个,而是在此之前,甚至在此之前的最后一个。所以你可以在一个循环中添加它来获得分裂clientId的第一部分,这是一个数字,因为JSF(html)id's cannot start with a number因此不能是一个数字所以你是'安全的'那么。
String[] fields = component.getClientId().split(":");
int indexPos = fields.length-2;
int index = -1;
do {
try {
index = Integer.parseInt(fields[indexPos]);
} catch {
indexPos--;
}
} while (index == -1)
if (gip == searchResults.get(index)) {
continue;
}
您还可以尝试将组件的父级转换为Datatable
,看看它是否包含可以使用的getCurrentRow()
之类的东西,但是如果父级不是数据表,那么您还应该创建一个循环来获取父级的父级。
调查的一个例子是http://showcase.omnifaces.org/validators/validateUniqueColumn的来源