3

我正在使用反应数据网格来列出我的数据。

我想在单击按钮时添加新行,因此第一列(不一定首先可以是任何列)应该是可编辑的并专注于创建。

我知道反应数据网格在双击时具有可编辑的单元格功能。但我希望在没有任何点击的情况下创建行。如果有任何方法可以在用户按 Enter 后禁用编辑,那就太好了。

生成列表的代码:

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;

有人知道如何实现这一目标吗?

4

1 回答 1

2

我已经解决了这个问题。解决方法在这里。

所以 onClick 一个按钮,我调用了我的 showActive() 函数,该函数负责使列(在我的情况下为第一个)完全可编辑,就像反应数据网格一样。

第一个使列可编辑,ReactDataGrid 将“可编辑”作为输入函数。allowEdit 是检查此列是否可编辑。对我来说,我想让单元格仅在创建新行时才可编辑。

创建新的 obj 以作为表中的新行插入。像这样 -

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

然后下面是可编辑单元格的列配置。

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

现在您必须编写一个函数来显示单元格突出显示并处于活动状态。为此,我调用了与响应数据网格调用相同的函数。

获取网格的句柄。

<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 () {
        });
    }

希望有所帮助。

于 2017-07-23T12:35:11.817 回答