jQuery UI Widgets Forums Grid Can someone verify my understanding of: Grid?

This topic contains 1 reply, has 2 voices, and was last updated by  admin 5 months, 3 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author

  • krishna_jp
    Participant

    How do I enable row editing and save changes back to my React state when using jqxGrid?


    admin
    Keymaster

    Hi,

    You can try this:

    import React, { useState, useMemo } from 'react';
    import JqxGrid from 'jqwidgets-scripts/jqwidgets-react-tsx/jqxgrid';
    import 'jqwidgets-scripts/jqwidgets/styles/jqx.base.css';
    import jqx from 'jqwidgets-scripts/jqwidgets/jqxcore';
    
    const MyGrid = () => {
      const [gridData, setGridData] = useState([
        { id: 1, name: 'Product A', price: 100 },
        { id: 2, name: 'Product B', price: 200 }
      ]);
    
      const source = useMemo(() => ({
        datatype: 'array',
        datafields: [
          { name: 'id', type: 'number' },
          { name: 'name', type: 'string' },
          { name: 'price', type: 'number' }
        ],
        localdata: gridData
      }), [gridData]);
    
      const dataAdapter = useMemo(() => {
        return new jqx.dataAdapter(source);
      }, [source]);
    
      const columns = [
        { text: 'ID', datafield: 'id', editable: false, width: 100 },
        { text: 'Name', datafield: 'name', width: 200 },
        { text: 'Price', datafield: 'price', cellsformat: 'c2' }
      ];
    
      const handleRowEndEdit = (event) => {
        const { rowindex, row } = event.args;
    
        setGridData(prevData => {
          const updated = [...prevData];
          updated[rowindex] = row;
          return updated;
        });
      };
    
      return (
        <JqxGrid
          width={'100%'}
          source={dataAdapter}
          columns={columns}
          editable={true}
          editmode={'row'}
          onRowendedit={handleRowEndEdit}
        />
      );
    };
    
    export default MyGrid;

    Best regards,
    Peter

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.