Frage

I managed to make a "Search" bar through a TEdit that seeks whatever i type from inside a ListView that gets its information from a DataBase and goes through a filter and updates the ListView's items on the fly after a key is pressed.

Now i am trying to learn how to implement a way of limiting the results i get in my ListView temporarily until i press a Show More button or something like that in order to get some more relevant results.

Since the Database might return over 500 results by the time i press "A" and that would be harsh to a mobile phone's capabilities so i need that feature to make my Search button more efficient.

Could someone give me some pointers on what i can use in order to make something like that?

EDIT.

The current code i am using for searching in the ListView is this...

   procedure TContactsForm.Edit1ChangeTracking(Sender: TObject);
var
   Lower: string;
   i: integer;
begin
   Lower:= LowerCase(Edit1.Text.Trim);
   if Lower= '' then
   begin
     if Filtered then
       begin
       ListView1.Items.Filter := nil;
       ListView1.ItemIndex := BindSourceDB1.ComponentIndex;
       end;
     end
     else
     begin
       ListView1.ItemIndex := -1;
       ListView1.Items.Filter :=
       function(X: string): Boolean
       begin
         Result:= (Lower = EmptyStr) or LowerCase(X).Contains(Lower);
       end;
    end;
   end;

 function TContactsForm.Filtered: Boolean;
 begin
   Result := Assigned(ListView1.Items.Filter);
 end;
War es hilfreich?

Lösung

The easiest way is to model your select statement so it only returns a limited about of rows (you can always remove the limitation upon user request).

For SQLite, MySQL and PostgreSQL you'd use a LIMIT clause:

SELECT acolumn FROM atable WHERE afield LIKE :param LIMIT 4;  

In SQL Server you'd have to do something like:

SELECT * FROM ( 
  SELECT column, ROW_NUMBER() OVER (ORDER BY name) as row FROM atable 
) a WHERE a.row <= 4

This has the added benefit that less data is generated and transmitted by the database.

When doing the full search you simple omit the limit clause.

If you want to keep the results you already have and just add to extra results, use a
LIMIT 20 OFFSET 5 clause (without the offset keyword the operands are reversed LIMIT 5,20).
You always want to limit so as to make the experience snappy.
Every new page, you fetch the next x records.

You can even do this in real time, as the user is scrolling the list down.
Fetch new records as he nears the bottom of the list.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top