Linked Questions

Popular Questions

How to get checkbox index / row number?

Asked by At

I have checkboxes on a table that, when checked, I would like to append values to an array from the data populating the table (tableData.element.link in my code below).

I already have a toggleRow method that is called when a checkbox is clicked, but how can I append the matching tableData.element.link to an array when a checkbox is clicked? I.e. how do I get the index of the checkbox's row that was clicked so that I can grab the right value from my tableData array?

  componentDidMount() {
    const _self = this;

    fetch(config.api.urlFor('xxx'))
    .then((response) => response.json())
    .then((data) => {

      const tableContent = [];

      data.array.forEach(element => {
        tableContent.push({
          date: element.date,
          title: element.title,
          link: <Link to='' onClick={(e)=> {e.preventDefault(); this.getPreauthorizedLink(element.link)}}>Report</Link>
        });

      })

      this.tableData = tableContent;

      this.setState({reportJSON: tableContent || [], tableIsBusy: false})

    })
    .catch((err) => _self.setState({tableIsBusy: false }));
  }

  toggleRow(name, id) {
    const newSelected = Object.assign({}, this.state.selected);

    let awsFileKeys = [];

    if(newSelected[name]){
      delete newSelected[name]
    }else {
          newSelected[name] = !this.state.selected[name];
    }
    this.setState({
            selected: newSelected,
            selectAll: 2
        });


        awsFileKeys.push(this.tableData.element.link[id]);
        console.log(awsFileKeys);
    }

Related Questions