3

I am using react data grid to list down my data .

I want to add a new row on click of a button and therefore the first column ( not necessarily first it can be any ) should be editable and focused upon creation.

I know react data grid has editable cell feature on double click. But I want this on row creation without any click. And it would be great if there is any way to disable editing once user hit enter.

Code to generate list:

import React, { Component } from 'react';
import FileMenuFormatter from './filemenuformatter';
import NameFormatter from './nameformatter';
import ListView from '../../components/listview/listview';
import { getRow } from '../../helpers/fileviewer/fileutils';

const columns = [
    {
        key: 'name',
        name: 'Name',
        formatter: NameFormatter,
        resizable: true,
        sortable: true
    }, {
        key: 'created',
        name: 'Created On',
        resizable: true,
        sortable: true
    }, {
        key: 'lastmodified',
        name: 'Last Modified',
        resizable: true,
        sortable: true
    }, {
        key: 'filesize',
        name: 'File Size',
        resizable: true,
        sortable: true
    },
    {
        key: 'menu',
        name: '',
        width: 35,
        formatter: FileMenuFormatter,
        draggable: false
    }
];
/**
 *
 *
 * @class myComp
 * @extends {Component}
 */
class myComp extends Component {
    getList() {
        let rows = [];
        for (let index in this.props.dList) {
            if (typeof index !== 'undefined') {
                let dir = this.props.dList[index];
                rows.push(getRow("dir", dir))
            }
        }
        for (let index in this.props.fList) {
            if (typeof index !== 'undefined') {
                let file = this.props.fList[index];
                rows.push(getRow("file", file))
            }
        }

        var newRow = this.props.newRow;

        if(Object.keys(newRow).length !== 0) {
            rows.push(getRow("dir", newRow[0]));

        }

        return rows
    }
    getRow(rowId) {
        return this.getList()[rowId];
    }

    render() {
        let rowListData = this.getRowList();
        return (
            <div>
                <ListView
                    columns={columns}
                    rows={rowListData}
                    minHeight={this.props.minHeight} />
            </div>
        );
    }
}
export default myComp;

Anybody has any idea how to achieve this ?

Arjita Mitra
  • 962
  • 2
  • 13
  • 36
  • 1
    Have you tried anything so far? like any readings, googling, searching on similar questions on stackoverflow? Please let us know what you've tried already to give an insight. – CodeMonkey Apr 28 '17 at 03:45
  • I have googled it and found solutions in Jquery. Using contenteditable you can make entire row editable. Those are normal html table. I am using react data grid also I want only that cell editable. So not helpful. – Arjita Mitra Apr 28 '17 at 03:50
  • Post your code here with CSS and everything. People won't bother to help you otherwise. – CodeMonkey Apr 28 '17 at 03:51
  • Do you want to make the full column non-editable or just a cell in a specific row? – Diogo Cunha Apr 29 '17 at 10:22

1 Answers1

2

I have resolved this problem. The workaround is here.

so onClick of a button I called my showActive() function which is responsible to make the column ( 1st in my case ) editable exactly as react data grid does.

1st make the column editable, ReactDataGrid takes "editable" as input function. allowEdit is to check if this column is editable. For me I wanted to make the cell editable only upon new row creation.

Create new obj to insert as the new row in the table. Like this -

 let newRow = [
                    {
                        created: milliseconds,
                        absPath: this.props.dirSelectedPath,
                        modified: milliseconds,
                        size: 0,
                        filename: folderName,
                        type: "FILE_DIR",
                        allowEdit: true
                    }
                ];

Then below is the column configuration of the editable cell.

const columns = [
    {
        key: 'name',
        name: 'Name',
        resizable: true,
        sortable: true,
        filterable: true,
        editable: function (rowData) {
            return rowData.allowEdit ? true : false;
        }
    }
];

Now you have to write a function to show the cell highlighted and active. To do so I called the same function as react data grid calls.

get the handle of grid.

<ReactDataGrid showActive={this.showActive.bind(this)} rowsCount={this.getSize()} ref={node => this.grid = node} {...this.props } />

showActive() {
        let length = this.getSize(); // get the length of the grid i.e number of rows
        let obj = { idx: 0, rowIdx: length };
        let promise = new Promise(function (resolve, reject) {
            if (this.grid) {
                resolve("this worked!");
            }
        }.bind(this));

        promise.then(function () {
            this.grid.onSelect(obj);
            this.grid.setActive('Enter');
        }.bind(this), function () {
        });
    }

hope the helps.

Arjita Mitra
  • 962
  • 2
  • 13
  • 36