1

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;
4

1 回答 1

0

最简单的方法是对您的 select 语句进行建模,使其仅返回有限的行(您始终可以根据用户请求删除限制)。

对于 SQLite、MySQL 和 PostgreSQL,你会使用一个LIMIT子句:

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

在 SQL Server 中,您必须执行以下操作:

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

这具有额外的好处,即数据库生成和传输的数据更少。

进行完整搜索时,您只需省略 limit 子句。

如果您想保留已有的结果并只是添加到额外的结果中,请使用
LIMIT 20 OFFSET 5子句(没有 offset 关键字,操作数会被反转LIMIT 5,20)。
您总是想限制以使体验变得生动。
每个新页面,您获取下 x 条记录。

您甚至可以在用户向下滚动列表时实时执行此操作。
当他接近列表底部时获取新记录。

于 2013-10-12T16:21:45.297 回答