简体   繁体   English

Bootstrap PopUp模态参考数组

[英]Bootstrap PopUp Modal Reference Array

How will i reference this popup modal to a specific title of array? 我如何将这个弹出模式引用到特定的数组标题? Like i have 4 titles in an array. 就像我在数组中有4个标题。 It outputs four lists of titles. 它输出四个标题列表。 But when i open each of the popup modal, it only shows the title of the first. 但是当我打开每个弹出模式时,它仅显示第一个的标题。 Example, when i click the popup modal of the second title, it still shows the the title of the first. 例如,当我单击第二个标题的弹出模式时,它仍然显示第一个标题的标题。 How can i address this? 我该如何解决?

function getThings(){

let selected = localStorage.getItem('token');
console.log(selected);
fetch('http://sample_website/things', {
    method: 'GET',
    headers: {'Content-Type': 'application/json',
    Authorization: 'Bearer ' + selected},      
})
.then(data => data.json())
.then(data => { 
    let output = document.getElementById("display");
    let iterable = data.data.data;
    for(value of iterable){
         output.innerHTML += `<div class="panel panel-primary">
                                <div class="panel-heading"><button class="btn btn-default view" data-toggle="modal" data-target="#viewmodal">View</button><button class="btn btn-danger delete">Delete</button>
                                    <h5>${value.created_at}</h5></div>
                                <div class="panel-body"><h3>${value.title}</h3>
                            </div>

                            <div class="modal fade" id="viewmodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                                <div class="modal-dialog" role="document">
                                    <div class="modal-content">
                                        <form>
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                <h4 class="modal-title text-info" id="myModalLabel"><h3 class="text-center">${value.title}</h3>
                                            </div>
                                            <div class="modal-body">            
                                                ${value.body}
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>`;    

    }

})
.catch((err) => {
    console.error(err);
})

} }

How can i address this? 我该如何解决?

I guess you could try to loop over the data twice, and FIRST save your values to an array. 我猜您可以尝试将数据循环两次,然后首先将您的值保存到数组中。 You could then map your values to the PopUps depending on they array key. 然后,您可以根据它们的数组键将值映射到PopUp。

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map

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

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