简体   繁体   English

使用来自特定按钮的信息显示模态

[英]Display modal with information from a specific button

I have a Django template that renders a little bit of data about the user.我有一个 Django 模板,用于呈现有关用户的一些数据。 It is a for list that goes through and creates this element for each user.它是一个 for 列表,它遍历并为每个用户创建此元素。 I want to be able to click on that and have a modal appear with all of their information.我希望能够点击它并显示一个包含所有信息的模式。 So if I click on user1 I get user1's information, and so on.所以如果我点击 user1,我会得到 user1 的信息,依此类推。 I am not sure how to do this.我不知道该怎么做。

I am currently trying to use JavaScript to do this.我目前正在尝试使用 JavaScript 来做到这一点。 The problem is that I cannot get the data from the specific element into JavaScript.问题是我无法将数据从特定元素获取到 JavaScript 中。 I have the modal working, in that it pops up.我有模态工作,因为它弹出。 However, I cannot find a way to retrieve the data from the specific user clicked on.但是,我找不到从单击的特定用户那里检索数据的方法。

#dashboard.html
{% for party in parties %}
    <div class="userInfo">
        <img user="{{party}}" src="{% static 'restaurants/images/userIcon.png' %}">
        <div class="userText">
            <p>{{party.lastName}} </p></br>
        </div>
        <button name="remove"><a href="/restaurants/removeParty/{{party.id}}">Remove</a></button>
    </div>
{% endfor %}

I just need to get data from a specific user into the js from the specifc button clicked.我只需要通过单击的特定按钮将来自特定用户的数据获取到 js 中。 Maybe there is a better way to do this?也许有更好的方法来做到这一点? I am not sure.我不确定。 I just need be able to populate a modal with data from the specific user clicked.我只需要能够使用来自点击的特定用户的数据填充模态。 The above code shows the html for the loop that creates the user info.上面的代码显示了创建用户信息的循环的 html。

It may be a bit verbose depending on how large your object is but you could use data attributes:根据您的对象有多大,它可能有点冗长,但您可以使用数据属性:

{% for party in parties %}
    <div class="userInfo" data-lastName="{{party.lastName}}" data-etc="{{party.etc}}">
        <img user="{{party}}" src="{% static 'restaurants/images/userIcon.png' %}">
        <div class="userText">
            <p>{{party.lastName}} </p></br>
        </div>
        <button name="remove"><a href="/restaurants/removeParty/{{party.id}}">Remove</a></button>
    </div>
{% endfor %}

and then just access them when you click to open the modal:然后在单击打开模态时访问它们:

const users = document.querySelectorAll('.userInfo');

for (let i = 0; i < users.length; i++) {
  users[i].addEventListener('click', function() {
    const userData = {
           lastName: this.dataset.lastName,
           etc: this.dataset.etc
        }
    openMyModal(userData);
  })
}
})

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

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