简体   繁体   中英

Display radio buttons depending on attributes present in dictionary

I need to dynamically display radio buttons depending on how many roi_ (eg roi0 or roi1) each frame_number has.
For example if frame_number: 1, had only one roi_ (roi0) attribute then it would display one radio button. But in the below output.json form the first three frames have two roi_ (roi0, roi2) so therefore two radio buttons are displayed.
I've tried multiple ways but cant get it.Any sugestions?

Also in Buttons_footer, ive displayed two radio buttons temporary as this is what i want it to look like, but to change dynamically depending on the data.

[{"frame_number": 1, "roi0": [101.78202823559488, 99.39509279584912, 49.546951219239915, 29.728170731543948], "intensity0": 80.0, "roi1": [101.78202823559488, 99.39509279584912, 49.546951219239915, 29.728170731543948], "intensity1": 157.0},
{"frame_number": 2, "roi0": [102.56623228630755, 97.95906005049548, 50.25603182631066, 30.153619095786393], "intensity0": 80.0, "roi1": [102.56623228630755, 97.95906005049548, 50.25603182631066, 30.153619095786393], "intensity1": 158.0},
{"frame_number": 3, "roi0": [103.39336535376313, 98.20468223716023, 49.58465295946593, 29.750791775679556], "intensity0": 80.0, "roi1": [103.39336535376313, 98.20468223716023, 49.58465295946593, 29.750791775679556], "intensity1": 157.0},

import "bootstrap/dist/css/bootstrap.css";
import React from "react";
import Radio_Button from "./components/Radio_Button.js";
import Buttons_Footer from "./components/Buttons_Footer.js";
import LeftPane from "./components/LeftPane.js";

//import './App.css';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { apiResponse: [] };

  }
  // Comunicate with API
  callAPI() {
    fetch("http://localhost:9000/IntensityAPI") //React app talks to API at this url
      .then(res => res.json())
      .then(res => this.setState({ apiResponse: res }));
  }
  componentWillMount() {
    this.callAPI();
  }
  render() {
    return (

      <div className="App">

        <header className="App-header">
          <p></p>
          <div class="row fixed-bottom no-gutters">
            <div class="col-3 fixed-top fixed-bottom">

              <LeftPane></LeftPane>
            </div>
            <div class="offset-md-3" >
            <Buttons_Footer readings =   {this.state.apiResponse}/>
            </div>
          </div>
        </header>
      </div>
    );
  }
}
export default App;
import $ from "jquery";
import React, { Component } from 'react';
import { MDBFormInline } from 'mdbreact';
import { Container, Row, Col } from 'reactstrap';
import Radio_Button from "./Radio_Button.js";
// Footer Component with checkbox's used to select region/region's of interest 
class Buttons_Footer extends Component {
  // Enables the Functionality of the "Select Multiple Region's" switch using jquerys
  componentDidMount() {
    $(".region").click(function(e){
    if($('#customSwitches').is(':not(:checked)')){
      if($('.region:checked').length > 1){ // Multiply regions unable to be selected
        alert('You can not check multiple');
        e.preventDefault();
      }
    }
});

$("#customSwitches").click(function(e){ // Multiply regions able to be selected
   $(".region").prop('checked', false);
}); }

//<p>{this.props.region.roi0}</p>
  render() {
    return (
      <Container class = "container  offset-md-3" > 
            <div className='custom-control custom-switch' >
            <input type='checkbox' className='custom-control-input' id='customSwitches' />
            <label  className='custom-control-label' htmlFor='customSwitches'>
                 Select Multiple Region's
            </label> 
            {this.props.readings.map((region)=>{
              return <Radio_Button region ={region} key ={region.frame_number}/>
            })}
              <MDBFormInline>
                <input class="region" type="checkbox" name="region1" value="1" />
                <label for="region1"> 1</label>
                <input class="region" type="checkbox" name="region2" value="2" />
                <label for="region2"> 2</label>
             </MDBFormInline>
            </div>      
    </Container>
    );
  }
}

export default Buttons_Footer;

import React, { Component } from 'react';
import { MDBFormInline } from 'mdbreact';
import { Container, Row, Col } from 'reactstrap';

class Radio_Button extends Component {

//
  render() {

    return (
      <div>
      //<p>{this.props.region.roi0}</p>
      <input class="region" type="checkbox" name="region1" value="1" />
      <label for="region1"> 1</label>
      </div>
    );
  }
}

export default Radio_Button;

Assuming that you don't know how many roi's will be in response:

  1. Get keys of every frame object and insert that data into array. After that you should have arrays in array.

const keys = frames.map(frame => Object.keys(frame))

  1. Filter keys with regex.

const filteredKeys = keys.map(frame => frame.filter(key => new RegExp(/^roi\\d+$/).test(key)))

  1. For each key that's left show button.

const showButtons = () => { return filteredKeys.map(frame => frame.map(roi => <Button />)) }

That should work, but it won't be the fastest and cleanest solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM