⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 untfilteroption.pas

📁 简要说明:对医院幼儿心理情况做的一个调查,统计系统.
💻 PAS
字号:
unit untFilterOption;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, untBaseDialog, StdCtrls, Buttons, ExtCtrls, DBGrids, DB,
  ADODB, jpeg, RzTabs;

type
  TFilterDataField = (fdtString, fdtNumeric); 

  TfrmFilterOption = class(TfrmBaseDialog)
    pcClient: TRzPageControl;
    tsValue: TRzTabSheet;
    rgLocateOptions: TRadioGroup;
    tsRange: TRzTabSheet;
    chk_UnFiltered: TCheckBox;
    Label1: TLabel;
    edtFilter: TEdit;
    Label2: TLabel;
    Label3: TLabel;
    edtStart: TEdit;
    edtEnd: TEdit;
    procedure FormShow(Sender: TObject);
    procedure rgLocateOptionsClick(Sender: TObject);
    procedure pcClientChange(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
  private
    { Private declarations }
    AFilterType: TFilterDataField;
    procedure InitControl;
    procedure RefreshDataFieldType;
    function GenerateFilterStr: string;
  public
    { Public declarations }
    FilterStr: string;
    FGrid: TDBGrid;
  end;

function FilterDataSet(AGrid: TDBGrid; var AFilterStr: string): Boolean;
function FilterCurValue(AGrid: TDBGrid): string;

implementation

uses untGlobalFun;

function FilterDataSet(AGrid: TDBGrid; var AFilterStr: string): Boolean;
begin
  Result := False;
  if (AGrid.DataSource.DataSet = nil) or (not AGrid.DataSource.DataSet.Active) then
    Exit;
  with TfrmFilterOption.Create(Application) do begin
    try
        FGrid :=  AGrid;
        FilterStr := Trim(AFilterStr);
        if ShowModal = mrOk then begin
          AFilterStr := FilterStr;
          Result := True;
        end;
      finally
        Free;
    end;
  end;
end;

function FilterCurValue(AGrid: TDBGrid): string;
var
  Ffs: string;
begin
  case AGrid.SelectedField.DataType of
    ftInteger, ftBCD, ftSmallInt, ftLargeInt, ftBoolean,
    ftWord, ftFloat, ftCurrency: begin
                                   Ffs := AGrid.SelectedField.FieldName + '=' + AGrid.SelectedField.AsString;
                                 end;
    else begin
            Ffs := AGrid.SelectedField.FieldName + '=''' + AGrid.SelectedField.DisplayText + '''';
         end;
  end;

  Result := Ffs;
end;

{$R *.dfm}

procedure TfrmFilterOption.FormShow(Sender: TObject);
begin
  inherited;
  pcClient.ActivePageIndex := 0;
  edtFilter.Text := FilterStr;
  edtStart.Text := FilterStr;
  edtEnd.Text := FilterStr;
  InitControl;
  RefreshDataFieldType;
end;

procedure TfrmFilterOption.InitControl;
begin
  Label1.Caption := FGrid.SelectedField.DisplayLabel;
end;

procedure TfrmFilterOption.RefreshDataFieldType;
begin
  if FGrid.SelectedField.DataType in [ftBCD, ftInteger, ftSmallInt, ftBoolean, ftFloat, ftCurrency,ftLargeint ] then
    AFilterType := fdtNumeric
  else
    AFilterType := fdtString;

  rgLocateOptions.Enabled := AFilterType = fdtString;
end;

procedure TfrmFilterOption.rgLocateOptionsClick(Sender: TObject);
begin
  inherited;
  if AFilterType <> fdtString then begin
    chk_UnFiltered.Enabled := False;
    chk_UnFiltered.Checked := False;
    Exit;
  end;
  if rgLocateOptions.ItemIndex <> 0 then begin
      if chk_UnFiltered.Checked then
        chk_UnFiltered.Checked := False;
      chk_UnFiltered.Enabled := False;
   end else begin
    chk_UnFiltered.Enabled := True;
  end;
end;

procedure TfrmFilterOption.pcClientChange(Sender: TObject);
begin
  inherited;
  if pcClient.ActivePageIndex <> 0 then begin
    chk_UnFiltered.Enabled := True;
  end else begin
    if rgLocateOptions.ItemIndex <> 0 then
      chk_UnFiltered.Enabled := False
    else
      chk_UnFiltered.Enabled := True;
  end;
end;

procedure TfrmFilterOption.btnOKClick(Sender: TObject);
begin
  inherited;
  case pcClient.ActivePageIndex of
    0: begin
         if edtFilter.Text = '' then begin
           MsgOK('过滤值不能为空!');
           edtFilter.SetFocus;
           Exit;
         end;
       end;
    1: begin
         if (edtStart.Text = '') and (edtEnd.Text = '') then begin
           MsgOK('起始和终止值不能为空!');
           edtStart.SetFocus;
           Exit;
         end;
       end;
  end;
  FilterStr := GenerateFilterStr;
  ModalResult := mrOk;
end;

function TfrmFilterOption.GenerateFilterStr: string;
var
  FFilterStr: string;
begin
  FFilterStr := '';
  case pcClient.ActivePageIndex of
    0: begin
         FFilterStr := FGrid.SelectedField.FieldName;
         case chk_UnFiltered.Checked of
           True: begin
                   case AFilterType of
                     fdtString: FFilterStr := FFilterStr + ' <> ';
                    fdtNumeric: FFilterStr := FFilterStr + ' <> ';
                   end;
                 end;
          False: begin
                   case AFilterType of
                     fdtString: FFilterStr := FFilterStr + ' like ';
                    fdtNumeric: FFilterStr := FFilterStr + ' = ';
                   end;
                 end;
         end;
         case AFilterType of
            fdtString: begin
                          case rgLocateOptions.ItemIndex of
                            0: FFilterStr := FFilterStr + '''' + edtFilter.Text + '''';
                            1: FFilterStr := FFilterStr + '''*' + edtFilter.Text + '*''';
                            2: FFilterStr := FFilterStr + '''' + edtFilter.Text + '*''';
                          end;
                       end;
           fdtNumeric: FFilterStr := FFilterStr + edtFilter.Text;
         end;
       end;
    1: begin
         if edtStart.Text <> '' then begin
           FFilterStr := FGrid.SelectedField.FieldName;
           case chk_UnFiltered.Checked of
             True: FFilterStr := FFilterStr + ' < ';
            False: FFilterStr := FFilterStr + ' >= ';
           end;

           case AFilterType of
              fdtString: FFilterStr := FFilterStr + '''' + edtStart.Text + '''';
             fdtNumeric: FFilterStr := FFilterStr + edtStart.Text;
           end;
         end;

         if edtEnd.Text <> '' then begin
             if Trim(FFilterStr) = '' then
                FFilterStr := FGrid.SelectedField.FieldName
           else begin
             if not chk_UnFiltered.Checked then
                FFilterStr := FFilterStr + ' and ' + FGrid.SelectedField.FieldName
              else
                FFilterStr := FFilterStr + ' or ' + FGrid.SelectedField.FieldName;
          end;
           case chk_UnFiltered.Checked of
             True: FFilterStr := FFilterStr + ' > ';
            False: FFilterStr := FFilterStr + ' <= ';
           end;

           case AFilterType of
              fdtString: FFilterStr := FFilterStr + '''' + edtEnd.Text + '''';
             fdtNumeric: FFilterStr := FFilterStr + edtEnd.Text;
           end;
         end;
       end;
  end;
  Result := FFilterStr;
  //ShowMessage(FFilterStr);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -