首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在传单弹出窗口中添加链接?

如何在传单弹出窗口中添加链接?
EN

Stack Overflow用户
提问于 2020-07-22 05:49:03
回答 1查看 116关注 0票数 0

我是第一次使用Leaflet,我在我的应用程序中获得了所有机会的弹出标记,但现在我希望它成为每个机会的链接。那么,弹出式窗口应该可以让{`/opportunities/${opportunities.id}`}知道如何实现这一点吗?谢谢大家!下面是我现在的代码:

代码语言:javascript
复制
import React from "react";
import { Link } from "react-router-dom";
import CreatedAtShow from "./CreatedAtShow"
import ControlPanel from "./ControlPanel";
import { Opportunities } from "../requests";
import { Spinner } from "./Spinner";
import { GoogleMap, withScriptjs, withGoogleMap } from "react-google-maps";
import { Map, TileLayer, Marker, Popup } from "react-leaflet";
import L from "leaflet";
import * as ELG from "esri-leaflet-geocoder";
import { ClipLoader } from "react-spinners";
import { css } from "@emotion/core";


const override = css`
position: fixed;
z-index: 999;
height: 2em;
width: 2em;
overflow: visible;
margin: auto;
top: 0;
left: 0;
bottom: 0;
right: 0;
`;


export class OpportunitiesIndexPage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      opportunities: [],
      isLoading: true
    };
  }

  async componentDidMount() {
    const opportunities = await Opportunities.all()
    console.log(opportunities)
    this.setState({
      opportunities: opportunities,
      isLoading: false
    });

  }

  async getCoordinates(array) {

    const results = await array.map(async opportunity => {
      let coordinates = []
      await ELG.geocode().text(opportunity.address).run(async function (err, results, response) {
        if (err) {
          console.log(err);
          return;
        }

        coordinates.push({
          lat: results.results[0].latlng.lat,
          lng: results.results[0].latlng.lng
        })

        opportunity.coordinates = [results.results[0].latlng.lat, results.results[0].latlng.lng]
        Opportunities.update(opportunity.id, opportunity)
      })
      console.log(coordinates[0])
      opportunity.coordinates = coordinates
      return opportunity

    })
    console.log(results[0].coordinates)
    return results
  }

  

  deleteOpportunity(id) {
    Opportunities.destroy(id).then(() => {
      this.setState({
        opportunities: this.state.opportunities.filter(q => q.id !== id)
      });
    });
  }
  render() {
    if (this.state.isLoading) {
      return <ClipLoader css={override} size={150}/> ;
    } 
    const { showAll = false } = this.props;
    const filteredOpportunities = this.state.opportunities.filter((q, index) => {
      if (showAll || index < 50) {
        return true;
      }
      return false;
    });


    return ( 
      <main className = "OpportunitiesIndexPage" >
        
          <div style = {{ width: "100vw", height: "60vh" }}> 
              
            
            <Map className = "opportunities-map" style = {{ height: '60vh' }} center = {[this.state.opportunities[0].latitude, this.state.opportunities[0].longitude]} zoom = {11}>
              <TileLayer attribution = '&copy; <a href="http://orm.org/copyright">OpenStreetMap</a> contributors' url = "https://{s}.tile.osm.org/{z}/{x}/{y}.png" />
            { this.state.opportunities.map(opportunity => ( 
                <Marker position = {[opportunity.latitude, opportunity.longitude]} >
                  <Popup > {opportunity.title} </Popup>      
                </Marker>
            ))
            }
            </Map> 
          </div> 
        
        <div>  
          <div className = "opportunities-div" style = {{listStyle: "none", paddingLeft: 0}}>
          {filteredOpportunities.map(opportunities => ( 
            <ul className="opportunities-list">
            <li className = "opportunity-item" key = { opportunities.id } >
              <h4>
              <Link to = {`/opportunities/${opportunities.id}`} className = "item" href = "" >
              { opportunities.title } 
              </Link> 
              </h4>
              </li>
              <li className = "opportunity-" key = { opportunities.id } >
                <h4>
              <Link to = {`/opportunities/${opportunities.id}`} className = "item" href = "" >
              { opportunities.date } 
              </Link> 
              </h4> 
            </li>
            </ul>
            ))
          } 
          </div> 
        </div>
        
    </main>
    );
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-07-22 16:12:38

如果我们考虑到filteredOpportunities是一个对象数组,并且每个对象都包含一个id属性,那么您可以应用与在this.state.opportunities上迭代时相同的代码。

代码语言:javascript
复制
 <Map className = "opportunities-map" style = {{ height: '60vh' }} center = {[this.state.opportunities[0].latitude, this.state.opportunities[0].longitude]} zoom={11}>
  <TileLayer attribution = '&copy; <a href="http://orm.org/copyright">OpenStreetMap</a> contributors' url = "https://{s}.tile.osm.org/{z}/{x}/{y}.png" />
  {this.state.opportunities.map(opportunity => (
    <Marker position={[opportunity.latitude, opportunity.longitude]}>
      <Popup>
        {opportunity.title}
        // here add the link which will redirect to each page accordingly
        <Link to={`/opportunities/${opportunities.id}`}>{opportunity.title}</Link>
      </Popup>
    </Marker>
  ))}
</Map> 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63023618

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档