【问题标题】:Adding hrefs to autogenerate data tables from firestore添加href以从firestore自动生成数据表
【发布时间】:2020-06-21 04:14:11
【问题描述】:

我想将不同的hrefs分配给一个表中的不同tds,该表从firestore中提取的数据自动生成所述tds。

这是我的oferta.js,我可以毫无问题地从firestore中检索数据,但我真的不知道如何为td“titulo”分配不同的href,我尝试使用锚但结果在每个连续的 td 中建立相同的 URL,我该怎么做才能规避这个问题?提前谢谢!

const ofertaList = document.querySelector('#ofertaLista');


const setupOferta = (data) => {

    let html = '';

    data.forEach(doc => {
      const oferta = doc.data();

      const td =`
        <tr>
          <td><a href="google.com">${oferta.titulo}</a></td>
          <td>${oferta.tipo}</td>
          <td>${oferta.fecha}</td>
          <td>${oferta.areaConocimiento}</td>
          <td>${oferta.cupoLimitado}</td>
        </tr>

      `;
      html += td

    });

    ofertaList.innerHTML = html;
}


db.collection('oferta').get().then((snapshot) =>{

 setupOferta(snapshot.docs)


});


编辑

这是我的oferta.html

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Oferta</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
        <link href="https://fonts.googleapis.com/css?family=Questrial&display=swap" rel="stylesheet">
    <!-- Bulma Version 0.8.x-->
    <link rel="stylesheet" href="https://unpkg.com/bulma@0.8.0/css/bulma.min.css" />
    <link rel="stylesheet" type="text/css" href="css/login.css">


</head>

<body>

    <section class="hero is-info is-bold is-fullhd">
        <div class="hero-body">
            <nav class="navbar is-info">
                <a class="navbar-item" href="https://balsamiq.cloud/sb17nzh/p8yk3ts/r2278">
                    <img src="https://bulma.io/images/bulma-logo.png" width="112" height="28">
                </a>
                <div class="container">
                    <div class="navbar-brand">
                        <h1 class="title">
                            CAEDI
                        </h1>  
                    </div>
                </div>
            </nav>
        </div>
    </section>

   <div id="main">
         <section class="hero is-light">
            <div class="hero-body">
                <div class="container">
                    <h1 class="title">
                        Oferta
                    </h1>
                </div>
            </div>
        </section>

 <div class="container">
      <input class="input" type="text" name="" id="textBoxSearch" placeholder="Buscar" />
      <br /><br />

    </div>

        <section class="section">
            <div class="container">
                <div class="columns">
                    <div class="column is-12">
                        <div>
                        <table id="fullfeatures" class="table is-striped is-bordered" cellspacing="0" width="100%">
                            <thead>
                                <tr id="tr" >
                                    <th>Título</th>
                                    <th>Tipo</th>
                                    <th>Fecha</th>
                                    <th>Área de conocimiento</th>
                                    <th>Cupo limitado</th>
                                </tr>
                            </thead>
                            <tbody id="ofertaLista"></tbody>

                        </table>
                        </div>
                    </div>
                </div> 
            </div>
        </section>






    <footer class="footer has-background-info is-mobile is-fullhd">
        <div class="container">
            <div class="columns is-mobil">
                <div class="column is-half-fullhd">
                    <h1 class="title has-text-left">
                        Contacto
                    </h1>
                    <h2 class="subtitle has-text-left">
                        Direccion, correo electronico, telefono
                    </h2>
                </div>
            <div class="column is-half-fullhd">
                    <h2 class="subtitle has-text-right">
                        Powered by (c) CAEDI
                    </h2>
            </div>
        </div>
    </footer>

<script src="https://www.gstatic.com/firebasejs/7.8.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.8.1/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.8.1/firebase-firestore.js"></script>



<script>

  var firebaseConfig = {
    apiKey: "AIzaSyA0Rl0WAmrpfxPZsWwc_P-AiHg44K8e05c",
    authDomain: "caedi-fd241.firebaseapp.com",
    databaseURL: "https://caedi-fd241.firebaseio.com",
    projectId: "caedi-fd241",

  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);




  const aut = firebase.auth();
  const db = firebase.firestore();


</script>

    <script async type="text/javascript" src="js/bulma.js"></script>
    <script src="https://unpkg.com/bulma-modal-fx/dist/js/modal-fx.min.js"></script> 
    <script async type="text/javascript" src="js/oferta.js"></script>








</body>

</html>

这是最终结果,我想为每个“Titulo”分配不同的href,但是这样做

&lt;td&gt;&lt;a href="google.com"&gt;${oferta.titulo}&lt;/a&gt;&lt;/td&gt;

发生这种情况

我想在“Historia del arte”中放一个href="historia.html",在“Matematicas 1”中放一个href="matematicas.html",这可能吗?,对不起,如果我没有表达我自己清晰

【问题讨论】:

  • 我想看${oferta.titulo}的数据示例
  • 我不太明白这个问题,但也许您正在寻找这样的东西? &lt;td&gt;&lt;a href=`${oferta.link}`&gt;oferta.titulo&lt;/a&gt;&lt;/td&gt;
  • 好的,我编辑了问题,感谢您的耐心等待!

标签: javascript jquery html firebase google-cloud-firestore


【解决方案1】:
<td><a href="${oferta.href}">${oferta.titulo}</a></td>

【讨论】:

  • 感谢您的回复!,我是一个新手,我在哪里使用“href="${oferta.href}"" 定义网址
  • 该信息必须来自数据库本身。您必须在数据库上创建一个名为“href”的属性,然后该属性会自动出现在 API 调用的响应中。换句话说,您需要创建另一个类似于 Titulo、Tipo、Fecha 的列......但名称为 href,那么我猜您将拥有它,但这取决于从数据库获取信息的代码。
【解决方案2】:

你可以这样做

<td><a href="${oferta.href}">${oferta.titulo}</a></td>

<td><a href="http://your-domain.com/${oferta.data_id_or_any_other_field}">${oferta.titulo}</a></td>

【讨论】:

  • 感谢回复,我是新手,使用这个方法的时候url没有定义,怎么定义呢?,不知道怎么"${oferta.href }”的作品
  • 不用担心,您可以轻松实现这一点,您需要在获取所有数据的数据库表中添加一个额外的列。列名将是“href”,然后在“Historia del arte”中添加值“historia.html”,在“Matematicas 1”中添加值“matematicas.html”等等......
【解决方案3】:

只需将元素的 href 设置为 URL 的变量即可:

      <td><a href="${thisIsSomeUrl}">${oferta.titulo}</a></td>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 2021-03-27
    • 1970-01-01
    相关资源
    最近更新 更多