Adding hrefs to autogenerate data tables from firestore

94 views Asked by At

I want to assign different hrefs to different tds within a table that autogenerates said tds from data pulled from firestore.

This is my oferta.js, I can retrieve the data from firestore without a problem but I don't really have any idea on how to assign a different href to the td "titulo", I tried using anchors but this results in establishing the same URL in each consecutive td, what can I do to circumvent this problem?, thanks in advance!

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)


});


EDIT

This is my 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/[email protected]/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>

enter image description here

This is the final result, I want to assign different hrefs to each "Titulo" but by doing this

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

This happens

enter image description here

I want to put a href="historia.html" in "Historia del arte" and a href="matematicas.html" in "Matematicas 1", Is it possible?, sorry if I'm not expressing my self with clarity

3

There are 3 answers

2
Telmo Dias On BEST ANSWER
<td><a href="${oferta.href}">${oferta.titulo}</a></td>
2
Arsee On

you can do this

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

or

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

Just set the href of the element to be the variable for the URL:

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