import * as React from 'react'; import { generatedata } from './generatedata'; import JqxButton from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxbuttons'; import JqxGrid, { IGridProps, jqx } from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxgrid'; import JqxInput from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxinput'; import JqxMenu from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxmenu'; import JqxNumberInput from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxnumberinput'; import JqxWindow from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxwindow'; class App extends React.PureComponent<{}, IGridProps> { private myGrid = React.createRef(); private myWindow = React.createRef(); private firstName = React.createRef(); private lastName = React.createRef(); private product = React.createRef(); private quantity = React.createRef(); private price = React.createRef(); private myMenu = React.createRef(); private editrow: number = -1; constructor(props: {}) { super(props); this.myGridOnRowClick = this.myGridOnRowClick.bind(this); this.myMenuOnItemClick = this.myMenuOnItemClick.bind(this); this.saveBtnOnClick = this.saveBtnOnClick.bind(this); this.cancelBtnOnClick = this.cancelBtnOnClick.bind(this); const source: any = { datafields: [ { name: 'firstname', type: 'string' }, { name: 'lastname', type: 'string' }, { name: 'productname', type: 'string' }, { name: 'quantity', type: 'number' }, { name: 'price', type: 'number' } ], datatype: 'array', localdata: generatedata(25, false) }; this.state = { columns: [ { text: 'First Name', datafield: 'firstname', width: 200 }, { text: 'Last Name', datafield: 'lastname', width: 200 }, { text: 'Product', datafield: 'productname', width: 190 }, { text: 'Quantity', datafield: 'quantity', width: 90, cellsalign: 'right' }, { text: 'Price', datafield: 'price', cellsalign: 'right', cellsformat: 'c2' } ], source: new jqx.dataAdapter(source) } } public componentDidMount() { document.addEventListener('contextmenu', event => event.preventDefault()); } public render() { return (
Edit
First Name:
Last Name:
Product:
Quantity:
Price:
Save Cancel
  • Edit Selected Row
  • Delete Selected Row
); } private myGridOnRowClick(event: any): void | boolean { if (event.args.rightclick) { this.myGrid.current!.selectrow(event.args.rowindex); const scrollTop = window.scrollY; const scrollLeft = window.scrollX; this.myMenu.current!.open(parseInt(event.args.originalEvent.clientX, 10) + 5 + scrollLeft, parseInt(event.args.originalEvent.clientY, 10) + 5 + scrollTop); return false; } }; private myMenuOnItemClick(event: any): void { const args = event.args; const rowindex = this.myGrid.current!.getselectedrowindex(); if (args.innerHTML === 'Edit Selected Row') { this.editrow = rowindex; this.myWindow.current!.setOptions({ position: { x: 60, y: 60 } }); // get the clicked row's data and initialize the input fields. const dataRecord = this.myGrid.current!.getrowdata(this.editrow); this.firstName.current!.val(dataRecord.firstname); this.lastName.current!.val(dataRecord.lastname); this.product.current!.val(dataRecord.productname); this.quantity.current!.setOptions({ decimal: dataRecord.quantity }); this.price.current!.setOptions({ decimal: dataRecord.price }); // show the popup window. this.myWindow.current!.open(); } else { const rowid = this.myGrid.current!.getrowid(rowindex); this.myGrid.current!.deleterow(rowid); } }; private saveBtnOnClick(): void { if (this.editrow >= 0) { const row = { firstname: this.firstName.current!.getOptions('value'), lastname: this.lastName.current!.getOptions('value'), price: this.price.current!.getDecimal(), productname: this.product.current!.getOptions('value'), quantity: this.quantity.current!.getDecimal(), }; const rowid = this.myGrid.current!.getrowid(this.editrow); this.myGrid.current!.updaterow(rowid, row); this.myWindow.current!.hide(); } }; private cancelBtnOnClick(): void { this.myWindow.current!.hide(); }; } export default App;