FNCDataGrid filter questions

Hi,

I have two questions regarding filtering.

  1. How is best to go about adding to a filter dynamically? I have seen the documentation regarding the chaining of filters but I haven't been able to add dynamically another condition to a column that already has a condition applied to it.

  2. How are gftNotEqual / gftNotContains supposed to work? I have added it as a filter in the form of
    .Filter.Add(0, gftNotEqual, val)
    and
    .Filter.Add(0, gftNotContains, val)

but all it does is blank the data. I was presuming that the intention of these was to filter out whatever is passed as val?

Thanks

Hi,

You need to add the filter together with an operation. Right now you have 2 individual operations.

  TMSFNCDataGrid1.Filter.Clear;
  TMSFNCDataGrid1.Filter.Add(1, gftNotEqual, 'Marie Terzi').&And(1, gftNotContains, 'David');
  TMSFNCDataGrid1.ApplyFilter;

Hi,

Thanks for the reply.

Unfortunately, this hasn't fixed my issues. In the example you've given, you've added the conditions dynamically at the same time; I want to add the second condition after the first, i.e.

TMSFNCDataGrid1.Filter.Add(1, gftNotEqual, 'Marie Terzi');
"AND"
TMSFNCDataGrid1.Filter.Add(1, gftNotContains, 'David');

Regarding the gftNotEqual/gftNotContains, I have applied it in the form of
TMSFNCDataGrid1.Filter.Add(1, gftNotEqual, 'Marie Terzi');

And it simply blanks the grid when I apply it? I'm testing this in a fresh project using the SampleData.

Thanks!

If you have a filter and want to add a new one with an AND operation later you need to use

TMSFNCDataGrid1.Filter.Add(1, gftNotContains, 'David', gfoAND);

for the filter with Marie Terzi, you need to add double quotes to detect the space, or add a question mark

TMSFNCDataGrid1.Filter.Add(1, gftNotEqual, 'Marie?Terzi');
OR
TMSFNCDataGrid1.Filter.Add(1, gftNotEqual, '"Marie Terzi"');

Hi Pieter,

Thanks again for the reply.

I have the dynamic filter chaining working now. Thank you for the example!

However, I still can't get the gftNotEqual/gftNotContains to work.
I have set up a new project and added a TMSFNCDataGrid and two buttons to it. I have added the following routines:

procedure TForm1.WebFormShow(Sender: TObject);
begin
  TMSFNCDataGrid1.LoadSampleData;
end;

procedure TForm1.WebButton1Click(Sender: TObject);
begin
  TMSFNCDataGrid1.ClearFilter;
  TMSFNCDataGrid1.Filter.Add(1, gftNotContains, 'a');
  TMSFNCDataGrid1.ApplyFilter;
end;

procedure TForm1.WebButton2Click(Sender: TObject);
begin
  TMSFNCDataGrid1.ClearFilter;
  TMSFNCDataGrid1.Filter.Add(1, gftNotEqual, 'Marie?Terzi');
  TMSFNCDataGrid1.ApplyFilter;
end;

Pressing either button returns no results.

Am I doing something incorrect here?

Thanks!

Hi after further testing, we noticed an issue in processing the filter in TMS WEB core. We didn't notice at first you were targeting TMS WEB core. A fix can be expected in the upcoming release, aimed for this week.

Hi Pieter,

Sorry, should have specified it was WEB Core! Thanks for that. Glad to hear it'll be fixed soon!

Regarding the dynamic filter chaining, I'm wondering if what I need is not as easy I thought.

What I need to be able to do is:

  1. Add and chain filters dynamically to a column
  2. Be able to remove each of these dynamically, as needed
  3. Do the above in conjunction with filters on other columns

Is this something that can be done using Filters.Items.Add/Remove, or is there a finer grain level of control I need?

Thanks again!

You can add an remove filters, it's a collection. The first one does not need an operation, but all the others need an AND or OR operation. Note that with each filter being added or removed, you need to clear and reapply filters. Unfortunately there is no built-in way to search for a specific filter, you'll have to do this yourself.

function TForm22.FilterByID(AID: string): TTMSFNCDataGridDataFilterData;
var
  I: Integer;
begin
  Result := nil;
  for I := 0 to TMSFNCDataGrid1.Filter.Count - 1 do
  begin
    if TMSFNCDataGrid1.Filter.Items[I].DataString = AID then
    begin
      Result := TMSFNCDataGrid1.Filter.Items[I];
      Break;
    end;
  end;
end;

If you want to add a filter, uniquely identify it with the DataString property.

  TMSFNCDataGrid1.ClearFilter;
  TMSFNCDataGrid1.filter.Add(3, gftStartsWith, 'a').DataString := 'MyFirstFilter';
  TMSFNCDataGrid1.ApplyFilter;

  TMSFNCDataGrid1.ClearFilter;
  TMSFNCDataGrid1.filter.Add(3,  gftEndsWith, 'e', gfoAnd).DataString := 'MySecondFilter';
  TMSFNCDataGrid1.ApplyFilter;

If you want to remove an existing filter

var
  f: TTMSFNCDataGridDataFilterData;
begin
  TMSFNCDataGrid1.ClearFilter;
  f := FilterByID('MyFirstFilter');
  if Assigned(f) then
    TMSFNCDataGrid1.Filter.Delete(f.Index);
  TMSFNCDataGrid1.ApplyFilter;
end;

Brilliant, thanks for that Pieter and all your help on this topic!

1 Like

Ah, sorry, another question, where you've put:

  TMSFNCDataGrid1.ClearFilter;
  TMSFNCDataGrid1.filter.Add(3, gftStartsWith, 'a').DataString := 'MyFirstFilter';
  TMSFNCDataGrid1.ApplyFilter;

  TMSFNCDataGrid1.ClearFilter;
  TMSFNCDataGrid1.filter.Add(3,  gftEndsWith, 'e', gfoAnd).DataString := 'MySecondFilter';
  TMSFNCDataGrid1.ApplyFilter;

In this scenario, MyFirstFilter will be cleared after the ClearFilter() and MySecondFilter can't be chained because we've deleted the other filter?

And in the remove an existing filter example, doesn't calling ClearFilter() simply clear all the filters anyway, so calling ApplyFilter() won't do anything because we've cleared all the filters?

Please use TMSFNCDataGrid1.RemoveFilter; instead, which will clear the grid but not remove the filter data items.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.