Angular 7和JQWidgets-从另一个组件导出网格数据

时间:2019-03-25 17:23:41

标签: angular angular7 jqwidget

我正在尝试Angular 7和JQWidgets。我正在处理Grid组件,并想从另一个名为settings的组件中导出Grid的数据。 我进行了演示(可用here),并创建了以下组件:

import { Component, ElementRef, Input, AfterViewInit, ViewChild} from '@angular/core';
import { jqxDropDownListComponent } from 'jqwidgets-scripts/jqwidgets-ts/angular_jqxdropdownlist';
import { jqxGridComponent } from 'jqwidgets-scripts/jqwidgets-ts/angular_jqxgrid';

@Component({
  selector: 'app-mydemo',
  templateUrl: './mydemo.component.html'
})

export class MydemoComponent{
    @ViewChild('myGrid') myGrid: jqxGridComponent;
    @ViewChild('myDropDownList') myDropDownList: jqxDropDownListComponent;

    exportFiletype: any;

    constructor() { }

    exportBtnOnClick() {
        this.exportFiletype = this.myDropDownList.getSelectedItem().value;
        switch (this.exportFiletype) {
            case 'Excel':
                this.myGrid.exportdata('xls', 'jqxGrid', true, null, true, 'https://jqwidgets.com/export_server/dataexport.php');
                break;
            case 'CSV':
                this.myGrid.exportdata('csv', 'jqxGrid', true, null, true, 'https://jqwidgets.com/export_server/dataexport.php');
                break;
        };
    };
}

我的问题是this.myGrid引用了其他组件中的Grid。我该如何直接参考?

1 个答案:

答案 0 :(得分:1)

根据新信息更新:-

使用https://angular.io/guide/component-interaction模型之一在组件之间进行交互。

以下是模板变量的示例。

主要组件

//showing only html

<my-grid #myGrid><my-grid>
<my-dropdown [grid]="myGrid.jqxGrid"><my-dropdown>

组件A(我的下拉列表)

使用onSelect,您也可以传入myDropDownList引用,这样您就可以传递任何引用

绑定到jqxDropDownList的select事件。

import { Component } from "@angular/core";

@Component({
    selector: "my-dropdown",
    template: `
        <jqxDropDownList #myDropDownList (onSelect)="exportTo($event)"
            [width]="200" [height]="25" [source]="source" [selectedIndex]="1">
        </jqxDropDownList>
    `
})

export class MyDropDown{
    @Input() grid: jqxGridComponent
    exportTo(event: any): void 
    {
        if (this.grid) {
            this.grid.doSomething()
        }
    }

    source: string[] =
    [
        'Affogato',
        'Americano',
        'Bicerin',
        'Breve'
    ];
}

组件B-网格组件

template: `
<jqxGrid #jqxGrid [theme]="'material'"
    [width]="getWidth()" [source]="dataAdapter" [columns]="columns"
    [pageable]="true" [autoheight]="true" [sortable]="true" 
    [altrows]="true"  [enabletooltips]="true"  [editable]="true" 
    [selectionmode]="'multiplecellsadvanced'" [columngroups]="columngroups">
</jqxGrid>
`
export class MyGrid {
     @ViewChild('jqxGrid') jqxGrid: jqxGridComponent;
}