【问题标题】:Filter does not take a value from readonly field during filtering过滤器在过滤期间不从只读字段中获取值
【发布时间】:2016-11-14 17:50:57
【问题描述】:

我使用 JSF 2.2、Primefaces 6.0 和 CDI。我有一个datatable,它实现了延迟加载(datatable 从数据库下载数据)。每列都有一个过滤器字段。

其中一列必须有readonly 过滤器字段(我在显示datatable 之前将此过滤器的值保存在某个变量中)。所以,正如我所写的,这个过滤器字段应该是readonly(不可编辑)并且过滤器应该从这个字段中获取值进行过滤。 如何实现此功能?

我尝试添加inputtext 组件并设置readonly 属性:

<p:dataTable id="dataTableOfDataStore" var="obj" widgetVar="dataTableOfDataStoreVar" 
            value="#{formVisualizationController.dataTableLazy}"             
            lazy="true"
            filteredValue="#{formVisualizationController.filteredDataTable}" 
            filterDelay="2000"

            <!-- other attributes -->                       
            >

    <!-- other columns -->

    <p:column headerText="Source IP" sortBy="#{obj.sip}"
            filterBy="#{obj.sip}" filterMatchMode="contains">
        <f:facet name="filter">
            <p:inputText readonly="true" onchange="PF('dataTableOfDataStoreVar').filter()"
             value="#{formVisualizationController.selectedSourceIPFieldOfFiltr.ip}"
            style="width: 100%; background-color: #0088cc; color: #ffffff;"/>
        </f:facet>          
        <h:outputText value="#{obj.sip}" />     
    </p:column> 

    <!-- other columns -->

</p:dataTable>

不幸的是,它不起作用。当我删除 readonly 属性时,它可以工作,但过滤器字段也可以编辑。

当然,我可以通过手动将此值传递给数据库查询,然后从列中删除过滤器并保留 inputtext 组件的值(以及 readonly 属性),但也许你知道一些不同的方法来实现这一点。

【问题讨论】:

    标签: jsf primefaces filter datatable


    【解决方案1】:

    所选答案的实现

    1. JSF 视图的修改。我保留了包含我的常量值的inputtext 组件(带有readonly 属性),并且我已经删除了此列的过滤器功能(在客户端)。过滤器属性必须保留在&lt;p:column&gt; 组件中,否则在列的标题行中您将看不到inputtext 组件。

      <p:column headerText="Source IP" sortBy="#{obj.sip}"
              filterBy="#{obj.sip}" filterMatchMode="contains">
          <f:facet name="filter">
              <p:inputText readonly="true" value="#{formVisualizationController.selectedSourceIPFieldOfFiltr.ip}"
              style="width: 100%; background-color: #0088cc; color: #ffffff;"/>
          </f:facet>          
          <h:outputText value="#{obj.sip}" />     
      </p:column>
      
    2. load 方法中,我将我的值放入了filters 参数中,该参数存储了过滤所需的所有值。

      dataTableLazy=new LazyDataModel<Data>() {
      
          @Override
          public List<Data> load(int first, int pageSize, List<SortMeta> multiSortMeta,
                  Map<String, Object> filters) {  
      
              //here I put my value
              filters.put("sip", selectedSourceIP.getIp());
      
              List<Data> result=dataService.getLazyDataStoreTable(idSeletedUserTable, first, pageSize, multiSortMeta, filters);
      
              //other code
      
              return result;
          }
      };
      

    【讨论】:

      【解决方案2】:

      answer 解释了为什么请求中不包含只读字段。因此 Primefaces 不使用它来过滤。

      在我看来,如果你想确保它没有被篡改,你将不得不在服务器端覆盖/检查它。因此,以编程方式设置它似乎是最好的解决方案。

      您可以在查询中或通过以编程方式添加过滤器值来执行此操作。例如,如果它是强制性的,您可以覆盖 load 方法以使用 bean 中的值。

      dataTableLazy = new LazyDataModel<?>(){
          public List<Site> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
              //set your filter here
              super.load(first, pageSize, sortField, sortorder, filters);
          }
      }
      

      【讨论】:

      • 感谢您的反馈。我修改了load 方法,并将我的值(用于Source IP 列)放入load 方法的filters 参数中。这似乎是最好的主意。谢谢!我还添加了答案,其中包括我所做的步骤。
      【解决方案3】:

      我的想法是使用一个选择一个按钮来模拟一个只读过滤器字段,如下所示:

          <p:column headerText="Source IP" sortBy="#{obj.sip}"
                    filterBy="#{obj.sip}" filterMatchMode="contains">
              <f:facet name="filter">
                  <p:selectOneButton onchange="PF('dataTableOfDataStoreVar').filter()">
                      <f:selectItem itemLabel="#{formVisualizationController.selectedSourceIPFieldOfFiltr.ip}" 
                                    itemValue="#{formVisualizationController.selectedSourceIPFieldOfFiltr.ip}" />
                  </p:selectOneButton>
              </f:facet>          
              <h:outputText value="#{obj.sip}" />     
          </p:column> 
      

      【讨论】:

      • Primefaces/JSF 是否自动验证,http 请求中返回的值实际上是一个有效的列表项?如果不是,会发生什么?
      • 感谢您的反馈。您的解决方案有效,但不像我预期的那样。在您的解决方案中,过滤器不会自动工作,所以首先我必须通过单击它来激活按钮。我排除了这样工作的灵魂:当显示datatable 时,过滤器必须自动工作,因为它在过滤器字段中获得了值。不过谢谢你的想法。
      猜你喜欢
      • 1970-01-01
      • 2020-12-29
      • 2018-01-07
      • 2016-02-18
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2010-12-15
      • 2019-08-16
      相关资源
      最近更新 更多