Index.js
export class ForeignKeyColumn extends SampleBase {
constructor() {
super(...arguments);
this.toolbarOptions = ['Add', 'Edit', 'Delete', 'Update', 'Cancel'];
this.validationRules = { required: true };
}
render() {
return (<div className='control-pane'>
<div className='control-section'>
<GridComponent dataSource={orderDetails} allowPaging={true} ref={grid => this.gridInstance = grid} allowFiltering={true} allowSorting={true} editSettings={{ allowEditing: true, allowDeleting: true, allowAdding: true }} filterSettings={{ type: 'Menu' }} toolbar={this.toolbarOptions}>
<ColumnsDirective>
<ColumnDirective field='OrderID' headerText='Order ID' width='120' textAlign='Right' validationRules={this.validationRules} isPrimaryKey={true}></ColumnDirective>
<ColumnDirective field='CustomerID' headerText='Customer Name' width='150' validationRules={this.validationRules} foreignKeyValue='ContactName' foreignKeyField='CustomerID' dataSource={customerData}></ColumnDirective>
<ColumnDirective field='Freight' headerText='Freight' width='100' format='C2' textAlign='Right' editType='numericedit'/>
<ColumnDirective field='ShipName' headerText='Ship Name' width='170'></ColumnDirective>
<ColumnDirective field='ShipCountry' headerText='Ship Country' width='150' editType='dropdownedit'></ColumnDirective>
</ColumnsDirective>
<Inject services={[Filter, Page, Edit, Sort, ForeignKey, Toolbar]}/>
</GridComponent>
</div>
</div>);
}
}
render(<ForeignKeyColumn />, document.getElementById('sample')); |
Index.js
import { render } from "react-dom";
import "./index.css";
import { DataManager, Query } from "@syncfusion/ej2-data";
import * as React from "react";
import { DropDownList } from "@syncfusion/ej2-dropdowns";
import {
GridComponent,
ColumnsDirective,
ColumnDirective,
Page,
Filter,
Inject,
Edit,
Sort,
ForeignKey,
Toolbar
} from "@syncfusion/ej2-react-grids";
import { orderDetails, customerData } from "./data";
import { SampleBase } from "./sample-base";
export class ForeignKeyColumn extends SampleBase {
constructor() {
super(...arguments);
this.toolbarOptions = ["Add", "Edit", "Delete", "Update", "Cancel"];
this.countryElem;
this.countryObj;
this.stateElem;
this.stateObj;
this.validationRules = { required: true };
this.country = [
{ ContactName: "Paul Henriot", CustomerID: "VINET", id: "1" },
{ ContactName: "Karin Josephs", CustomerID: "TOMSP", id: "2" },
{ ContactName: "Mario Pontes", CustomerID: "HANAR", id: "3" },
{ ContactName: "Mary Saveley", CustomerID: "VICTE", id: "4" },
{ ContactName: "Pascale Cartrain", CustomerID: "SUPRD", id: "5" },
{ ContactName: "Yang Wang", CustomerID: "CHOPS", id: "6" }
];
this.stateColl = [
{
CompanyName: "Alfreds Futterkiste",
CustomerID: "VINET",
countryId: "1",
stateId: "101",
Country: "Germany"
},
{
CompanyName: "Ana Trujillo Emparedados y helados",
CustomerID: "TOMSP",
stateId: "102",
Country: "Mexico"
},
{
CompanyName: "Antonio Moreno",
CustomerID: "VINET",
stateId: "103",
Country: "Mexico"
},
{
CompanyName: "Around the Horn",
CustomerID: "HANAR",
stateId: "104",
Country: "UK"
},
{
CompanyName: "Berglunds snabbköp",
CustomerID: "SUPRD",
stateId: "105",
Country: "Sweden"
},
{
CompanyName: "Blauer See Delikatessen",
CustomerID: "CHOPS",
stateId: "106",
Country: "Germany"
},
{
CompanyName: "Blondesddsl père et fils",
CustomerID: "SUPRD",
stateId: "105",
Country: "France"
},
{
CompanyName: "Bólido Comidas preparadas",
CustomerID: "HANAR",
stateId: "106",
Country: "Spain"
},
{
CompanyName: "Blauer See Delikatessen",
CustomerID: "VICTE",
stateId: "106",
Country: "Germany"
}
];
this.countryParams = {
create: () => {
this.countryElem = document.createElement("input");
return this.countryElem;
},
destroy: () => {
this.countryObj.destroy();
},
read: () => {
return this.countryObj.value;
},
write: () => {
this.countryObj = new DropDownList({
change: () => {
this.stateObj.enabled = true;
const tempQuery = new Query().where(
"CustomerID",
"equal",
this.countryObj.value
);
this.stateObj.query = tempQuery; // in the change event of the first dropdown generate query for second dropdown.
this.stateObj.text = "";
this.stateObj.dataBind();
},
dataSource: new DataManager(this.country),
fields: { value: "CustomerID", text: "ContactName" },
floatLabelType: "Never",
placeholder: "Select a country"
});
this.countryObj.appendTo(this.countryElem);
}
};
this.stateParams = {
create: () => {
this.stateElem = document.createElement("input");
return this.stateElem;
},
destroy: () => {
this.stateObj.destroy();
},
read: () => {
return this.stateObj.value;
},
write: () => {
this.stateObj = new DropDownList({
dataSource: new DataManager(this.stateColl),
enabled: false,
fields: { value: "Country", text: "CompanyName" },
floatLabelType: "Never",
placeholder: "Select a state"
});
this.stateObj.appendTo(this.stateElem);
}
};
}
render() {
return (
<div className="control-pane">
<div className="control-section">
<GridComponent
dataSource={orderDetails.slice(0, 8)}
allowPaging={true}
ref={grid => (this.gridInstance = grid)}
allowFiltering={true}
allowSorting={true}
editSettings={{
allowEditing: true,
allowDeleting: true,
allowAdding: true
}}
filterSettings={{ type: "Menu" }}
toolbar={this.toolbarOptions}
>
<ColumnsDirective>
<ColumnDirective
field="OrderID"
headerText="Order ID"
width="120"
textAlign="Right"
validationRules={this.validationRules}
isPrimaryKey={true}
/>
<ColumnDirective
field="CustomerID"
headerText="Customer Name"
width="150"
validationRules={this.validationRules}
foreignKeyValue="ContactName"
foreignKeyField="CustomerID"
dataSource={customerData}
edit={this.countryParams}
/>
<ColumnDirective
field="Country"
headerText="Company Name"
foreignKeyValue="CompanyName"
foreignKeyField="Country"
dataSource={customerData}
edit={this.stateParams}
width="170"
/>
<ColumnDirective
field="Freight"
headerText="Freight"
width="100"
format="C2"
textAlign="Right"
editType="numericedit"
/>
<ColumnDirective
field="ShipCountry"
headerText="Ship Country"
width="150"
editType="dropdownedit"
/>
</ColumnsDirective>
<Inject
services={[Filter, Page, Edit, Sort, ForeignKey, Toolbar]}
/>
</GridComponent>
</div>
</div>
);
}
}
render(<ForeignKeyColumn />, document.getElementById("sample")); |
[demoGrid.tsx]
<GridComponent
dataSource={GridData}
----
>
-----
<Inject services={[Edit,ForeignKey, Toolbar]} />
</GridComponent> |
DemoGrid.tsx
import React from "react";
import _ from "lodash";
import "@syncfusion/ej2-base/styles/material.css";
import "@syncfusion/ej2-buttons/styles/material.css";
import "@syncfusion/ej2-calendars/styles/material.css";
import "@syncfusion/ej2-dropdowns/styles/material.css";
import "@syncfusion/ej2-inputs/styles/material.css";
import "@syncfusion/ej2-navigations/styles/material.css";
import "@syncfusion/ej2-popups/styles/material.css";
import "@syncfusion/ej2-splitbuttons/styles/material.css";
import "@syncfusion/ej2-react-grids/styles/material.css";
import { DropDownList } from "@syncfusion/ej2-dropdowns";
import { DataManager, Query } from "@syncfusion/ej2-data";
import {
GridComponent,
ColumnsDirective,
ColumnDirective,
EditSettingsModel,
PageSettingsModel,
ActionEventArgs,
GroupEventArgs,
FilterEventArgs,
SearchEventArgs,
SortEventArgs,
AddEventArgs,
SaveEventArgs,
EditEventArgs,
PageEventArgs,
DeleteEventArgs
} from "@syncfusion/ej2-react-grids";
import {
Edit,
ForeignKey,
IEditCell,
Inject,
Page,
Toolbar,
ToolbarItems
} from "@syncfusion/ej2-react-grids";
import {
Customers,
CustomerLocations,
GridData,
ICustomer,
IGridData,
ICustomerLocation
} from "./gridData";
type SyncfusionAction =
| PageEventArgs
| GroupEventArgs
| FilterEventArgs
| SearchEventArgs
| SortEventArgs
| AddEventArgs
| SaveEventArgs
| EditEventArgs
| DeleteEventArgs
| ActionEventArgs
| undefined;
interface IDemoGridProps {}
interface IDemoGridState {
dataSourceGrid: IGridData[] | undefined;
loading: boolean;
}
export default class DemoGrid extends React.Component<
IDemoGridProps,
IDemoGridState
> {
constructor(props: IDemoGridProps) {
super(props);
this.state = {
dataSourceGrid: undefined,
loading: true
};
this.gridInstance = React.createRef<GridComponent>();
this.customerElem = undefined;
this.customerObj = undefined;
this.locationElem = undefined;
this.locationObj = undefined;
}
private customerElem: HTMLElement | undefined;
private customerObj: DropDownList | undefined;
private locationElem: HTMLElement | undefined;
private locationObj: DropDownList | undefined;
private customersDm = new DataManager([]);
private locationsDm = new DataManager([]);
private gridInstance: React.RefObject<GridComponent>;
private toolbarOptions: ToolbarItems[] = ["Add", "Edit", "Update", "Cancel"];
private editSettings: EditSettingsModel = {
allowEditing: true,
allowAdding: true,
allowDeleting: true,
newRowPosition: "Top",
mode: "Normal"
};
private pageSettings: PageSettingsModel = { pageCount: 5 };
private validationRules = { required: true };
public async componentDidMount(): Promise<void> {
_.each(Customers, (cust: ICustomer) => this.customersDm.insert(cust));
_.each(CustomerLocations, (cl: ICustomerLocation) =>
this.locationsDm.insert(cl)
);
this.setState({ dataSourceGrid: GridData, loading: false });
}
public customerParams: IEditCell = {
create: () => {
this.customerElem = document.createElement("input");
return this.customerElem;
},
destroy: () => {
if (!!this.customerObj) this.customerObj.destroy();
},
read: () => {
return !!this.customerObj ? this.customerObj.value : "";
},
write: () => {
this.customerObj = new DropDownList({
change: () => {
if (!!this.customerObj && !!this.locationObj) {
this.locationObj.enabled = true;
const tempQuery: Query = new Query().where(
"customerId",
"equal",
this.customerObj.value
);
this.locationObj.query = tempQuery;
this.locationObj.text = "";
this.locationObj.dataBind();
}
},
dataSource: this.customersDm,
fields: { value: "customerId", text: "customerName" },
floatLabelType: "Never",
placeholder: "Select a customer"
});
this.customerObj.appendTo(this.customerElem);
}
};
public locationParams: IEditCell = {
create: () => {
this.locationElem = document.createElement("input");
return this.locationElem;
},
destroy: () => {
if (!!this.locationObj) this.locationObj.destroy();
},
read: () => {
return !!this.locationObj ? this.locationObj.value : "";
},
write: () => {
debugger;
this.locationObj = new DropDownList({
dataSource: this.locationsDm,
enabled: false,
fields: { value: "locationId", text: "locationName" },
floatLabelType: "Never",
placeholder: "Select a location or leave empty"
});
this.locationObj.appendTo(this.locationElem);
}
};
private actionComplete = (args: SyncfusionAction): void => {
const captains = console;
captains.log(!!args ? args.requestType : "Unknown request type");
const actionEvent = args as ActionEventArgs;
const data = actionEvent?.data;
captains.log(JSON.stringify(data));
};
public render() {
return this.state.loading ? (
<div>Loading ...</div>
) : (
<div>
<h3>The Grid:</h3>
<GridComponent
dataSource={GridData}
ref={this.gridInstance}
toolbar={this.toolbarOptions}
allowPaging={true}
editSettings={this.editSettings}
pageSettings={this.pageSettings}
actionComplete={this.actionComplete}
title="Grid Test"
>
<ColumnsDirective>
<ColumnDirective
field="id"
headerText="Id"
width="100"
isPrimaryKey={true}
visible={true}
/>
<ColumnDirective
field="info"
headerText="Info"
width="250"
textAlign="Left"
validationRules={this.validationRules}
/>
<ColumnDirective
field="customerId"
headerText="Customer"
width="150"
validationRules={this.validationRules}
foreignKeyValue="customerName"
foreignKeyField="customerId"
dataSource={this.customersDm}
edit={this.customerParams}
/>
<ColumnDirective
field="locationId"
headerText="Location"
width="150"
type="string"
foreignKeyField="locationId"
foreignKeyValue="locationName"
dataSource={this.locationsDm}
edit={this.locationParams}
/>
</ColumnsDirective>
<Inject services={[Edit, ForeignKey, Page, Toolbar]} />
</GridComponent>
</div>
);
}
} |