简体   繁体   English

如何将数据从一个功能传递到另一个功能?

[英]how to pass data from one to another function of?

I get the data. 我得到了数据。 I bring a minimum of information about the user. 我带来了有关用户的最少信息。 When you click on a block, modal window appears. 当您单击一个块时,将出现模态窗口。 Modal window, more information should be. 模态窗口,应有更多信息。 I cannot transfer data in a modal window. 我无法在模式窗口中传输数据。 Maybe I'm doing wrong. 也许我做错了。 Point out the mistakes, tell me how to correctly, please. 指出错误,请告诉我如何正确。 codepen.io/zaytsevav/pen/oBQrOm codepen.io/zaytsevav/pen/oBQrOm

You can store results in a global variable and access it in myModal using index. 您可以将结果存储在全局变量中,并使用索引在myModalmyModal进行访问。

var results = [];
getData('https://api.randomuser.me/1.0/?results=50&nat=gb,us&inc=gender,name,location,email,phone,picture',
  function(data) {

    var html = '';
    for (var i = 0; i < data["results"].length; i++) {
      var name = data["results"][i]["name"]["title"] + ' ' + data["results"][i]["name"]["first"] + ' ' + data["results"][i]["name"]["last"],
        img = data["results"][i]["picture"]["large"];
      html += '<div class="col-md-2 block__user_on" onclick="myModal(this.id)" id="' + [i] + '"> ';
      html += '<img src="' + img + '" title="' + name + '"/>';
      html += '<p class="block__user_on-capitalize">' + name + '</p>';
      html += '</div>';
    }
    document.getElementById("user").innerHTML = html;
    results = data["results"];
  }
);


function myModal(index) {
  var data = results[index];
  console.log(data);
  document.getElementById("Modal").style.display = "block";
  document.getElementById("userModal").innerHTML = '<div>' + data.name.first + " " + data.name.last + ',' + data.location.city + '</div>';

}

demo : https://jsbin.com/juxuhixubu/1/edit?js,output 演示: https : //jsbin.com/juxuhixubu/1/edit?js,输出

This is will make it work and will help you to understand things. 这将使其起作用并帮助您理解事物。 But if the project has more such use cases, you can think about using a modern framework in Javascript, in which this can be done in a much better way. 但是,如果项目中有更多此类用例,则可以考虑使用Javascript中的现代框架,在这种框架中,可以采用更好的方法来完成。

You can solve this by replacing the myModal function with the code below: 您可以通过以下代码替换myModal函数来解决此问题:

function myModal(data) {

    document.getElementById("userModal").innerHTML = "";
    document.getElementById("userModal").appendChild(data);
    document.getElementById("Modal").style.display = "block";

    console.log(data);

} 

It first erases everything that the modal may have had (by setting its innerHTML to nothing) and then it uses the appendChild method in order to add the div (that is in the data string) in the modal window. 它首先清除模式可能具有的所有内容(通过将其innerHTML设置为nothing),然后使用appendChild方法以便在模式窗口中添加div(即data字符串中的div)。

You can test that it works here . 您可以在这里测试它是否有效。

EDIT 编辑

As written on the documentation for appendChild function : 如关于appendChild函数文档中所述

If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position. 如果给定的子代是对文档中现有节点的引用,则appendChild()将其从其当前位置移动到新位置。

That's why the block was removed when you clicked on it. 这就是为什么您单击该块时将其删除的原因。

So, in the code below I used the cloneNode function and the problem was solved. 因此,在下面的代码中,我使用了cloneNode函数 ,问题得以解决。

function myModal(data) {

    console.log(data);

    document.getElementById("userModal").innerHTML = "";
    dataNode = data.cloneNode(true);
    document.getElementById("userModal").appendChild(dataNode);
    document.getElementById("Modal").style.display = "block";

} 

Pleasae let me know if that was helpful ! Pleasae让我知道这是否有帮助!

Put it inside your cycle: 将其放入您的循环中:

document.getElementById("user").insertAdjacentHTML('beforeend', html);
document.getElementById(i).setAttribute('data-person', JSON.stringify(data["results"][i]));

Instead of: 代替:

document.getElementById("user").innerHTML = html;

Then add inside your myModal function: 然后在myModal函数内部添加:

console.log(JSON.parse(data.getAttribute('data-person')));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM