import * as React from 'react'; import * as ReactDOM from 'react-dom'; import { generatedata } from './generatedata'; import JqxButton from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxbuttons'; import JqxDropDownList, { IDropDownListProps } from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxdropdownlist'; import JqxGrid, { IGridProps, jqx } from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxgrid'; import JqxInput from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxinput'; import JqxWindow from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxwindow'; export interface IState extends IGridProps { dropDownSource: IDropDownListProps['source']; } class App extends React.PureComponent<{}, IState> { private myGrid = React.createRef(); private myDropDownList = React.createRef(); private myInput = React.createRef(); private myWindow = React.createRef(); constructor(props: {}) { super(props); this.findBtnOnClick = this.findBtnOnClick.bind(this); this.clearBtnOnClick = this.clearBtnOnClick.bind(this); const rendertoolbar = (toolbar: any): void => { const style: React.CSSProperties = { float: 'left', marginLeft: '5px' }; const buttonsContainer = (
); ReactDOM.render(buttonsContainer, toolbar[0]); }; this.state = { columns: [ { text: 'First Name', columntype: 'textbox', datafield: 'firstname', width: 120 }, { text: 'Last Name', datafield: 'lastname', columntype: 'textbox', width: 120 }, { text: 'Product', datafield: 'productname', width: 170 }, { text: 'In Stock', datafield: 'available', columntype: 'checkbox', width: 125 }, { text: 'Quantity', datafield: 'quantity', width: 85, cellsalign: 'right', cellsformat: 'n2' }, { text: 'Price', datafield: 'price', cellsalign: 'right', cellsformat: 'c2' } ], dropDownSource: ['First Name', 'Last Name', 'Product', 'Quantity', 'Price'], rendertoolbar, source: this.getAdapter() } } public componentDidMount() { setTimeout(() => { this.createButtons(); }); } public render() { return (
Find Record
Find what:
Look in:
Find Clear
); } private getAdapter = (): any => { const source: any = { datafields: [ { name: 'firstname', type: 'string' }, { name: 'lastname', type: 'string' }, { name: 'productname', type: 'string' }, { name: 'quantity', type: 'number' }, { name: 'price', type: 'number' }, { name: 'available', type: 'bool' } ], datatype: 'array', localdata: generatedata(15), updaterow: (rowid: number, rowdata: any, commit: any): void => { // synchronize with the server - send update command // call commit with parameter true if the synchronization with the server is successful // and with parameter false if the synchronization failed. commit(true); } }; const dataAdapter: any = new jqx.dataAdapter(source); return dataAdapter; }; private createButtons(): void { const addButtonClick = (event?: any) => { const datarow = generatedata(1); this.myGrid.current!.addrow(null, datarow[0]); }; const deleteButtonClick = (event?: any) => { const selectedrowindex = this.myGrid.current!.getselectedrowindex(); const id = this.myGrid.current!.getrowid(selectedrowindex); this.myGrid.current!.deleterow(id); }; const reloadButtonClick = (event?: any) => { this.myGrid.current!.setOptions({ source: this.getAdapter() }); }; const searchButtonClick = (event?: any) => { this.myWindow.current!.open(); this.myWindow.current!.move(60, 60); }; ReactDOM.render( , document.getElementById('addButton') ); ReactDOM.render( , document.getElementById('deleteButton') ); ReactDOM.render( , document.getElementById('reloadButton') ); ReactDOM.render( , document.getElementById('searchButton') ); } private findBtnOnClick(): void { this.myGrid.current!.clearfilters(); const searchColumnIndex = this.myDropDownList.current!.getSelectedIndex(); let datafield = ''; switch (searchColumnIndex) { case 0: datafield = 'firstname'; break; case 1: datafield = 'lastname'; break; case 2: datafield = 'productname'; break; case 3: datafield = 'quantity'; break; case 4: datafield = 'price'; break; } const searchText = this.myInput.current!.getOptions('value'); const filtergroup = new jqx.filter(); const filterOrOperator = 1; const filtervalue = searchText; const filtercondition = 'contains'; const filter = filtergroup.createfilter('stringfilter', filtervalue, filtercondition); filtergroup.addfilter(filterOrOperator, filter); this.myGrid.current!.addfilter(datafield, filtergroup); // apply the filters. this.myGrid.current!.applyfilters(); } private clearBtnOnClick(): void { this.myGrid.current!.clearfilters(); } } export default App;