Filter for JSF table

I would like to add a filter for the JSF table and restrict the values โ€‹โ€‹based on the 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 believe the best way is to add the filter value to the SQL query:

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

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

How can I implement this for code in a link?

PS. I changed the code like this:

 <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; } 
+5
source share

Source: https://habr.com/ru/post/1246584/


All Articles