Saya ingin menggunakan Moustache untuk menghasilkan halaman HTML khusus untuk setiap objek array, setiap kali kita mengklik tautan "hubungi dia". Namun, sepertinya tautan dibuat hanya untuk satu pengguna.

Pertanyaan saya: Bagaimana saya bisa memiliki halaman khusus untuk setiap objek larik, alih-alih memiliki tautan yang sama untuk setiap objek larik?

Berikut adalah kode untuk mereproduksi kasus:

Index.html

<!DOCTYPE html>
<html lang="en">
<head>
</head>


<body>

    <div id="test-id">

<!-- Generated Content  -->

    </div>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="main.js"></script>
    <script src="mustache.js"></script>
</body>
</html>

Template_page.html

<!DOCTYPE html>
<html lang="en">
<head>
</head>

<body>

    <div id="template-div">

    <!-- part filled by Mustache -->

    </div>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="main.js"></script>
    <script src="mustache.js"></script>

</body>


</html>

Main.js

document.addEventListener('DOMContentLoaded', function() {

    var ALL_DATA = [
{
    'lastname': 'lastname_1',
    'firstname': 'firstname_1'
},
{
    'lastname': 'lastname_2',
    'firstname': 'firstname_2'
},
{
    'lastname': 'lastname_3',
    'firstname': 'firstname_3'
}
];

    function displayData(obj_name) {

        var theDiv = document.getElementById("test-id");

        obj_name.forEach(doc => {
            var lastname = doc['lastname'];
            var firstname = doc['firstname'];

            console.log("found");
            console.log(doc);

            newdiv = document.createElement("div")
            var text = document.createTextNode(lastname + ' ' + firstname);
            var anchor_value = document.createElement("a")
            anchor_value.setAttribute("href", "template_page.html");
            anchor_value.setAttribute("class", "link-a");
            anchor_value.onclick = createPage(doc);
            anchor_value.appendChild(document.createTextNode("Contact him"));
            newdiv.appendChild(text);
            newdiv.appendChild(anchor_value);
            theDiv.appendChild(newdiv);

        });
    };

    displayData(ALL_DATA);



    function createPage(data) {

        console.log("found2");
        console.log(data);
        var template =

            `
    <h1> Welcome, {{firstname}} </h1>
    <h2> Your last name: {{lastname}} </h2>

    `

        var html = Mustache.render(template, data);
        $("#template-div").html(html);

    };


});

(mustache.js juga diperlukan)

2
Abilys38 7 Maret 2020, 21:44

1 menjawab

Jawaban Terbaik

Jika Anda menginginkannya hanya dari sisi klien, Anda dapat menggunakan parameter Url

Index.html

<!DOCTYPE html>
<html lang="en">

<head>
</head>

<body>

  <div id="test-id">
  </div>

  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/3.1.0/mustache.min.js">
  </script>

  <script>
    document.addEventListener("DOMContentLoaded", function() {
      var ALL_DATA = [{
          lastname: "lastname_1",
          firstname: "firstname_1"
        },
        {
          lastname: "lastname_2",
          firstname: "firstname_2"
        },
        {
          lastname: "lastname_3",
          firstname: "firstname_3"
        }
      ];

      function displayData(obj_name) {
        var theDiv = document.getElementById("test-id");
        obj_name.forEach(doc => {
          var lastname = doc["lastname"];
          var firstname = doc["firstname"];
          newdiv = document.createElement("div");
          var text = document.createTextNode(lastname + " " + firstname);
          var anchor_value = document.createElement("a");
          anchor_value.setAttribute("href", "template_page.html?fN=" + firstname + "&lN=" + lastname);
          anchor_value.setAttribute("class", "link-a");
          anchor_value.onclick = function() {
            createPage(doc);
          };
          anchor_value.appendChild(document.createTextNode("UserProfile"));
          newdiv.appendChild(text);
          newdiv.appendChild(anchor_value);
          theDiv.appendChild(newdiv);
        });
      }
      displayData(ALL_DATA);
    });
  </script>
</body>

</html>

Template_page.html

    <!DOCTYPE html>
<html lang="en">

<head>
</head>

<body>

  <div id="template-div">
  </div>

  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/3.1.0/mustache.min.js">
  </script>

  <script>
$(document).ready(function() {
  data = { firstname: GetURLParameter("fN"), lastname: GetURLParameter("lN") };
  createPage(data);
});

function createPage(data) {
  var template = `
    <h1> Welcome, {{firstname}} </h1>
    <h2> Your last name: {{lastname}} </h2>

    `;

  var html = Mustache.render(template, data);
  $("#template-div").html(html);
}

function GetURLParameter(sParam) {
  var sPageURL = window.location.search.substring(1);
  var sURLVariables = sPageURL.split("&");
  for (var i = 0; i < sURLVariables.length; i++) {
    var sParameterName = sURLVariables[i].split("=");
    if (sParameterName[0] == sParam) {
      return sParameterName[1];
    }
  }
}

  </script>
</body>

</html>
0
Rkv88 - Kanyan 9 Maret 2020, 10:49