在selectOneListbox或SelectOneMenu中过滤自定义项以忽略重音符号或特殊字符

时间:2018-09-26 09:48:33

标签: javascript primefaces

如何在这些Primefaces组件的搜索字段中进行过滤,以使它们忽略诸如重音符号之类的特殊字符

example custom filter

1 个答案:

答案 0 :(得分:1)

要自定义过滤器,您必须将filterMatchMode属性定义为“ custom”,并使用在Primefaces documentation(466或470)中定义的定义javascript函数的filterFunction =“ myFilter”属性。 / p>

在这种情况下,我将自定义过滤器以忽略包含的重音符号和特殊字符(与filterMatchMode =“ contains”相同)

file.xhtml

<p:selectOneListbox filter="true" filterMatchMode="custom" filterFunction="contains" value="#{controllerBean.id}">  
        <f:selectItems value="#{controllerBean.list}" var="item" itemLabel="#{item.value}" itemValue="#{item.id}" />
</p:selectOneListbox>  

file.js

 function contains(itemLabel, filterValue) {

    return itemLabel.includes(filterValue) || specialCharacters(itemLabel).includes(filterValue);

}

function specialCharacters(input){

           var c=input.toLowerCase();
           c = c.replace(new RegExp("\\s", 'g'),"");
           c = c.replace(new RegExp("[àáâãäå]", 'g'),"a");
           c = c.replace(new RegExp("æ", 'g'),"ae");
           c = c.replace(new RegExp("ç", 'g'),"c");
           c = c.replace(new RegExp("[èéêë]", 'g'),"e");
           c = c.replace(new RegExp("[ìíîï]", 'g'),"i");                           
           c = c.replace(new RegExp("[òóôõö]", 'g'),"o");
           c = c.replace(new RegExp("œ", 'g'),"oe");
           c = c.replace(new RegExp("[ùúûü]", 'g'),"u");
           c = c.replace(new RegExp("[ýÿ]", 'g'),"y");
           return c;
}