1

In my code I am trying to do when I select row and click on button then this url open http://codeskulptor-assets.commondatastorage.googleapis.com/assets_clock_minute_arrow.png.

But right now in my code when I select row and click on button then url is not open.

How can we do that to open this url when I select row and click button the open url http://codeskulptor-assets.commondatastorage.googleapis.com/assets_clock_minute_arrow.png.

My code here https://codesandbox.io/embed/react-example-forked-o8tu5?codemirror=1

Anyone plz suggest any idea and help me out. I m stuck on that.

   import React from 'react';
import axios from 'axios';
class ProvFileRptSearchResult extends React.Component {
    constructor(props) {
        super();
      
        this.state = {
            pymtDetails:[],
            data: [],
            rowDetails:[],
            checkbox: false
            
           };
        //    this.handleFile=this.handleFile.bind(this);
             this.handleClick=this.handleClick.bind(this);
       
    }

    handleClick() {
        const apiUrl = "http://localhost:9090/PrvFileRpt/getPrvFileData";
        if (this.state.checkbox) {
          fetch(apiUrl)
            .then((response) => response.json())
            .then((data) => {
              this.setState({ data: data });
              console.log("This is your data", data);
              window.open("https://example.com", "_blank");
            })
        } else {
          alert("Data not fetched!");
        }
        // console.log('click');
      }

    // handleClick(e) {
    //     e.preventDefault();
    //     console.log("The link was clicked");
    //   }

    // handleFile()
    // {
    //     //fetch birt report from here
    //     console.log("inside file  ",this.state.rowDetails);
    // }
    rowSelected(j) {
  
       // e.preventDefault();
       
        console.log(j)
       
        const rownum=j;
         console.log("rownum=",rownum)
   
         console.log(this.props.customerDetails[rownum] )
        this.setState({rowDetails:this.props.customerDetails[rownum]}, () => {
        
        
    });
    }
   
    
    render()
    {
        return(
            <div>
                <div className="table-employee" style={{ marginTop:"20px",border:" 1.5px solid darkgray" }}>
            <table className="table table-hover table-bordered table-sm">
            <thead>
            <tr >
            <th scope="col">Select</th>
            <th scope="col"> LOAD DATE</th>
            <th scope="col"> FILE DATE</th>
                <th scope="col"> SERVICE</th>
                <th scope="col"> PROVISIONER CODE </th>
                <th scope="col"> DESCRIPTION</th>
                
               
            </tr>
            </thead>
            <tbody>
                     {
                     this.props.customerDetails.map((type,j)=>{
                        return(
 
                        <tr> 
                        <td ><input type="radio" preventDefault name="select"  key={j}  onClick={(e) =>this.rowSelected(j)} value={this.state.checkbox}
                    onChange={(e) =>
                      this.setState({ checkbox: !this.state.checkbox })
                    }/></td>
                         <td> {type.provis_file_stamp}</td>
                          <td> {type.provis_file_hdrdt}</td>
                          <td> {type.service_code}</td>
                            <td>{type.provisioner_code}</td>
                            <td>{type.provisioner_desc}</td>   
                            
                            </tr>
                        )
                     })
                         
                }

            </tbody>
            </table>
            </div>
            <div className="btn-submit" >
                            <button className="btn btn-primary" style={{marginRight:"30px"}}  type="submit" onClick={this.handleClick}>FILE</button>
                               
                        </div>
            
    </div>
        )
    }
}
    
export default ProvFileRptSearchResult;
5
  • Any body suggest me any idea codesandbox.io/embed/react-example-forked-o8tu5?codemirror=1 my code Commented May 5, 2021 at 10:34
  • Please don't just post a link to codesandbox for us to help you. Inclulde a minimal example here in the question Commented May 5, 2021 at 10:37
  • Are you sure that you want to open that url? Maybe you want to show that image on page? Commented May 5, 2021 at 10:37
  • Yes openurl link Commented May 5, 2021 at 10:49
  • no i want to open url Commented May 5, 2021 at 10:50

3 Answers 3

1

Call the openInNewTab with the URL. It will open the URL in a new browser tab. Remove '_blank', if you want to open it in the same tab.

const openInNewTab = (url) => {
    const newWindow = window.open(url, '_blank', 'noopener,noreferrer')
    if (newWindow) newWindow.opener = null
}
Sign up to request clarification or add additional context in comments.

3 Comments

where can i use thie code which u posted in my code codesandbox.io/s/o8tu5?file=/index.js
can u help me out.i am stuck on that
where can i use onst openInNewTab in here codesandbox.io/s/o8tu5?file=/index.js:0-2352
0

Well, if I understand correctly, you're trying to open a URL on button click?

If that's right, using window.open('https://example.com', '_blank') in your click handler will allow you to open any URL in a new tab.

18 Comments

how can i use in my code window.open('example.com', '_blank') @Aayush my code codesandbox.io/s/o8tu5?file=/index.js
Check line 21 in this
thanks for help and i want to make 2 more data in my body .How can we add 2 more data.
<td>dfgrty</td> <td>fgfg</td> <td>fgfg</td> <td>erer</td> <td>uuio</td> right now i have only one data in body in here codesandbox.io/s/o8tu5?file=/index.js
You can add more <tr> elements
|
0

First you need to find correct object using find method and then you can open url with window.open

Try following code:-

handleClick = () => {
    const apiUrl = "https://mocki.io/v1/b512f8b8-64ab-46e4-9e0c-9db538a0ad9e";
    if (this.state.checkbox) {
      fetch(apiUrl)
        .then((response) => response.json())
        .then((data) => {
          this.setState({ data: data });
          const urlData = data.find(element => element.id === 3); // filter data with id
          window.open(urlData.url, '_blank'); // open url here
        });
    } else {
      alert("check the radio!");
    }
  };

1 Comment

Thanks but in my code codesandbox.io/s/o8tu5?file=/index.js when i select radio button then click button then url is open but again when i select 2nd row then data not show console and not url

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.