自定义单元格内容

此示例描述了自定义单元格内容导出模式。

FlexGridPdfConverter有两种导出模式可供选择,它由IFlexGridDrawSettings定义。 customCellContent boolean属性:

wijmo.grid.pdf.FlexGridPdfConverter.export($scope.ctx.grid, 'FlexGrid.pdf', {
    customCellContent: true
});

在第一个模式(customCellContent = false)(默认值)中,导出网格的单元格外观由IFlexGridDrawSettings。styles 属性确定,单元格值使用 FlexGrid.getCellData 检索方法。

在第二种模式(customCellContent = true)中,直接从HTML DOM中检索单元格的外观和内容。 FlexGridPdfConverter获取元素,该元素是导出单元格的DOM表示形式; element.textContent属性定义单元格内容,其样式设置(背景颜色,前景色,边框颜色和字体)使用window.getComputedStyle(element)计算。

除了在自定义单元格内容模式下导出的网格外观接近原始外,如果FlexGrid包含Angular单元格模板,此模式也很有用。由于DOM评估,此模式的性能较低。

重要提示:要正确实例化单元格模板,在Angular 1的情况下,整个导出代码应在Angular摘要之外运行(通常应在window.setTimeout中完成)。

在此示例中,使用 customCellContent 属性的不同值导出相同的网格。以下差异在两个第一个网格之间是显而易见的:

  • 由于网格中使用的自定义单元格格式,“Country”列的单元格内容看起来不同。
  • 使用非customCellContent模式导出的网格没有样式,但第二个导出的网格确实(IFlexGridDrawSettings。styles 属性为清晰起见留空)。

第三个按钮显示使用IFlexGridDrawSettings导出带有标志图像的网格的能力。formatItem callback 和 PdfDocument API。这项技术将在下一个主题中详细解释。

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; import * as grid from '@grapecity/wijmo.grid'; import * as gridPdf from '@grapecity/wijmo.grid.pdf'; import { getData } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { var exportSettings = { maxPages: 10, documentOptions: { header: { declarative: { text: '\t&[Page]\\&[Pages]' } }, footer: { declarative: { text: '\t&[Page]\\&[Pages]' } } } }; // let flexGrid = new grid.FlexGrid('#flexGrid', { autoGenerateColumns: false, headersVisibility: grid.HeadersVisibility.All, selectionMode: grid.SelectionMode.ListBox, columns: [ { header: 'ID', binding: 'id' }, { header: 'Country', binding: 'country', width: "*", isReadOnly: true }, { header: 'Product', binding: 'product' }, { header: 'Color', binding: 'color' } ], formatItem: (sender, e) => { if (e.panel === sender.cells && sender.columns[e.col].binding === 'country') { let data = e.panel.rows[e.row].dataItem; // e.cell.textContent = ''; // // add flag image let image = document.createElement('img'); image.src = `resources/${data['country']}.png`; e.cell.appendChild(image); // // add non-breaking space e.cell.appendChild(document.createTextNode('\u00A0')); // // add text e.cell.appendChild(document.createTextNode(`${data['country']}-${data['capital']}`)); } }, itemsSource: getData(5) }); // // document.querySelector('#btnExport1').addEventListener('click', () => { exportSettings.customCellContent = false; gridPdf.FlexGridPdfConverter.export(flexGrid, 'FlexGrid.pdf', exportSettings); }); // document.querySelector('#btnExport2').addEventListener('click', () => { exportSettings.customCellContent = true; gridPdf.FlexGridPdfConverter.export(flexGrid, 'FlexGrid.pdf', exportSettings); }); // document.querySelector('#btnExport3').addEventListener('click', () => { exportSettings.customCellContent = false; exportSettings.formatItem = formatCountryCell; gridPdf.FlexGridPdfConverter.export(flexGrid, 'FlexGrid.pdf', exportSettings); exportSettings.formatItem = null; }); // function formatCountryCell(args) { // if this is a regular grid cell... if (args.panel.cellType === grid.CellType.Cell) { // ... that belongs to the 'country' column if (args.panel.columns[args.col].binding === 'country') { let // get cell with custom content produced by a cell template or grid.formatItem handler cell = args.getFormattedCell(), // bound rectangle of cell's content area contentRect = args.contentRect, // construct flag image url based on country name passed in args.data image = args.canvas.openImage(`resources/${args.data}.png`), imageTop = contentRect.top + (contentRect.height - image.height) / 2; // draw flag image args.canvas.drawImage(image, contentRect.left, imageTop); // Draw custom cell text retrieved using the cell.textContent property, // right to the image and in the args.textTop vertical position. The latter // works because we draw text using default cell font. args.canvas.drawText(cell.textContent.trim(), contentRect.left + image.width + 3, args.textTop); // cancel standard cell content drawing args.cancel = true; } } } } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Custom cell content</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <!-- Export button --> <div class="row"> <button class="btn btn-default" id="btnExport1">Export (customCellContent = false)</button> <button class="btn btn-default" id="btnExport2">Export (customCellContent = true)</button> <button class="btn btn-default" id="btnExport3">Export (customCellContent = false + formatItem callback)</button> </div> <!-- FlexGrid --> <div id="flexGrid" class="grid"></div> </div> </body> </html> export function getData(count) { // data used to generate random items const countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece']; const capitals = ['Washington', 'Berlin', 'London', 'Tokyo', 'Rome', 'Athens']; const products = ['Widget', 'Gadget', 'Doohickey']; const colors = ['Orange', 'White', 'Red', 'Green', 'Blue']; // let data = []; // // add count items for (let i = 0; i < count; i++) { // constants used to create data items let countryId = Math.floor(Math.random() * countries.length), productId = Math.floor(Math.random() * products.length), colorId = Math.floor(Math.random() * colors.length); // // create the item let item = { id: i, country: countries[countryId], capital: capitals[countryId], product: products[productId], color: colors[colorId] }; // // add the item to the list data.push(item); } // return data; } body { margin-bottom: 24px; } .grid { margin-top: 6px; height: 200px; } .btn { margin: 2px 2px 2px 0; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as grid from '@grapecity/wijmo.grid'; import * as gridPdf from '@grapecity/wijmo.grid.pdf'; // import { Component, Inject, enableProdMode, NgModule, ViewChild } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { DataService } from './app.data'; import { WjGridModule } from '@grapecity/wijmo.angular2.grid'; // @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { data: any[]; @ViewChild('flexGrid') flexGrid: grid.FlexGrid; // constructor(@Inject(DataService) private dataService: DataService) { this.data = dataService.getData(5); } // export() { gridPdf.FlexGridPdfConverter.export(this.flexGrid, 'FlexGrid.pdf', { customCellContent: false }); } // export2() { gridPdf.FlexGridPdfConverter.export(this.flexGrid, 'FlexGrid.pdf', { customCellContent: true }); } // export3() { gridPdf.FlexGridPdfConverter.export(this.flexGrid, 'FlexGrid.pdf', { customCellContent: false, formatItem: this.formatCountryCell }); } // private formatCountryCell(args: gridPdf.PdfFormatItemEventArgs) { // if this is a regular grid cell... if (args.panel.cellType === grid.CellType.Cell) { // ... that belongs to the 'country' column if (args.panel.columns[args.col].binding === 'country') { let // get cell with custom content produced by a cell template or grid.formatItem handler cell = args.getFormattedCell(), // bound rectangle of cell's content area contentRect = args.contentRect, // construct flag image url based on country name passed in args.data image = args.canvas.openImage(`resources/${args.data}.png`), imageTop = contentRect.top + (contentRect.height - image.height) / 2; // draw flag image args.canvas.drawImage(image, contentRect.left, imageTop); // Draw custom cell text retrieved using the cell.textContent property, // right to the image and in the args.textTop vertical position. The latter // works because we draw text using default cell font. args.canvas.drawText(cell.textContent.trim(), contentRect.left + image.width + 3, args.textTop); // cancel standard cell content drawing args.cancel = true; } } } } // @NgModule({ imports: [WjGridModule, BrowserModule], declarations: [AppComponent], providers: [DataService], bootstrap: [AppComponent] }) export class AppModule { } // enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Custom cell content</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html> <div class="container-fluid"> <!-- Export button --> <button class="btn btn-default" (click)="export()">Export (customCellContent = false)</button> <button class="btn btn-default" (click)="export2()">Export (customCellContent = true)</button> <button class="btn btn-default" (click)="export3()">Export (customCellContent = false + formatItem callback)</button> <!-- FlexGrid --> <wj-flex-grid class="grid" #flexGrid [autoGenerateColumns]="false" headersVisibility="All" selectionMode="ListBox" [itemsSource]="data"> <wj-flex-grid-column header="ID" binding="id"></wj-flex-grid-column> <wj-flex-grid-column header="Country" binding="country" width="*" isReadOnly="true"> <ng-template wjFlexGridCellTemplate cellType="Cell" let-item="item"> <img src="resources/{{item.country}}.png" /> {{item.country}} - {{item.capital}} </ng-template> </wj-flex-grid-column> <wj-flex-grid-column header="Product" binding="product"></wj-flex-grid-column> <wj-flex-grid-column header="Color" binding="color"></wj-flex-grid-column> </wj-flex-grid> </div> import { Injectable } from '@angular/core'; // @Injectable() export class DataService { // getData(count: number) { // data used to generate random items const countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece']; const capitals = ['Washington', 'Berlin', 'London', 'Tokyo', 'Rome', 'Athens']; const products = ['Widget', 'Gadget', 'Doohickey']; const colors = ['Orange', 'White', 'Red', 'Green', 'Blue']; // let data = []; // // add count items for (let i = 0; i < count; i++) { // constants used to create data items let countryId = Math.floor(Math.random() * countries.length), productId = Math.floor(Math.random() * products.length), colorId = Math.floor(Math.random() * colors.length); // // create the item let item = { id: i, country: countries[countryId], capital: capitals[countryId], product: products[productId], color: colors[colorId] }; // // add the item to the list data.push(item); } // return data; } } body { margin-bottom: 24px; } .grid { margin-top: 6px; height: 200px; } .btn { margin: 2px 2px 2px 0; } <template> <div class="container-fluid"> <!-- Export button --> <button class="btn btn-default" v-on:click="export1">Export (customCellContent = false)</button> <button class="btn btn-default" v-on:click="export2">Export (customCellContent = true)</button> <button class="btn btn-default" v-on:click="export3">Export (customCellContent = false + formatItem callback)</button> <!-- FlexGrid --> <wj-flex-grid class="grid" :autoGenerateColumns=false headersVisibility="All" selectionMode="ListBox" :formatItem="formatItem" :itemsSource="data" :initialized="initializeGrid"> <wj-flex-grid-column header="ID" binding="id"></wj-flex-grid-column> <wj-flex-grid-column header="Country" binding="country" width="*" :isReadOnly=true></wj-flex-grid-column> <wj-flex-grid-column header="Product" binding="product"></wj-flex-grid-column> <wj-flex-grid-column header="Color" binding="color"></wj-flex-grid-column> </wj-flex-grid> </div> </template> <script> import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import Vue from 'vue'; import { getData } from './data'; import { WjGridModule } from '@grapecity/wijmo.vue2.grid'; import * as grid from '@grapecity/wijmo.grid'; import * as gridPdf from '@grapecity/wijmo.grid.pdf'; new Vue({ el: '#app', data: { data: getData(5) }, methods:{ initializeGrid(ctl) { this.flexGrid = ctl; }, formatItem(sender, e) { if (e.panel === sender.cells && sender.columns[e.col].binding === 'country') { let data = (e.panel.rows[e.row]).dataItem; // e.cell.textContent = ''; // // add flag image let image = document.createElement('img'); image.src = `resources/${data['country']}.png`; e.cell.appendChild(image); // // add non-breaking space e.cell.appendChild(document.createTextNode('\u00A0')); // // add text e.cell.appendChild(document.createTextNode(`${data['country']}-${data['capital']}`)); } }, export1() { gridPdf.FlexGridPdfConverter.export(this.flexGrid, 'FlexGrid.pdf', { customCellContent: false }); }, export2() { gridPdf.FlexGridPdfConverter.export(this.flexGrid, 'FlexGrid.pdf', { customCellContent: true }); }, export3() { gridPdf.FlexGridPdfConverter.export(this.flexGrid, 'FlexGrid.pdf', { customCellContent: false, formatItem: this.formatCountryCell }); }, formatCountryCell(args) { // if this is a regular grid cell... if (args.panel.cellType === grid.CellType.Cell) { // ... that belongs to the 'country' column if (args.panel.columns[args.col].binding === 'country') { let // get cell with custom content produced by a cell template or grid.formatItem handler cell = args.getFormattedCell(), // bound rectangle of cell's content area contentRect = args.contentRect, // construct flag image url based on country name passed in args.data image = args.canvas.openImage(`resources/${args.data}.png`), imageTop = contentRect.top + (contentRect.height - image.height) / 2; // draw flag image args.canvas.drawImage(image, contentRect.left, imageTop); // Draw custom cell text retrieved using the cell.textContent property, // right to the image and in the args.textTop vertical position. The latter // works because we draw text using default cell font. args.canvas.drawText(cell.textContent.trim(), contentRect.left + image.width + 3, args.textTop); // cancel standard cell content drawing args.cancel = true; } } } } }); </script> <style> body { margin-bottom: 24px; } .grid { margin-top: 6px; height: 200px; } .btn { margin: 2px 2px 2px 0; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Custom cell content</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); </script> </head> <body> <div id="app"> </div> </body> </html> export function getData(count) { // data used to generate random items const countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece']; const capitals = ['Washington', 'Berlin', 'London', 'Tokyo', 'Rome', 'Athens']; const products = ['Widget', 'Gadget', 'Doohickey']; const colors = ['Orange', 'White', 'Red', 'Green', 'Blue']; // let data = []; // // add count items for (let i = 0; i < count; i++) { // constants used to create data items let countryId = Math.floor(Math.random() * countries.length), productId = Math.floor(Math.random() * products.length), colorId = Math.floor(Math.random() * colors.length); // // create the item let item = { id: i, country: countries[countryId], capital: capitals[countryId], product: products[productId], color: colors[colorId] }; // // add the item to the list data.push(item); } // return data; } import "@grapecity/wijmo.styles/wijmo.css"; import "bootstrap.css"; import "./app.css"; // import * as React from 'react'; import * as ReactDOM from 'react-dom'; import * as wjcGrid from "@grapecity/wijmo.react.grid"; import * as wjGrid from "@grapecity/wijmo.grid"; import * as gridPdf from "@grapecity/wijmo.grid.pdf"; import { getData } from "./data"; class App extends React.Component { constructor(props) { super(props); this.state = { data: getData(5) }; } render() { return <div className="container-fluid"> <button className="btn btn-default" onClick={this.export1.bind(this)}>Export (customCellContent = false)</button> <button className="btn btn-default" onClick={this.export2.bind(this)}>Export (customCellContent = true)</button> <button className="btn btn-default" onClick={this.export3.bind(this)}>Export (customCellContent = false + formatItem callback)</button> <wjcGrid.FlexGrid className="grid" autoGenerateColumns={false} headersVisibility="All" selectionMode="ListBox" itemsSource={this.state.data} formatItem={this.formatItem.bind(this)} initialized={this.initializeGrid.bind(this)}> <wjcGrid.FlexGridColumn header="ID" binding="id"></wjcGrid.FlexGridColumn> <wjcGrid.FlexGridColumn header="Country" binding="country" width="*" isReadOnly={true}></wjcGrid.FlexGridColumn> <wjcGrid.FlexGridColumn header="Product" binding="product"></wjcGrid.FlexGridColumn> <wjcGrid.FlexGridColumn header="Color" binding="color"></wjcGrid.FlexGridColumn> </wjcGrid.FlexGrid> </div>; } initializeGrid(ctl) { this.flexGrid = ctl; } formatItem(sender, e) { if (e.panel === sender.cells && sender.columns[e.col].binding === 'country') { let data = (e.panel.rows[e.row]).dataItem; // e.cell.textContent = ''; // // add flag image let image = document.createElement('img'); image.src = `resources/${data['country']}.png`; e.cell.appendChild(image); // // add non-breaking space e.cell.appendChild(document.createTextNode('\u00A0')); // // add text e.cell.appendChild(document.createTextNode(`${data['country']}-${data['capital']}`)); } } export1() { gridPdf.FlexGridPdfConverter.export(this.flexGrid, 'FlexGrid.pdf', { customCellContent: false }); } export2() { gridPdf.FlexGridPdfConverter.export(this.flexGrid, 'FlexGrid.pdf', { customCellContent: true }); } export3() { gridPdf.FlexGridPdfConverter.export(this.flexGrid, 'FlexGrid.pdf', { customCellContent: false, formatItem: this.formatCountryCell }); } formatCountryCell(args) { // if this is a regular grid cell... if (args.panel.cellType === wjGrid.CellType.Cell) { // ... that belongs to the 'country' column if (args.panel.columns[args.col].binding === 'country') { let // get cell with custom content produced by a cell template or grid.formatItem handler cell = args.getFormattedCell(), // bound rectangle of cell's content area contentRect = args.contentRect, // construct flag image url based on country name passed in args.data image = args.canvas.openImage(`resources/${args.data}.png`), imageTop = contentRect.top + (contentRect.height - image.height) / 2; // draw flag image args.canvas.drawImage(image, contentRect.left, imageTop); // Draw custom cell text retrieved using the cell.textContent property, // right to the image and in the args.textTop vertical position. The latter // works because we draw text using default cell font. args.canvas.drawText(cell.textContent.trim(), contentRect.left + image.width + 3, args.textTop); // cancel standard cell content drawing args.cancel = true; } } } } ReactDOM.render(<App />, document.getElementById('app')); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Grapecity Wijmo OLAP Pivot Chart Overview</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div id="app"></div> </body> </html> body { margin-bottom: 24px; } .grid { margin-top: 6px; height: 200px; } .btn { margin: 2px 2px 2px 0; } export function getData(count) { // data used to generate random items const countries = ['US', 'Germany', 'UK', 'Japan', 'Italy', 'Greece']; const capitals = ['Washington', 'Berlin', 'London', 'Tokyo', 'Rome', 'Athens']; const products = ['Widget', 'Gadget', 'Doohickey']; const colors = ['Orange', 'White', 'Red', 'Green', 'Blue']; // let data = []; // // add count items for (let i = 0; i < count; i++) { // constants used to create data items let countryId = Math.floor(Math.random() * countries.length), productId = Math.floor(Math.random() * products.length), colorId = Math.floor(Math.random() * colors.length); // // create the item let item = { id: i, country: countries[countryId], capital: capitals[countryId], product: products[productId], color: colors[colorId] }; // // add the item to the list data.push(item); } // return data; }