首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有Wijmo5更改数据的Angular8柔性网格-选择恢复已编辑文本列中的更改文本

具有Wijmo5更改数据的Angular8柔性网格-选择恢复已编辑文本列中的更改文本
EN

Stack Overflow用户
提问于 2020-03-06 16:09:18
回答 1查看 986关注 0票数 0

我正在使用wijmo 5柔性网格与角8,并试图创建一个内联编辑网格与编辑按钮在每一行。每一行都有一个datamap列和一个文本列,当单击该特定行的Edit按钮时,这两个列都应该是可编辑的。单击“编辑”按钮后,首先编辑文本字段(员工姓名)。之后,当我更改datamap下拉列表(Department)的选择时,文本字段中的更改文本将恢复为原始文本。在“部门数据”字段中选择更改后,如何将更改后的文本保留在“员工姓名”文本字段中?代码在链接:https://stackblitz.com/edit/angular-g9wk57?file=app%2Fapp.component.ts

app.component.ts

代码语言:javascript
复制
import { Component, ViewChild } from '@angular/core';
import { CollectionView, ObservableArray } from 'wijmo/wijmo';
import * as wjCore from 'wijmo/wijmo';
import * as wjInput from 'wijmo/wijmo.input';
import * as wjGrid from 'wijmo/wijmo.grid';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  private _currentEditItem: any = null;

  public data : any;
  public deptList: any = [
    {'deptId':'D1', 'deptName':'Accounts'},
    {'deptId':'D2', 'deptName':'Development'},
    {'deptId':'D3', 'deptName':'HR'}
    ];

    public empList: any = [
      {'empId':'E1', 'empName':'AA', 'deptId':'D3'},
      {'empId':'E2', 'empName':'BB', 'deptId':'D2'},
      {'empId':'E3', 'empName':'CC', 'deptId':'D1'},
      {'empId':'E4', 'empName':'DD', 'deptId':'D2'}
    ];

  @ViewChild('flex') flex:wjGrid.FlexGrid;

public deptMap = new wjGrid.DataMap(this.deptList, 'deptId', 'deptName');
    constructor() {
      this.data = new wjCore.CollectionView(this.empList);
  }  

initializeGrid(flex: wjGrid.FlexGrid) {
    flex.rows.defaultSize = 40;
    // custom formatter to paint buttons and editors
    flex.formatItem.addHandler((s: wjGrid.FlexGrid, e: wjGrid.FormatItemEventArgs) => {
      if (e.panel == s.cells) {
        let col = s.columns[e.col],
          item = s.rows[e.row].dataItem;
        if (item == this._currentEditItem) {
          // create editors and buttons for the item being edited
          switch (col.binding) {
            case 'buttons':
              e.cell.innerHTML = document.getElementById('tplBtnEditMode').innerHTML;
              e.cell['dataItem'] = item;
              break;            
            case 'empName':
              e.cell.innerHTML = '<input class="form-control" ' +
                'id="' + col.binding + '" ' +
                'value="' + s.getCellData(e.row, e.col, true) + '"/>';
              break;
          }
        } else {
          // create buttons for items not being edited
          switch (col.binding) {
            case 'buttons':
              e.cell.innerHTML = document.getElementById('tplBtnViewMode').innerHTML;
              e.cell['dataItem'] = item;
              break;
          }
        }
      }
    });

    // handle button clicks
    flex.addEventListener(flex.hostElement, 'click', (e: MouseEvent) => {
      let targetBtn: HTMLButtonElement;
      if (e.target instanceof HTMLButtonElement) {
        targetBtn = e.target;
      }
      // else if (e.target instanceof HTMLSpanElement && e.target.classList.contains('glyphicon')) {
      //   targetBtn = e.target.parentElement as HTMLButtonElement;
      // }
      if (targetBtn) {
        // get button's data item
        let item = wjCore.closest(targetBtn, '.wj-cell')['dataItem'];
        // handle buttons
        switch (targetBtn.id) {
          // start editing this item
          case 'btnEdit':
            this._editItem(item);
            break;
          // remove this item from the collection
          // case 'btnDelete':
          //   (<wjCore.CollectionView>flex.collectionView).remove(item);
          //   break;
          // commit edits
          case 'btnOK':
            this._commitEdit();
            break;
          // cancel edits
          case 'btnCancel':
            this._cancelEdit();
            break;
        }
      }
      e.preventDefault();
    });

    // exit edit mode when scrolling the grid or losing focus
    flex.scrollPositionChanged.addHandler(this._cancelEdit.bind(this));
    flex.lostFocus.addHandler(this._cancelEdit.bind(this));
  }

  private _editItem(item: any) {
    this._cancelEdit();
    this._currentEditItem = item;
    this.flex.invalidate();
  }

  private _commitEdit() {
    if (this._currentEditItem) {
      this.flex.columns.forEach((col: any) => {
        let input = <HTMLInputElement>this.flex.hostElement.querySelector('#' + col.binding);
        if (input) {
          let value = wjCore.changeType(input.value, col.dataType, col.format);
          if (wjCore.getType(value) == col.dataType) {
            this._currentEditItem[col.binding] = value;
          }
        }
      });
    }

    console.log(this._currentEditItem);
    this._currentEditItem = null;
    this.flex.invalidate();
  }

  private _cancelEdit() {
    if (this._currentEditItem) {
      this._currentEditItem = null;
      this.flex.invalidate();
    }
  }

}

app.component.html

代码语言:javascript
复制
<div class="header">
    <div class="container">
        <h1>
            DataMap
        </h1>
    </div>
</div>

<!-- content -->
<div class="container">
    <div>
        <wj-flex-grid #flex [itemsSource]="data"
            [headersVisibility]="'Column'" (initialized)="initializeGrid(flex)">
            <wj-flex-grid-column [header]="'Employee Name'" [binding]="'empName'" [width]="'4*'">
            </wj-flex-grid-column>
            <wj-flex-grid-column [header]="'Department'" [binding]="'deptId'" [width]="'4*'" [dataMap]="deptMap">
            </wj-flex-grid-column>
            <wj-flex-grid-column [header]="'Actions'" [binding]="'buttons'" [width]="'3*'"></wj-flex-grid-column>
        </wj-flex-grid>

        <!-- template for buttons on items in view mode -->
        <div id="tplBtnViewMode" style="display:none">
            <button id="btnEdit" class="btn btn-default btn-sm">
        Edit
    </button>
        </div>

        <!-- template for buttons on items in edit mode -->
        <div id="tplBtnEditMode" style="display:none">
            <button id="btnOK" class="btn btn-primary btn-sm">
        OK
    </button>
            <button id="btnCancel" class="btn btn-warning btn-sm">
        Cancel
    </button>
        </div>
    </div>
</div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-09 22:22:04

我使用ComboBox而不是DataMap来编辑值wijmo论坛

样本:StackBlitz

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60567785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档