0

I have TFDMemTable data from API. The TFDMemTable is livebinded with TListView. The tricky part here is that, I want to show in the TListView the filtered data only using OnChangeTracking event of TEdit control.

Here's the code that I am working with to effect my intended result but, unfortunately, it is not returning anything.

procedure TformMain.edtSearchBoxChangeTracking(Sender: TObject);
var
  metastr : string;
begin
  metastr := edtSearchBox.text;   //edtSearchBox is my TEdit control
  with dmMain_u.dmMain do
  begin
    mtbMeta.Active := False;      //mtbMeta is my TFDMemTable
    mtbMeta.Filtered := False;
    mtbMeta.Filter := 'meta LIKE ' + QuotedStr('%' + metastr + '%');
    mtbMeta.Filtered := True;
    mtbMeta.Active := True;
  end;
end;

Can anyone here try to check my code if it is correct? Or I might need to do something else?

Juke
  • 135
  • 2
  • 9
  • 1
    I would remove the .Active := False and .Active := True. They are the same as calls to .Close and .Open, and the Close will cause mtbMeta to lose its data. – MartynA Jun 01 '20 at 12:11
  • @MartynA You are right. I have to removed .Active:=True/False. It is now working. Thank you so much! But here's my observation, it's off the topic if you don't mind. I noticed that it is so slow compared to my previous direct database TFDQuery with parameter. – Juke Jun 01 '20 at 12:18
  • @MartynA Can you post your answer so I can mark it already as answered. Thanks. – Juke Jun 01 '20 at 12:19

1 Answers1

1

I would remove the .Active := False and .Active := True. They are the same as calls to .Close and .Open, and the Close will cause mtbMeta to lose its data.

As for the slowness you mentioned in your comment, the usual way around that is to to something like this:

procedure TformMain.edtSearchBoxChangeTracking(Sender: TObject);
var
  metastr : string;
begin
  Timer1.Enabled := True;
end;

and put your remaining code from your q into Timer1's OnTimer event. With its time interval set to say, 150 or 200 (millseconds), using the timer this way effectively waits until your typing "pauses for breath" as it were, rather than trying to update the gui on every keypress.

MartynA
  • 30,454
  • 4
  • 32
  • 73
  • I tried your timer suggestion but still it is slower compared to directly passing query with parameter to the database. I suspect the reason of slowness is because the TListView has loaded the data already along with API request that is being executed during OnActivate of the form, though I already set the TFDMemTable.Active:=False at design time. I am looking right now on how to prevent the TListView from loading the data during activation of the form. – Juke Jun 01 '20 at 12:59