Javascript React.js 创建一个带有可编辑列的动态行数的表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/30281143/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React.js creating a table with a dynamic amount of rows with an editable column
提问by napkinsterror
I am trying to create a react.js table that syncs with a leaflet map. I have this data and I am able to get the data properly, but I cannot create a table correctly. I am able to see the headers because they are hardcoded, but I am not able to see the rows. I also have a picture to explain the data at the console.log() points in the code. Here is the code:
我正在尝试创建一个与传单地图同步的 react.js 表。我有这些数据,并且能够正确获取数据,但无法正确创建表。我能够看到标题,因为它们是硬编码的,但我看不到行。我也有图解释代码中console.log()点的数据。这是代码:
/* Table React Component */
var TABLE_CONFIG = {
  sort: { column: "Zone", order: "desc" },
  columns: {
    col1: { name: "Zone", filterText: "", defaultSortOrder: "desc" },
    col2: { name: "Population", filterText: "", defaultSortOrder: "desc" }
  }
};
var Table = React.createClass({
  getInitialState: function() {
    var tabledata = [];
    var length = _.size(testJSON.zones);
    for(i = 0; i < length; i++) {
      var name = _.keys(testJSON.zones)[i];
      var population = testJSON.zones[name].population.value;
      if(name == "default") {
        population = testJSON.zones[name].population.default.value;
      }
      tabledata[i] = {name, population};
    }
    console.log(tabledata);
    return {zones: tabledata};
  },
  render: function() {
    var rows = [];
    this.state.zones.forEach(function(zone) {
        rows.push(<tr Population={zone.population} Zone={zone.name} />);
    }.bind(this));
    console.log(rows);
    return (
        <table>
            <thead>
                <tr>
                    <th>Zone</th>
                    <th>Population</th>
                </tr>
            </thead>
            <tbody>{rows}</tbody>
        </table>
      );
  }
});
Here you can see the console.log(tabledata)line in full as well as the first object in console.log(rows)
在这里您可以看到console.log(tabledata)完整的线条以及第一个对象console.log(rows)


I would like to see something like the picture below. Please note I want to Zone column to not be editable input, but the population to be editable:
我想看到类似下图的东西。请注意,我希望 Zone 列不可编辑输入,但人口可编辑:


回答by Michelle Tilley
<tr Population={zone.population} Zone={zone.name} />does not seem like a valid React component to me. The lowercase <tr>indicates that this is not a custom component, but represents a standard HTML table row. <tr>elements require <td>or <th>elements inside them, for example:
<tr Population={zone.population} Zone={zone.name} />对我来说似乎不是一个有效的 React 组件。小写<tr>表示这不是自定义组件,而是表示标准 HTML 表格行。<tr>元素 require<td>或其中的<th>元素,例如:
var rows = [];
this.state.zones.forEach(function(zone) {
    rows.push(
      <tr />
        <td><SomePopulationComponent /></td>
        <td><SomeZoneComponent /></td>
      </tr>
    );
}.bind(this));
You could extract this into a custom element:
您可以将其提取到自定义元素中:
var CustomRow = React.createClass({
    render: function() {
        return (
            <tr>
                <td>{this.props.Population}</td>
                <td>{this.props.Zone}</td>
            </tr>
        );
    }
});
// ...
var rows = [];
this.state.zones.forEach(function(zone) {
    rows.push(<CustomRow Population={zone.population} Zone={zone.name} />);
}.bind(this));
Also, don't forget to give elements generated from inside a map/foreachfunction a keyattribute.
另外,不要忘记给从map/foreach函数内部生成的元素一个key属性。
回答by RohanArihant
I have made this dynamic data table with dynamic rows and columns editable
我已经使这个动态数据表具有可编辑的动态行和列
class App extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            data: [
                {'id':1,1:'',2:'Class1',3:'Class2',4:'Class3',5:'Class4',6:'Class5',7:'Class6'},
                {'id':2,1:'MONDAY',2:'1',3:'2',4:'3',5:'4',6:'5',7:'6'},
                {'id':3,1:'TUESDAY',2:'1',3:'2',4:'3',5:'4',6:'5',7:'6'},
                {'id':4,1:'WEDNESDAY',2:'1',3:'2',4:'3',5:'4',6:'5',7:'6'},
                {'id':5,1:'THURSDAY',2:'1',3:'2',4:'3',5:'4',6:'5',7:'6'},
                {'id':6,1:'FRIDAY',2:'1',3:'2',4:'3',5:'4',6:'5',7:'6'}
            ],
            errorInput:''
        };
        this.submitStepSignupForm = this.submitStepSignupForm.bind(this);
        this.appendColumn = this.appendColumn.bind(this);
        //  this.editColumn = this.editColumn.bind(this);
    render(){
        let tableStyle = {
            align:"center"
        };
        let list = this.state.data.map(p =>{
            return (
                <tr className="grey2" key={p.id}>
                    {Object.keys(p).filter(k => k !== 'id').map(k => {
                        return (
                            <td className="grey1" key={p.id+''+k}>
                                <div suppressContentEditableWarning="true" contentEditable="true" value={k} onInput={this.editColumn.bind(this,{p},{k})}>
                                    {p[k]}
                                </div>
                            </td>
                        );
                    })}
                </tr>
            );
        });
        return (
            <fieldset className="step-4">
                <div className="heading">
                    <h3>Tell us about your schedule</h3>
                    <p>Dynamic Data Table by Rohan Arihant </p>
                </div>
                <div className="schedule padd-lr">
                    <table cellSpacing="3" id="mytable" style={tableStyle}>
                        <tbody>{list}</tbody>
                    </table>
                </div>
           </fieldset>
        );
    }
}

