DevExpress cxDBFilterControl值枚举

时间:2014-02-03 16:54:36

标签: delphi devexpress vcl

我正在尝试枚举cxDBFilterControl中的条件,包括任何组。 字段名称,运算符,值字段。

cxDBFilterControl1.ApplyFilter;
Memo1.Lines.Add(cxDBFilterControl1.FilterCaption);
Memo1.Lines.Add('');
for ci := 0 to cxDBFilterControl1.Criteria.Root.Count - 1 do begin
  if not cxDBFilterControl1.Criteria.Root.Items[ci].IsItemList then begin
    Memo1.Lines.Add(
//    ((cxDBFilterControl1.Criteria.Root.Items[ci] as TcxFilterCriteriaItem).ItemLink as TcxFilterItem).FieldName +' : '+ // typecast issue
      (cxDBFilterControl1.Criteria.Root.Items[ci] as TcxFilterCriteriaItem).Operator.DisplayText +' : '+
      (cxDBFilterControl1.Criteria.Root.Items[ci] as TcxFilterCriteriaItem).DisplayValue
);
  end
  else begin
  end;
end;

上面的代码将列出没有字段名称的根级别标准,如何获取字段名称以及任何添加的组及其运算符?

cxDBFilterControl1.FilterCaption output:
(id = 1) and (ttype > 4) and ((tdate < 2/2/2014) or (outcome = test))

枚举输出应该是:

id equals 1
AND
ttype equals 4
AND
  tdate lessthan 2/2/2014
  OR
  outcome equals test

1 个答案:

答案 0 :(得分:0)

enter image description here

procedure Form1.Test
begin
  PrintFilter(Memo1, cxDBFilterControl1.Criteria.Root);
end;

procedure PrintFilter(Memo: TMemo; ARoot: TcxFilterCriteriaItemList; Level: integer = 0);
var
  I: Integer;
  Op: string;
  Line: string;
  LItemLink: TObject;
  LItem: TcxFilterCriteriaItem;
begin
  for I := 0 to ARoot.Count - 1 do
  begin
    //extract into a function?
    case ARoot.BoolOperatorKind of
      fboAnd: Op := 'AND';
      fboOr: Op := 'OR';
      fboNotAnd: Op := 'NOT AND';
      fboNotOr: Op := 'NOT OR';
    end;
    //
    if not ARoot.Items[I].IsItemList then
    begin
      if I <> 0 then
        Memo.Lines.Add(StringOfChar(' ', Level * 2) + Op);
      LItem := (ARoot.Items[I] as TcxFilterCriteriaItem);
      LItemLink := LItem.ItemLink;
      if (LItemLink <> nil) and (LItemLink is TField) then
        Line := TField(LItemLink).DisplayName;
      Line := Line + ' ' + LItem.Operator.DisplayText +' '+ LItem.DisplayValue;
      Memo.Lines.Add(StringOfChar(' ', Level * 2) + Line)
    end else
    begin
      if TcxFilterCriteriaItemList(ARoot.Items[I]).Count > 0 then
        Memo.Lines.Add(StringOfChar(' ', Level * 2) + Op);
      PrintFilter(Memo, TcxFilterCriteriaItemList(ARoot.Items[I]), Level + 1);
    end;
  end;
end;