0

I have an ajax request in the background that fetches forum posts and renders them in a loop as a table item (<tr> and <td>). All items have a checkbox. When the user checked a checkbox and finally pressed a complete button, I want to get all checked items including their data. (Not just id or name)

If you look at my demo you will understand what I am trying to do.

I've tried with states but how do add state to ajax response data?

For the demo, I've created a posts const. It actually comes from an ajax response. I do not have the ability to alter their data.

Live Editor: https://stackblitz.com/edit/react-bzwmsg

Live Demo: https://react-bzwmsg.stackblitz.io

2 Answers 2

1

I have Changed The files please Go Through It

index.js

import React, { Component } from 'react';
import { render } from 'react-dom';
import TableItem from './TableItem';
import './style.css';

class App extends Component {
  constructor() {
    super();
    this.state = {
      name: 'React',
      items:[]
    };

    this.handleSelected = this.handleSelected.bind(this)
    this.handleAddItem = this.handleAddItem.bind(this)

  }

  handleSelected() {
    // how to get items here? (when user select via checkboxes.)
    const items = this.state.items;
    alert("Selected items are: " + JSON.stringify(items));

  }

   handleAddItem(e,item){
      let items = [...this.state.items]
      var ids = items.map(ele => ele.id);
      if(e.target.checked)
          items.push(item)
      else {
        var index = ids.indexOf(item.id);
        items.splice(index,1);
      }

      this.setState({items});
    }

  render() {

    // posts actually came from ajax request...
    const posts = [
      {
        id: 1,
        name: 'Text 1'
      },
      {
        id: 2,
        name: 'Text 2'
      },
      {
        id: 3,
        name: 'Text 3'
      }
    ]
   console.log(this.state.items)
    return (
      <div>
        {posts.map((fx, i) => {
           { /* here i loop them and render... */ }
          return (
            <TableItem key={fx.id} data={fx} handleAddItem={this.handleAddItem} />
          )
        })}
        <div>
        { /* I need to get checked items outisde of TableItem. */ }
        <button onClick={this.handleSelected}>Get Selected Items (with ID, Name...)</button>
        </div>
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

TableItems.js

import React from 'react'



export default class TableItem extends React.Component {

  constructor(props) {
    super(props)
    this.state = { checked: false }
  }

  render() {

    return (
      <tr>
        <td><input onChange={(e) => this.props.handleAddItem(e,this.props.data) } type="checkbox" defaultChecked={this.state.checked} /></td>
        <td>{this.props.data.name}</td>
      </tr>
    )
  }
}

Sign up to request clarification or add additional context in comments.

2 Comments

May I ask, in this example, how can i check all? (like a check all checkboxes)?
use js or jquery $(".checkBoxClass").attr('checked', this.checked); on click an element
0

This solved my problem: https://medium.com/@wlodarczyk_j/handling-multiple-checkboxes-in-react-js-337863fd284e

But, If you have a better alternative, I'm here to listen.

Basically, I created a map and pass handleChange to TableItem. Therefore, when a checkbox is checked/unchecked, its status passed to parent component.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.