ReactJS:使用react-table使表格可编辑

时间:2017-07-06 11:49:23

标签: javascript reactjs

我使用react-table npm包(https://www.npmjs.com/package/react-table)来生成我的表:

#include <iostream>
using namespace std;

bool isPrime(int p) {
    bool result = true;
    for (int i = 2; i <= p / 2; i++) {
        if (p%i == 0) {
            result = false;
            break;
        }
    }
    return result;
}

int goLeft(int l) {
    if (l != 65) {
        for (int i = l; i>65; i--) {
            if (isPrime(i))
                return i;
        }
        return 0;
    }
    else {
        return 0;
    }
}

int goRight(int r) {
    if (r != 122) {
        for (int i = r; i<122; i++) {
            if (isPrime(i))
                return i;
        }
        return 0;
    }
    else {
        return 0;
    }
}

int performAll(int l, int r, int p) {
    int diffLeft = 0, diffRight = 0;

    if (l == 0 && r != 0) {
        diffLeft = diffRight = r;
    }
    else if (l != 0 && r == 0) {
        diffRight = diffLeft = l;
    }
    else if (l == 0 && r == 0) {
        diffRight = diffLeft = 0;
    }
    else {
        diffLeft = p - l;
        diffRight = r - p;
    }

    if (diffLeft > diffRight) {
        return r;
    }
    else if (diffLeft < diffRight) {
        return l;
    }
    else {
        if (l<r) {
            if (l != 0) {
                return l;
            }
            else {
                return r;
            }
        }
        else if (l>r) {
            if (r != 0) {
                return r;
            }
            else {
                return l;
            }
        }
    }
return 0;
}

int main()
{
    int T, N;
    int left = 0, right = 0, ss = 0;
    cin >> T;
    string S, R;
    while (T>0) {
        cin >> N;
        cin >> S[j];

        for (int i = 0; i<N; i++) {
            cout << "i-" << i << endl;
            ss = S[i];
            if ((ss >= 65 && ss <= 90) || (ss >= 97 && ss <= 122)) {
                if (isPrime(ss)) {
                    R[i] = S[i];
                }
                else {
                    left = goLeft(ss);
                    right = goRight(ss);
                    R[i] = performAll(left, right, ss);
                }
            }
            else {
                if (ss<65) {
                    left = 0;
                    right = 67;
                }
                else if (ss>122) {
                    left = 113;
                    right = 0;
                }
                else {
                    left = goLeft(ss);
                    right = goRight(ss);
                }
                R[i] = performAll(left, right, ss);
            }
            cout << R[i];
        }
        T--;
    }

    return 0;
}

该表按预期显示。但现在我想使这个表格可编辑。所以我需要使用带有数据值的输入字段而不仅仅是文本数据值。

我该怎么做?我不明白这方面的文档......所以我需要一些帮助。

2 个答案:

答案 0 :(得分:2)

首先,您需要编辑Columns props

Check render: props => <input value={props.row.name} onChange={onChangeFct} />

例如:

const onChangeFct = () => console.log("onChange usually handled by redux");
const tableColumns = [
{
    header: 'Id',
    accessor: 'id'
},
{
    header: 'Name',
    accessor: 'name',
    render: props => <input value={props.row.name} onChange={onChangeFct} />
}
]


<ReactTable
    data={ tableData }
    columns={ tableColumns }
    showPagination={ false }
    defaultPageSize={ tableData.length }
/>

答案 1 :(得分:2)

您可以使用文本就位编辑插件,例如react-x-editable插件,使表格可编辑,因为它提供了两种模式:内联和弹出窗口。