Saturday, April 16, 2016

Filter for JSF table

Leave a Comment

I would like to add filter for JSF table and limit the values based on filter value.

<h:inputText id="search" value="#{accounts.searchString}"></h:inputText>  <h:commandButton value="Search" action="#{accounts.search()}">     <f:ajax execute="search" render="output"></f:ajax> </h:commandButton> 

I suppose that the best way will be to add the filter value into the SQL query:

SELECT * FROM CUSTOMERS ORDER BY %S %S offset ? limit ? 

Full code: http://pastebin.com/eEeTWEqK

How I can implement this for the code into the link?

PS. I modified the code this way:

<div class="div_input_style">     <h:inputText id="search" class="input_style" value="#{accounts.searchString}"></h:inputText>      <h:commandButton class="theSpan" value="Search by title">         <f:ajax execute="search" render="output"></f:ajax>     </h:commandButton> </div>  public List<AccountsObj> list(int firstRow, int rowCount, String sortField, boolean sortAscending) throws SQLException     {         String SqlStatement = null;          if (ds == null)         {             throw new SQLException();         }          Connection conn = ds.getConnection();         if (conn == null)         {             throw new SQLException();         }          String sortDirection = sortAscending ? "ASC" : "DESC";          SqlStatement = "SELECT * FROM ACCOUNT "             + " WHERE ? IS NULL OR ? IN (USER_NAME, FIRST_NAME, LAST_NAME)"             + " ORDER BY %S %S offset ? limit ? ";          String sql = String.format(SqlStatement, sortField, sortDirection);          PreparedStatement ps = null;         ResultSet resultSet = null;         List<AccountsObj> resultList = new ArrayList<>();          try         {             conn.setAutoCommit(false);             boolean committed = false;              ps = conn.prepareStatement(sql);             ps.setString(1, searchString);             ps.setString(2, searchString);             ps.setInt(3, firstRow);             ps.setInt(4, rowCount);              resultSet = ps.executeQuery();             resultList = ProcessorArrayList(resultSet);              conn.commit();             committed = true;          }         finally         {             ps.close();             conn.close();         }          return resultList;     } 

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment