CollectionView:JSON日付の読み込み

JSONは日付をサポートしていないので、サーバーからデータを読み込むときにそれらを書式設定する必要があるかもしれません。このサンプルは、日付を解析する前後の値をFlexGridに表示する方法を示します。これはJSON.parseメソッドを使用して、日付のように見える文字列をDateオブジェクトに変換します。

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import * as grid from '@grapecity/wijmo.grid'; import { getData } from './data'; // document.readyState === 'complete' ? init() : window.onload = init; // function init() { // Start with some data encoded as JSON (as it would arrive from a server) let json = getData(); // // Decode the data // No special parsing for dates, the date field will contain strings let dataBad = JSON.parse(json); // // Show the bad data in a grid let theGridBad = new grid.FlexGrid('#theGridBad', { autoGenerateColumns: false, columns: [ { binding: 'id', header: 'ID', width: 80 }, { binding: 'country', header: '国' }, { binding: 'sales', header: '売上', width: 80 }, { binding: 'expenses', header: '費用', width: 80 }, { binding: 'date', header: '日付' }, ], itemsSource: dataBad }); // // Decode the data // Use with a Date reviver to restore date fields let dataGood = JSON.parse(json, (key, value) => { if (typeof value === 'string') { // Parse dates saved as JSON-strings let m = value.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/); if (m) { return new Date(Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6])); } // // Parse dates saved as OData-style strings m = value.match(/^\/Date\((\d+)\)$/); if (m) { return new Date(parseInt(m[1])); } } return value; }); // // Show the good data in a grid let theGridGood = new grid.FlexGrid('#theGridGood', { autoGenerateColumns: false, columns: [ { binding: 'id', header: 'ID', width: 80 }, { binding: 'country', header: '国' }, { binding: 'sales', header: '売上', width: 80 }, { binding: 'expenses', header: '費用', width: 80 }, { binding: 'date', header: '日付' }, ], itemsSource: dataGood }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Loading JSON Dates</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"> <p> 次の例で、「日付」列に文字列がどのように含まれているかに注意してください。 </p> <div id="theGridBad"></div> <p> 同じデータを表示するグリッドを示します。今回は、日付値が正常に解析/設定されています。 </p> <div id="theGridGood"></div> </div> </body> </html> export function getData() { let json = '[' + '{"id":0,"country":"アメリカ","sales":489.51,"expenses":2358.24,"date":"2017-02-08T12:47:06.405Z"},' + '{"id":1,"country":"ドイツ","sales":7803.20,"expenses":2513.54,"date":"2017-02-09T12:47:06.405Z"},' + '{"id":2,"country":"イギリス","sales":9996.58,"expenses":2616.71,"date":"2017-02-10T12:47:06.405Z"},' + '{"id":3,"country":"日本","sales":9351.68,"expenses":3030.59,"date":"2017-02-10T12:47:06.405Z"},' + '{"id":4,"country":"スペイン","sales":349.51,"expenses":7358.24,"date":"/Date(1486561758556)"}' + ']'; // return json; } .wj-flexgrid { max-height: 250px; } body { margin-bottom: 36pt; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './styles.css'; // import { Component, Inject, enableProdMode, NgModule } 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 { dataBad: any; dataGood: any; // constructor(@Inject(DataService) private dataService: DataService) { // Start with some data encoded as JSON (as it would arrive from a server) let json = dataService.getData(); // // Decode the data // No special parsing for dates, the date field will contain strings this.dataBad = JSON.parse(json); // // Decode the data // Use with a Date reviver to restore date fields this.dataGood = JSON.parse(json, (key, value) => { if (typeof value === 'string') { // Parse dates saved as JSON-strings let m = value.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/); if (m) { return new Date(Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6])); } // // Parse dates saved as OData-style strings m = value.match(/^\/Date\((\d+)\)$/); if (m) { return new Date(parseInt(m[1])); } } return value; }); } } // @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>GrapeCity Loading JSON Dates</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"> <p> 次の例で、「日付」列に文字列がどのように含まれているかに注意してください。 </p> <wj-flex-grid [itemsSource]="dataBad"> <wj-flex-grid-column binding="id" header="ID" [width]="80"></wj-flex-grid-column> <wj-flex-grid-column binding="country" header="国"></wj-flex-grid-column> <wj-flex-grid-column binding="sales" header="売上" [width]="80"></wj-flex-grid-column> <wj-flex-grid-column binding="expenses" header="費用" [width]="80"></wj-flex-grid-column> <wj-flex-grid-column binding="date" header="日付"></wj-flex-grid-column> </wj-flex-grid> <p> 同じデータを表示するグリッドを示します。今回は、日付値が正常に解析/設定されています。 </p> <wj-flex-grid [itemsSource]="dataGood"> <wj-flex-grid-column binding="id" header="ID" [width]="80"></wj-flex-grid-column> <wj-flex-grid-column binding="country" header="国"></wj-flex-grid-column> <wj-flex-grid-column binding="sales" header="売上" [width]="80"></wj-flex-grid-column> <wj-flex-grid-column binding="expenses" header="費用" [width]="80"></wj-flex-grid-column> <wj-flex-grid-column binding="date" header="日付"></wj-flex-grid-column> </wj-flex-grid> </div> import { Injectable } from '@angular/core'; // @Injectable() export class DataService { getData() { let json = '[' + '{"id":0,"country":"アメリカ","sales":489.51,"expenses":2358.24,"date":"2017-02-08T12:47:06.405Z"},' + '{"id":1,"country":"ドイツ","sales":7803.20,"expenses":2513.54,"date":"2017-02-09T12:47:06.405Z"},' + '{"id":2,"country":"イギリス","sales":9996.58,"expenses":2616.71,"date":"2017-02-10T12:47:06.405Z"},' + '{"id":3,"country":"日本","sales":9351.68,"expenses":3030.59,"date":"2017-02-10T12:47:06.405Z"},' + '{"id":4,"country":"スペイン","sales":349.51,"expenses":7358.24,"date":"/Date(1486561758556)"}' + ']'; // return json; } } .wj-flexgrid { max-height: 250px; } body { margin-bottom: 36pt; } import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import './app.css'; import * as React from 'react'; import * as ReactDOM from 'react-dom'; import * as wjGrid from '@grapecity/wijmo.react.grid'; import { getData } from './data'; class App extends React.Component { constructor(props) { super(props); var json = getData(); this.state = { dataBad: JSON.parse(json), dataGood: JSON.parse(json, (key, value) => { if (typeof value === 'string') { // Parse dates saved as JSON-strings let m = value.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/); if (m) { return new Date(Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6])); } // // Parse dates saved as OData-style strings m = value.match(/^\/Date\((\d+)\)$/); if (m) { return new Date(parseInt(m[1])); } } return value; }) }; } render() { return <div className="container-fluid"> <p> 次の例で、「日付」列に文字列がどのように含まれているかに注意してください。 </p> <wjGrid.FlexGrid itemsSource={this.state.dataBad}> <wjGrid.FlexGridColumn binding="id" header="ID" width={80}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="country" header="国"></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="sales" header="売上" width={80}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="expenses" header="費用" width={80}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="date" header="日付"></wjGrid.FlexGridColumn> </wjGrid.FlexGrid> <p> 同じデータを表示するグリッドを示します。今回は、日付値が正常に解析/設定されています。 </p> <wjGrid.FlexGrid itemsSource={this.state.dataGood}> <wjGrid.FlexGridColumn binding="id" header="ID" width={80}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="country" header="国"></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="sales" header="売上" width={80}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="expenses" header="費用" width={80}></wjGrid.FlexGridColumn> <wjGrid.FlexGridColumn binding="date" header="日付"></wjGrid.FlexGridColumn> </wjGrid.FlexGrid> </div>; } } 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>AutoComplete</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> .wj-flexgrid { max-height: 250px; } body { margin-bottom: 36pt; } export function getData() { let json = '[' + '{"id":0,"country":"アメリカ","sales":489.51,"expenses":2358.24,"date":"2017-02-08T12:47:06.405Z"},' + '{"id":1,"country":"ドイツ","sales":7803.20,"expenses":2513.54,"date":"2017-02-09T12:47:06.405Z"},' + '{"id":2,"country":"イギリス","sales":9996.58,"expenses":2616.71,"date":"2017-02-10T12:47:06.405Z"},' + '{"id":3,"country":"日本","sales":9351.68,"expenses":3030.59,"date":"2017-02-10T12:47:06.405Z"},' + '{"id":4,"country":"スペイン","sales":349.51,"expenses":7358.24,"date":"/Date(1486561758556)"}' + ']'; // return json; } <template> <div class="container-fluid"> <p> 次の例で、「日付」列に文字列がどのように含まれているかに注意してください。 </p> <wj-flex-grid :items-source="dataBad"> <wj-flex-grid-column binding="id" header="ID" :width=80></wj-flex-grid-column> <wj-flex-grid-column binding="country" header="国"></wj-flex-grid-column> <wj-flex-grid-column binding="sales" header="売上" :width=80></wj-flex-grid-column> <wj-flex-grid-column binding="expenses" header="費用" :width=80></wj-flex-grid-column> <wj-flex-grid-column binding="date" header="日付"></wj-flex-grid-column> </wj-flex-grid> <p> 同じデータを表示するグリッドを示します。今回は、日付値が正常に解析/設定されています。 </p> <wj-flex-grid :items-source="dataGood"> <wj-flex-grid-column binding="id" header="ID" :width=80></wj-flex-grid-column> <wj-flex-grid-column binding="country" header="国"></wj-flex-grid-column> <wj-flex-grid-column binding="sales" header="売上" :width=80></wj-flex-grid-column> <wj-flex-grid-column binding="expenses" header="費用" :width=80></wj-flex-grid-column> <wj-flex-grid-column binding="date" header="日付"></wj-flex-grid-column> </wj-flex-grid> </div> </template> <script> import "bootstrap.css"; import "@grapecity/wijmo.styles/wijmo.css"; import Vue from 'vue'; import * as wijmo from '@grapecity/wijmo.vue2.grid'; import { getData } from './data'; new Vue({ el: '#app', data: function () { var json = getData(); return { dataBad: JSON.parse(json), dataGood : JSON.parse(json, (key, value) => { if (typeof value === 'string') { // Parse dates saved as JSON-strings let m = value.match(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/); if (m) { return new Date(Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6])); } // // Parse dates saved as OData-style strings m = value.match(/^\/Date\((\d+)\)$/); if (m) { return new Date(parseInt(m[1])); } } return value; }) } } }) </script> <style> .container-fluid .wj-flexgrid { max-height: 250px; } body { margin-bottom: 36pt; } </style> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Loading JSON Dates</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() { let json = '[' + '{"id":0,"country":"アメリカ","sales":489.51,"expenses":2358.24,"date":"2017-02-08T12:47:06.405Z"},' + '{"id":1,"country":"ドイツ","sales":7803.20,"expenses":2513.54,"date":"2017-02-09T12:47:06.405Z"},' + '{"id":2,"country":"イギリス","sales":9996.58,"expenses":2616.71,"date":"2017-02-10T12:47:06.405Z"},' + '{"id":3,"country":"日本","sales":9351.68,"expenses":3030.59,"date":"2017-02-10T12:47:06.405Z"},' + '{"id":4,"country":"スペイン","sales":349.51,"expenses":7358.24,"date":"/Date(1486561758556)"}' + ']'; // return json; }