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

📄 jcamtablevieweditor.pas

📁 销售软件
💻 PAS
📖 第 1 页 / 共 3 页
字号:
procedure TfrmTableViewEditorForm.pFillInfoByKeyFieldID(
  const nKeyFieldID: Integer );
var
  rsInfo: _Recordset;
  cmp: TComponent;
  sFieldName, sFieldValue: string;
  n: Integer;
begin
  { 列出内容由关键字段的值 }

  if DBListView <> nil then
    with DBListView do
    begin
      m_nCurrentIdx := nKeyFieldID;
      rsInfo := CoRecordset.Create;

      // TODO: 列出所有数据
      rsInfo.Open( 'SELECT * FROM [' + TableViewName + '] WHERE [' +
        KeyFieldName + ']=' + IntToStr( nKeyFieldID ),
        Self.Connection, adOpenForwardOnly, adLockReadOnly, adCmdText );

      if RecordsetValid( rsInfo ) then
        for n := 0 to sbxInfoList.ComponentCount - 1 do
        begin
          cmp := sbxInfoList.Components[ n ];
          if ( cmp is TCustomEdit ) or ( cmp is TCustomComboBox ) then
          begin
            // TODO: 获取每个控件所对应的字段
            sFieldName := PFieldProp( TControl( cmp ).Tag )^.FieldName;
            sFieldValue := VarToStr( rsInfo.Fields[ sFieldName ].Value );

            if cmp is TCustomEdit then
              TCustomEdit( cmp ).Text := sFieldValue
            else
              TRzComboBox( cmp ).Text := sFieldValue;
          end;
        end;
      rsInfo := nil;
    end;
end;

procedure TfrmTableViewEditorForm.pFillListView( const bClearColumnHeader:
  Boolean = True );
var
  tf: TTableField;
begin
  { 初始化并填充ListView }

  if DBListView <> nil then
    with DBListView do
    begin
      Visible := False;
      Connection := Self.Connection;
      SQLString := 'SELECT * FROM [' + TableViewName + ']' + m_sLoadCondition;
      KeyFieldName := Self.KeyFieldName;
      ShowKeyField := Self.ShowKeyField;
      StartFillData( bClearColumnHeader );

      if bClearColumnHeader then
      begin
        // TODO: 将列表头翻译成中文并调整状态
        tf := TTableField.Create;
        with tf do
        begin
          Connection := Self.Connection;
          FormatColumnHeader( DBListView );
        end;
        FreeAndNil( tf );
      end;
      Visible := True;
    end;
end;

procedure TfrmTableViewEditorForm.InfoMultiSelected;
begin
  FillAllEditString( STR_DIFFVALS );
  m_nCurrentIdx := -1;
end;

procedure TfrmTableViewEditorForm.pInitEditButtonEnabled;
var
  te: TTableEmployee;
begin
  te := TTableEmployee.Create;
  with te do
  begin
    Connection := Self.Connection;
    if CheckUserCanModify( frmMain.LoginName, m_bUserCanModfy ) then
    begin
      ActionAdd.Enabled := m_bUserCanModfy;
      ActionDelete.Enabled := m_bUserCanModfy;
      ActionEdit.Enabled := m_bUserCanModfy;
    end;
  end;
  FreeAndNil( te );
end;

procedure TfrmTableViewEditorForm.pInitInfoPanel( const bClearComponents:
  Boolean = True );
var
  tf: TTableField;
  tvp: TTableViewParent;
  pfp: PFieldProp;
  cboInfoEditor: TRzComboBox;
  rsTableView: _Recordset;
  sTableViewParent, sFieldName, sRParentCaption: string;
  n, nCmpTop: Integer;
  bFieldLocked, bIsSpecial, bCtlEnabled {, bIsCode}: Boolean;
begin
  { 初始化编辑面板 }

  if bClearComponents then
  begin
    // TODO: 清空编辑面板中的组件
    sbxInfoList.Visible := False;
    pDestroyComponents;

    tf := TTableField.Create;
    with tf do
    begin
      Connection := Self.Connection;
      rsTableView := CoRecordset.Create;
      rsTableView.Open( 'SELECT TOP 0 * FROM [' + TableViewName + ']',
        Connection, adOpenForwardOnly, adLockReadOnly, adCmdText );

      // TODO: 获取视图的母表名
      tvp := TTableViewParent.Create;
      with tvp do
      begin
        Connection := Self.Connection;
        ViewName := Self.TableViewName;
        sTableViewParent := ParentName;
        if sTableViewParent = EmptyStr then
          sTableViewParent := Self.TableViewName;
      end;
      FreeAndNil( tvp );

      // TODO: 循环获取字段
      nCmpTop := 4;
      for n := 0 to rsTableView.Fields.Count - 1 do
      begin
        sFieldName := rsTableView.Fields[ n ].Name;
        FieldName := sFieldName;
        bFieldLocked := Locked;

        if IDFromCode = EmptyStr then
          sRParentCaption := sFieldName
        else
          sRParentCaption := IDFromCode;

        bIsSpecial := tf.FieldInTable( sRParentCaption, sTableViewParent );
        bCtlEnabled := ( not bIsSpecial or m_bSpecialEnabled ) and ( not
          bFieldLocked );
        //bIsCode := False;
        //if not bIsSpecial then
        //  bIsCode := tf.FieldInTable( sFieldName, sTableViewParent );

        if ( ShowKeyField or ( sFieldName <> KeyFieldName ) ) and
          ( ShowSpecial or not bIsSpecial ) then
        begin
          tf.FieldName := sFieldName;

          // TODO: 初始化字段信息并设置值
          New( pfp );
          with pfp^ do
          begin
            FieldName := sFieldName;
            FieldChs := tf.FieldChs;
            Locked := tf.Locked;
            AllowNull := tf.AllowNull;
            AllowRepeated := tf.AllowRepeated;
            RowHeight := tf.RowHeight;
            Hint := tf.Hint;
            Width := tf.ColumnWidth;
            AutoSize := tf.AutoSize;
            CodeTable := tf.CodeTable;
            IDFromCode := tf.IDFromCode;
            NameFromCode := tf.NameFromCode;
            FieldType := rsTableView.Fields[ n ].Type_;
            MaxLength := rsTableView.Fields[ n ].DefinedSize;
          end;

          // TODO: 创建信息的标签
          with TLabel.Create( sbxInfoList ) do
          begin
            Visible := False;
            Parent := sbxInfoList;
            Caption := FieldChs + ':';
            AutoSize := True;
            Tag := n;
            Enabled := ( not bIsSpecial or m_bSpecialEnabled ) and
              ( not bFieldLocked );
            SetBounds( 4, nCmpTop + 4, Width, Height );
          end;

          if pfp^.FieldType = adDBTimeStamp then
            // TODO: 创建选择日期
            with TRzDateTimeEdit.Create( sbxInfoList ) do
            begin
              Visible := False;
              Parent := sbxInfoList;
              HideSelection := False;
              CaptionTodayBtn := RSTR_MANAGER_INFOTODAY;
              CaptionClearBtn := RSTR_MANAGER_INFOCLEAR;
              Tag := Integer( pfp );
              Enabled := bCtlEnabled;
              ReadOnly := True;
              TabOnEnter := True;
              FocusColor := CL_INFOEDITCOLORFOCUS;
              ReadOnlyColor := CL_INFOEDITCOLORREADONLY;
              SetBounds( Left, nCmpTop, Width, Height );

              //OnEnter := pEDTEnter;
            end
          else if pfp^.RowHeight > 1 then
            // TODO: 多行的文本编辑框
            with TRzMemo.Create( sbxInfoList ) do
            begin
              Visible := False;
              Parent := sbxInfoList;
              Tag := Integer( pfp );
              Enabled := bCtlEnabled;
              ReadOnly := True;
              TabOnEnter := True;
              Height := 20 * pfp^.RowHeight;
              ScrollBars := ssVertical;
              FocusColor := CL_INFOEDITCOLORFOCUS;
              ReadOnlyColor := CL_INFOEDITCOLORREADONLY;
              SetBounds( Left, nCmpTop, Width, Height );

              //OnEnter := pEDTEnter;
            end
          else
          begin
            // TODO: 生成下拉关系字段的菜单
            cboInfoEditor := TRzComboBox.Create( sbxInfoList );
            with cboInfoEditor do
            begin
              Visible := False;
              Parent := sbxInfoList;
              Tag := Integer( pfp );
              AutoComplete := False;
              Enabled := bCtlEnabled;
              ReadOnly := True;
              TabOnEnter := True;
              FocusColor := CL_INFOEDITCOLORFOCUS;
              ReadOnlyColor := CL_INFOEDITCOLORREADONLY;
              SetBounds( Left, nCmpTop, Width, Height );

              OnDropDown := pCBODropDown;
            end;

            // TODO: 生成关系字段选择窗体
            with TRzBitBtn.Create( sbxInfoList ) do
              //with TRzControlButton.Create( sbxInfoList ) do
            begin
              Visible := False;
              Parent := sbxInfoList;
              Tag := Integer( cboInfoEditor.ComponentIndex );
              TabStop := False;
              Margin := 1;
              NumGlyphs := 2;
              Glyph.LoadFromResourceName( hInstance, RESTR_BTN_LOOKUP );
              ShowFocusRect := False;
              SetBounds( Left, nCmpTop, 21, 21 );

              OnClick := pBTNClick;
            end;
          end;
          Inc( nCmpTop, pfp^.RowHeight * 20 + 4 );
        end;
      end;
      rsTableView := nil;
    end;
    FreeAndNil( tf );

    // TODO: 调整控件的位置
    sbxInfoListResize( Self );
  end;
  sbxInfoList.Visible := True;
end;

procedure TfrmTableViewEditorForm.SetAllEditReadOnly( const bReadOnly: Boolean
  );
var
  cmp: TComponent;
  n: Integer;
begin
  { 设置所有Edit的readOnly }

  for n := 0 to sbxInfoList.ComponentCount - 1 do
  begin
    cmp := sbxInfoList.Components[ n ];
    if ( cmp is TCustomEdit ) or ( cmp is TCustomComboBox ) then
      SetOrdProp( cmp, 'ReadOnly', Integer( bReadOnly ) );
  end;
end;

procedure TfrmTableViewEditorForm.sbxInfoListResize( Sender: TObject );
var
  cmpContained: TComponent;
  n, nMaxLabelWidth: Integer;
begin
  { 内容信息面板改变大小 }

  nMaxLabelWidth := 0;

  // TODO: 首先获得最大标签宽度
  for n := 0 to sbxInfoList.ComponentCount - 1 do
  begin
    cmpContained := sbxInfoList.Components[ n ];

    // TODO: 如果是标签
    if cmpContained is TLabel then
      with TLabel( cmpContained ) do
      begin
        SetBounds( 6, Top, Width, Height );
        Visible := True;
        if nMaxLabelWidth < Width then
          nMaxLabelWidth := Width;
      end;
  end;

  // TODO: 对其它编辑框改变大小
  for n := 0 to sbxInfoList.ComponentCount - 1 do
  begin
    cmpContained := sbxInfoList.Components[ n ];
    if not ( cmpContained is TLabel ) then
      with TControl( cmpContained ) do
      begin
        if cmpContained is TCustomComboBox then
          SetBounds( 12 + nMaxLabelWidth, Top, sbxInfoList.ClientWidth -
            nMaxLabelWidth - 43, Height )
        else if cmpContained is TRzCustomButton then
          SetBounds( sbxInfoList.ClientWidth - 27, Top, 21, 21 )
        else
          SetBounds( 12 + nMaxLabelWidth, Top, sbxInfoList.ClientWidth -
            nMaxLabelWidth - 20, Height );
        Visible := True;
      end;
  end;
end;

procedure TfrmTableViewEditorForm.SetConnection( const Value: _Connection );
begin
  m_cn := Value;
end;

procedure TfrmTableViewEditorForm.SetDBListView( const Value: TJSDBListView );
begin
  m_lvwDBList := Value;
  if m_lvwDBList <> nil then
    with m_lvwDBList do
    begin
      EditOnRowClick := False;
      FullDrag := True;
      FillLastColumn := False;
      GridLines := True;
      HideSelection := False;
      OwnerData := True;
      RowSelect := True;
      ShowHint := True;

      m_eOldDVWColumnClick := OnColumnClick;
      m_eOldDVWDataStateChange := OnDataStateChange;
      m_eOldDVWSelectItem := OnSelectItem;
      OnColumnClick := pDVWColumnClick;
      OnDataStateChange := pDVWDataStateChange;
      OnSelectItem := pDVWSelectItem;
      OnInfoTip := pDVWInfoTip;
      //OnColumnCreate := pDVWColumnCreate;
    end;
end;

procedure TfrmTableViewEditorForm.SetDeleteInfoProcName( const Value: string );
begin
  m_sDeleteInfoProcName := Value;
end;

procedure TfrmTableViewEditorForm.SetKeyFieldName( const Value: string );
begin
  m_sKeyFieldName := Value;
end;

procedure TfrmTableViewEditorForm.SetLoadCondition( const Value: string );
begin
  m_sLoadCondition := Value;
  if m_sLoadCondition <> EmptyStr then
  begin
    if SameText( LeftBStr( m_sLoadCondition, 6 ), 'WHERE ' ) then
      m_sLoadCondition := ' ' + m_sLoadCondition
    else if not SameText( LeftBStr( m_sLoadCondition, 7 ), ' WHERE ' ) then
      m_sLoadCondition := ' WHERE ' + m_sLoadCondition;
  end;
end;

procedure TfrmTableViewEditorForm.SetOnButtonAddClick(
  const Value: TNotifyEvent );
begin
  m_eBtnAddClick := Value;
end;

procedure TfrmTableViewEditorForm.SetOnButtonCancelClick(
  const Value: TNotifyEvent );
begin
  m_eBtnCancelClick := Value;
end;

procedure TfrmTableViewEditorForm.SetOnButtonDeleteClick(
  const Value: TNotifyEvent );
begin
  m_eBtnDeleteClick := Value;
end;

procedure TfrmTableViewEditorForm.SetOnButtonEditClick(
  const Value: TNotifyEvent );
begin
  m_eBtnEditClick := Value;
end;

procedure TfrmTableViewEditorForm.SetOnButtonSaveClick(
  const Value: TNotifyEvent );
begin
  m_eBtnSaveClick := Value;
end;

procedure TfrmTableViewEditorForm.SetSaveCheckNull( const Value: Boolean );
begin
  m_bSaveCheckNull := Value;
end;

procedure TfrmTableViewEditorForm.SetSaveCheckRepeated( const Value: Boolean );
begin
  m_bSaveCheckRepeated := Value;
end;

procedure TfrmTableViewEditorForm.SetSaveInfoProcName( const Value: string );
begin
  m_sSaveInfoProcName := Value;
end;

procedure TfrmTableViewEditorForm.SetShowKeyField( const Value: Boolean );
begin
  m_bShowKeyField := Value;
end;

procedure TfrmTableViewEditorForm.SetShowListItemInfoHint( const Value: Boolean
  );
begin
  m_bShowListItemInfoHint := Value;
end;

procedure TfrmTableViewEditorForm.SetShowSpecial( const Value: Boolean );
begin
  m_bShowSpecial := Value;
end;

procedure TfrmTableViewEditorForm.SetSpecialEnabled( const Value: Boolean );
begin
  m_bSpecialEnabled := Value;
end;

procedure TfrmTableViewEditorForm.SetTableViewName( const Value: string );
begin
  m_sTableViewName := Value;
end;

procedure TfrmTableViewEditorForm.StartSurf( const bClearAll: Boolean = True );
begin
  pInitEditButtonEnabled;
  pInitInfoPanel( bClearAll );
  pFillListView( bClearAll );

  // TODO: 如果有项目则显示项目
  if ( DBListView <> nil ) and ( DBListView.Items.Count > 0 ) then
    DBListView.Items[ 0 ].Selected := True;
end;

end.

⌨️ 快捷键说明

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