我使用的是棱角版的DevExtreme PivotGrid。这是我的密码
import { Component, OnInit, ViewChild, HostListener } from '@angular/core';
import { Service, Sale } from './pivotgrid.service';
import PivotGridDataSource from 'devextreme/ui/pivot_grid/data_source';
import { DxPivotGridComponent } from 'devextreme-angular';
@Component({
selector: 'app-pivotgridsample',
templateUrl: './pivotgridsample.component.html',
styleUrls: ['./pivotgridsample.component.scss'],
providers: [Service]
})
export class PivotgridsampleComponent implements OnInit {
pivotGridDataSource: any;
drillDownDataSource: any;
originalData: Sale[];
@ViewChild("sales", { static: true }) salesPivotGrid: DxPivotGridComponent;
constructor(private service: Service) {
this.originalData = this.service.getSales();
this.pivotGridDataSource = new PivotGridDataSource({
fields: [{
caption: "Region",
width: 120,
dataField: "region",
area: "row"
}, {
caption: "City",
dataField: "city",
width: 150,
area: "row"
}, {
dataField: "date",
dataType: "date",
area: "column"
}, {
groupName: "date",
groupInterval: "year",
expanded: true
}, {
groupName: "date",
groupInterval: "month",
visible: false
}, {
caption: "Total",
dataField: "amount",
dataType: "number",
summaryType: "sum",
format: "currency",
area: "data"
}, {
caption: "Running Total",
dataField: "amount",
dataType: "number",
summaryType: "sum",
format: "currency",
area: "data",
runningTotal: "row",
allowCrossGroupCalculation: true
}],
store: service.getSales()
});
}
onPivotCellClick(e) {
if (e.area == "data") {
let cellValue = e.cell.value == null ? "" : e.cell.value;
let cell = e.cellElement;
this.drillDownDataSource = this.pivotGridDataSource.createDrillDownDataSource(e.cell);
// Check if there is already input attched to cell
if (cell.childNodes.length > 0) {
// Clear Cell content
cell.innerHTML = '';
// Append Textbox
cell.innerHTML = '<input type="text" class="editable" value="' + cellValue + '" style="width:100%;">'
var textBox = cell.querySelector("input.editable");
textBox.focus();
this.drillDownDataSource.load();
let groupData = this.drillDownDataSource._items;
let oData = this.originalData;
let pivotTable = this.salesPivotGrid.instance;
textBox.onkeypress = function (event) {
var keycode = (event.keyCode ? event.keyCode : event.which);
if (keycode == '13') {
var newValue = textBox.value;
cell.innerHTML = "<span>" + parseInt(newValue) + "</span>";
textBox.remove();
console.log(oData);
for (var i = 0; i < oData.length; i++) {
for (var j = 0; j < groupData.length; j++) {
if (oData[i].id == groupData[j].id) {
oData[i].amount = newValue / groupData.length;
//console.log(drillDownDataSource._items[i].amount);
}
}
}
this.setDataSource(oData)
//pivotTable.option("store", oData);
//pivotTable.dataSource.store = oData;
}
return true;
}
}
}
}
setDataSource(data:any){
alert('can be called');
}
ngOnInit() {
}
}这里,我使用onPivotCellClick of PivotGrid来处理我的自定义需求。基本上,我将一个输入附加到网格单元格上,用于内联编辑,还附加了一个键-按事件。
因为,它是在普通的JavaScript中,所以我无法调用角方法(即,从我用JavaScript编写的输入按键回调函数中调用JavaScript)。
有什么办法我能做到吗?
发布于 2019-07-04 06:32:58
这个问题不是关于普通的JavaScript,而是this在JavaScript中的范围。
声明引用this的变量,并在回调函数中调用组件的方法时使用该变量。
onPivotCellClick(e) {
const that = this;
...
textBox.onkeypress = function (event) {
...
that.setDataSource(oData);
...也可以使用箭头函数按您的意愿使用this关键字。
onPivotCellClick(e) {
textBox.onkeypress = (event) => {
...
this.setDataSource(oData);
...https://stackoverflow.com/questions/56881782
复制相似问题