如何获取复选框索引/行号?

时间:2019-02-07 00:57:31

标签: javascript node.js

我在表上有一个复选框,选中后,我想将填充表中数据的值追加到数组中(下面代码中的tableData.element.link)。

我已经有一个toggleRow方法,该方法在单击复选框时会被调用,但是如何在单击复选框时将匹配的tableData.element.link附加到数组中?即如何获取被单击的复选框行的索引,以便可以从tableData数组中获取正确的值?

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

1 个答案:

答案 0 :(得分:0)

我完成此操作的方法实际上是在将AWS文件密钥分配给每个复选框之后查看复选框的值。然后,我从所选项目数组中取出所有AWS文件密钥,并将它们放在新数组中:

  toggleSelectAll() {
    let newSelected = {};
    let newFileKeys = [];

    if (this.state.selectAll === 0) {
        this.state.reportJSON.forEach(x => {
            newSelected[x.linkRaw] = true;
        });
    }

    this.setState({
        selected: newSelected,
        selectAll: this.state.selectAll === 0 ? 1 : 0
    })

    for(let item in Object.keys(this.state.selected)){
      newFileKeys.push(this.state.selected[item]);
    }

    this.state.awsFileKeys = newFileKeys;

    }