Valentina Troche
Valentina Troche

Reputation: 65

How to filter dataTable items?

Problem

When creating a dataTable using Bootfaces, I can bring all items of my DB but I need to bring just the ones which field equals a specific number. ¿How can I do that?

Example

Evento (Id, nombre, categoria)

dataTable only brings the first one, Id 1.

Code

<b:dataTable value="#{eventoController.items}" var="item" styleClass="list" scroll-x="true">

    <h:column>
        <f:facet name="header">
            <h:outputText value="#{bundle.ListEventoTitle_idEvento}"/>
        </f:facet>
        <h:outputText value="#{item.idEvento}"/>
    </h:column>
                    
    <h:column>
        <f:facet name="header">
            <h:outputText value="#{bundle.ListEventoTitle_nombre}"/>
        </f:facet>
        <h:outputText value="#{item.nombre}"/>
    </h:column>

    <h:column>
        <f:facet name="header">
            <h:outputText value="#{bundle.ListEventoTitle_idCategoria}"/>
        </f:facet>
        <h:outputText value="#{item.idCategoria}"/>
    </h:column>
                    
</b:dataTable>
  

Upvotes: 1

Views: 1083

Answers (1)

Stephan Rauh
Stephan Rauh

Reputation: 3120

Replace the <h:column> by it's BootsFaces counterpart <b:dataTableColumn />. Now you can set the attribute search-value. Most likely you also need to activate multi-column-search='true'and s searching='true'. In your case, that's

<b:dataTable value="#{eventoController.items}" 
             var="item" 
             styleClass="list" 
             scroll-x="true"
             multi-column-search="true"
             searching="true">
    <b:dataTableColumn search-value="1" searchable="true">
       <f:facet name="header">
          <h:outputText value="#{bundle.ListEventoTitle_idEvento}"/>
       </f:facet>
       <h:outputText value="#{item.idEvento}"/>
    </b:dataTableColumn>
    ...
</b:dataTable>

Please keep in mind that the BootsFaces dataTable is a client-side widget. This means that the entire HTML table is sent to the client. That, in turn, makes for fast filtering, pagination, and sorting once the data has been loaded, but if you've got a huge amount of data, you'll want to filter the data on the server side, too.

Also see https://showcase.bootsfaces.net/forms/DataTable.jsf (search for "initial search filter").

Upvotes: 1

Related Questions