Javascript 它就像我以前一样…@AnnahI通常将html代码标记为选中的:checked='checked'和not checked=true,这就是原因:) export default class ResultTable extends Component {

Javascript 它就像我以前一样…@AnnahI通常将html代码标记为选中的:checked='checked'和not checked=true,这就是原因:) export default class ResultTable extends Component { ,javascript,reactjs,Javascript,Reactjs,它就像我以前一样…@AnnahI通常将html代码标记为选中的:checked='checked'和not checked=true,这就是原因:) export default class ResultTable extends Component { state = { data: [ { insightName: 'Name 1', description: 'name 1 de


它就像我以前一样…@AnnahI通常将html代码标记为选中的:checked='checked'和not checked=true,这就是原因:)
export default class ResultTable extends Component {
    state = {
        data: [
            {
                insightName: 'Name 1',
                description: 'name 1 desc',
                created: new Date(),
                createdBy: 'Person 1',
                category: 'insight 1',
                status: 'published'
            },
            {
                insightName: 'Name 2',
                description: 'name 2 desc',
                created: new Date(),
                createdBy: 'Person 2',
                category: 'insight 2',
                status: 'published'
            },
            {
                insightName: 'Name 3',
                description: 'name 3 desc',
                created: new Date(),
                createdBy: 'Person 3',
                category: 'insight 3',
                status: 'published'
            }
        ],
        selectedRow: ''
    }

    handleRadioButton = (e) => {
        e.persist()
        console.log('clicked', e)

         this.setState({
             selectedRow: e.target.value
         }, () => console.log(this.state.selectedRow))
    }

    render() {
        return (
            <div>
                <Table striped bordered hover>
                    <thead>
                        <tr>
                            <th> </th>
                            <th>Insight Name</th>
                            <th>Insight Description</th>
                            <th>Created</th>
                            <th>Created By</th>
                            <th>Category</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        {
                            this.state.data.map((obj, idx) => {
                                return (
                                    <tr onClick={this.selectRow}>
                                        <td>
                                            <div class="Form-group">
                                                <div class="Form-radio Form-radio--inline">
                                                    <input id="radio7" name="radioInline" type="radio" value={obj.insightName} checked={this.state.selectedRow === obj.insightName} onChange={this.handleRadioButton} />
                                                    <label for="radio7"></label>
                                                </div>
                                                <div />
                                            </div>
                                        </td>
                                        <td>{obj.insightName}</td>
                                        <td>{obj.description}</td>
                                        <td>{obj.created.toString()}</td>
                                        <td>{obj.createdBy}</td>
                                        <td>{obj.category}</td>
                                        <td>{obj.status}</td>
                                    </tr>)
                            })
                        }
                    </tbody>
                </Table>
            </div>
        )
    }
}
checked={state.selectedRow === obj.insightName ? "checked" : null }



import React from "react";
import ReactDOM from "react-dom";

import "./styles.css";

function App() {
  const state = {
    data: [
      {
        insightName: "Name 1",
        description: "name 1 desc",
        created: new Date(),
        createdBy: "Person 1",
        category: "insight 1",
        status: "published"
      },
      {
        insightName: "Name 2",
        description: "name 2 desc",
        created: new Date(),
        createdBy: "Person 2",
        category: "insight 2",
        status: "published"
      },
      {
        insightName: "Name 3",
        description: "name 3 desc",
        created: new Date(),
        createdBy: "Person 3",
        category: "insight 3",
        status: "published"
      }
    ],
    selectedRow: ""
  };

  const handleRadioButton = e => {
    e.persist();
    console.log("clicked", e);

    this.setState(
      {
        selectedRow: e.target.value
      },
      () => console.log(this.state.selectedRow)
    );
  };

  return (
    <div className="App">
      <table clasName="striped bordered hover">
        <thead>
          <tr>
            <th> </th>
            <th>Insight Name</th>
            <th>Insight Description</th>
            <th>Created</th>
            <th>Created By</th>
            <th>Category</th>
            <th>Status</th>
          </tr>
        </thead>
        <tbody>
          {state.data.map((obj, idx) => {
            return (
              <tr click={state.selectedRow}>
                <td>
                  <div className="Form-group">
                    <div className="Form-radio Form-radio--inline">
                      <input
                        id={`radio${idx}`}
                        name="radioInline"
                        type="radio"
                        value={obj.insightName}
                        checked={
                          state.selectedRow === obj.insightName
                            ? "checked"
                            : null
                        }
                        change={handleRadioButton}
                      />
                      <label for="radio7" />
                    </div>
                    <div />
                  </div>
                </td>
                <td>{obj.insightName}</td>
                <td>{obj.description}</td>
                <td>{obj.created.toString()}</td>
                <td>{obj.createdBy}</td>
                <td>{obj.category}</td>
                <td>{obj.status}</td>
              </tr>
            );
          })}
        </tbody>
      </table>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);