B4J Question Search field on a Tableview

Peter Lewis

Active Member
Licensed User
Longtime User
Hi All

I am looking for a way to be able to search my SQLite data and display the results in a Tableview.

I am from a Clarion Background and we had the option to place a box next to the table and as we started typing in what we were looking for it would automatically filter out records depending on what was typed in, even if it was not complete.

Does B4J have something like this ?

I have searched around the Forums but could not find anything, maybe my search wording was wrong.

Thank you

Peter
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
There are all kinds of ways to implement it.

I would have started with the direct approach. Handle the TextChanged event. Check if the text length is larger than 2 or 3 and create a SQL query with LIKE to filter based on the prefix.

Example is attached.

SS-2017-05-11_16.04.21.png


The source for the CSV file with the data: http://blog.plsoucy.com/2012/04/iso-3166-country-code-list-csv-sql/
 

Attachments

  • SQL_TABLE_WITH_FILTER.zip
    8.8 KB · Views: 390
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
There are all kinds of ways to implement it.

I would have started with the direct approach. Handle the TextChanged event. Check if the text length is larger than 2 or 3 and create a SQL query with LIKE to filter based on the prefix.

Example is attached.

SS-2017-05-11_16.04.21.png


The source for the CSV file with the data: http://blog.plsoucy.com/2012/04/iso-3166-country-code-list-csv-sql/


I updated and tested with one of my tables and it works exactly how I wanted it to, I am surprised I did not find it searching the forums. How should I have searched for it ? I have having to ask questions all the time.


There was only one issue with this and if I put the following lines of code as you suggested, then the Tableview problem I had earlier by not displaying the correct headers and column widths does not work. So I took these 2 lines and and it worked again.

Are these lines Important ?

B4X:
'    SearchField_TextChanged("", "")
'    SearchField.RequestFocus


Thank You
 
Upvote 0

inakigarm

Well-Known Member
Licensed User
Longtime User
The first line calls the SearchField (textfield view) Textchanged Event with params "" to fill the tableview with all the records from DB
B4X:
If New = "" Then
        DBUtils.ExecuteTableView(sql, "SELECT name, code FROM countries", Null, 100, tableview1)

The second calls the focus on Searchfield (textfield) view.

Maybe if you introduce a delay before calling this lines (with Callsubdelayed or the new sleep function) the tableview1 redraws correctly (difficult without seeing the code)
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
The first line calls the SearchField (textfield view) Textchanged Event with params "" to fill the tableview with all the records from DB
B4X:
If New = "" Then
        DBUtils.ExecuteTableView(sql, "SELECT name, code FROM countries", Null, 100, tableview1)

The second calls the focus on Searchfield (textfield) view.

Maybe if you introduce a delay before calling this lines (with Callsubdelayed or the new sleep function) the tableview1 redraws correctly (difficult without seeing the code)

I found out how to solve it. Initially I had to move all the code that relates to the table ie names and widths to after the DBUTILS commands. Not that DBUTILS command are inside another sub , I have to do it there as well and it works.

Thank you for responding
 
Upvote 0
Top