TcxLookupComboBox 对下拉列表中的所有列进行即时筛选

https://supportcenter.devexpress.com/ticket/details/t857755/filtering-in-tcxlookupcombobox
有时过滤结果只有一条时不能自动选择匹配项

<cxLookupCombobox>.Properties.DropDownListStyle  := lsEditList;  
<cxLookupCombobox>.Properties.IncrementalFiltering := false;  

uses
  cxDBData, cxCustomData, cxFilter;

procedure SplitDelimitedString(AStrings: TStrings; AText, ADelimiter: string);  
var  
  p, n: integer;  
  Text: PChar;  
begin  
  Text := PChar(AText);  
  AStrings.Clear;  
  n := Length(ADelimiter);  
  while Assigned(Text) do  
  begin  
    p := Pos(ADelimiter, Text) - 1;  
    if p < 0 then  
      Break;  
    AStrings.Add(Copy(Text, 1, p));  
    Inc(Text, p + n);  
  end;  
  if Assigned(Text) and (Length(Text) > 0) then  
    AStrings.Add(Text);  
end;  

procedure ApplySearchFilter(Controller: TcxDBDataController; Fields: string; Text: string);  
var  
  i, j: integer;  
  ItemLink: TObject;  
  Filter: TcxDataFilterCriteria;  
  FL: TcxFilterCriteriaItemList;  
  FieldList, TextWords: TStrings;  
begin  
  TextWords := TStringList.Create;  
  FieldList := TStringList.Create;  
  try  
    Filter := Controller.Filter;  
    Filter.BeginUpdate;  
    try  
      Filter.Active := false;  
      Filter.Clear;  
      Filter.Options := Filter.Options + [fcoCaseInsensitive];  
      Filter.Root.BoolOperatorKind := fboOr;  
      SplitDelimitedString(FieldList, Fields, ';');  
      SplitDelimitedString(TextWords, Text, ' ');  
      for i := 0 to FieldList.Count - 1 do  
        if FieldList[i] <> '' then  
        begin  
          FL := Filter.Root.AddItemList(fboAnd);  
          ItemLink := Controller.GetItemByFieldName(FieldList[i]);  
          if Assigned(ItemLink) then  
            for j := 0 to TextWords.Count - 1 do  
              if TextWords[j] <> '' then  
                FL.AddItem(ItemLink, foLike, '%' + TextWords[j] + '%', TextWords[j]);  
        end;  
      Filter.Active := true;  
    finally  
      Filter.EndUpdate;  
    end;  
  finally  
    TextWords.Free;  
    FieldList.Free;  
  end;  
end;  

procedure <AForm>.<AcxDBLookupComboBox>PropertiesChange(Sender: TObject);  
var  
  S: TcxDBLookupComboBox;  
begin  
  S := TcxDBLookupComboBox(Sender);  
  ApplySearchFilter(S.Properties.DataController, S.Properties.ListFieldNames, S.Text);  

  // 用 Label同步显示过滤条件
  Label1.Caption := S.Properties.DataController.Filter.FilterText;  
end;  
posted @ 2024-10-29 14:31  汉学  阅读(25)  评论(0)    收藏  举报