使用索引作为ID VueJS的动态模态

时间:2018-09-10 13:51:57

标签: javascript vue.js sharepoint-2013

希望有人可以提供帮助。

这是我第一次使用Vue,有人要求我创建一个团队页面,该页面从列表中提取数据,将团队名称按名称分组在一个数组中,并以卡片格式显示。我已经做到了所有这些,但是当点击卡片时,我希望所有的团队成员及其角色(例如,开发人员,产品经理等)以模态形式显示在团队形象中

我已经使用索引作为每张卡片的ID,这可以正常工作,但是我不确定如何在单击卡片时将其传递给模式以显示每个团队的正确成员。我尝试了在这里找到的各种方法,但是似乎没有一种方法与我在做的事情完全相关,由于缺乏Vue知识,我无法使用普通的HTML / JS轻松完成此任务。

值得一提的是,由于PC的工作权限以及它是基于SharePoint构建的,因此我不得不使用Vue CDN并以1个HTML文件实现它,而不是使用组件。

非常感谢,我希望这很有意义

这是我的HTML:

        <div class="container">
        <h1 class="page-heading">MEET<span style="color:#ff0000">IN</span>G THE TEAM</h1>
        <p class="intro-text">This is a chance for you to tell your Country and the other teams who you are and what you stand for. You’ll
            need a team name, team mascot image, who’s in your team and what you want to say to the world.</p>
        <br>
        <button class="btn btn-outline-light" type="button" role="button" data-target="#create-modal" data-toggle="modal" id="create-team">Create a New Team</button>
        <button class="btn btn-outline-light" type="button" role="button" data-target="#create-modal" data-toggle="modal" id="update-team">Update Your Team</button>

        <div class="row" id="team-cards">
            <div class="col-md-4" v-for="(team, index) in teams" :key="team[0].teamname" v-if="team[0].teamname !== 'No Team'">
                <a href="'#teamModal' + index" data-toggle="modal">
                <div class="card" v-bind:id="index">
                    <img v-bind:src="teammate.teamimage" class="card-img-top" v-for="teammate in team" v-if="teammate.teamimage !== null" :key="teammate.teamimage">
                    <div class="card-body">
                        <h5 class="card-title"><strong>{{ team[0].teamname }}</strong></h5>
                        <p class="card-text" v-for="teammate in team" v-if="teammate.teamdescription !== null" :key="teammate.teamdescription">{{ teammate.teamdescription }}</p>
                    </div>
                    <div class="card-footer">
                        <img class="group-image" v-for="teammate in team" v-if="teammate.hackertype !== 'Developer'" :key="teammate.hackertype" src="https://community.global.hsbc/sites/DigiHub/SiteAssets/hub_assetts/Hack%20Teams/Images/red%20group.png">
                        <!-- <img class="group-image" v-for="teammate in team" v-else-if="teammate.hackertype !== 'Product Manager' || teammate.hackertype == 'Developer'" :key="teammate.hackertype" src="https://community.global.hsbc/sites/DigiHub/SiteAssets/hub_assetts/Hack%20Teams/Images/yellow%20group.png"> -->
                        <!-- <img class="group-image" v-for="teammate in team" v-if="teammate.hackertype == 'Developer' || teammate.hackertype == 'Product Manager'" :key="teammate.hackertype" src="https://community.global.hsbc/sites/DigiHub/SiteAssets/hub_assetts/Hack%20Teams/Images/green%20group.png"> -->
                        <a href="https://community.global.hsbc/sites/DigiHub/SitePages/Hack-Chat.aspx" target="_blank">
                            <img class="chat-image" src="https://community.global.hsbc/sites/DigiHub/SiteAssets/hub_assetts/Hack%20Teams/Images/chat.png">
                        </a>
                    </div>
                </div>
                </a>
            </div>
        </div>
    </div>

    <!-- Team Modal -->
    <div class="modal fade" v-bind:id="['teamModal'+index]" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title"><b>{{ team[0].teamname }}</b></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <br>
                <div class="modal-body" v-for="teammate in team">   
                    <p>{{ teammate.name }}</p>
                    <p>{{ teammate.hackertype }}</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

还有我的VueJS:

$(function () {

var appTeamCards = new Vue({
    el: '#app-teams',
    data: {
        teams: [],
        teamMembers: [],
    },
    mounted: function() {
        this.getTeams();
    },
    methods: {
        getTeams: function() {
            var self = this;
            $.ajax({
                url: '/sites/DigiHub/_api/web/lists/getbytitle(\'global-hackathon-reg\')/items?$orderby=Modified desc',
                method: "GET",
                headers: {
                    "Accept": "application/json;odata=verbose"    
                },
                success: function (data) {
                    var posts = data.d.results;
                    readData(posts);
                    console.log(self.teams);
                },
            });

            function readData(data) {
                self.teams = groupBy(data, "teamname");  
            };

            function groupBy(collection, property) {
                var i = 0, val, index,
                    values = [], result = [];
                for (; i < collection.length; i++) {
                    val = collection[i][property];
                    index = values.indexOf(val);
                    if (index > -1)
                        result[index].push(collection[i]);
                    else {
                        values.push(val);
                        result.push([collection[i]]);
                    }
                }
                return result;
            }

        },

    }
}); 

$('#update-team').click(function(){
    $("#new-member-form").hide();
    $("#reg-another-update").show();
    $("#update-form").show();
});

$('#create-team').click(function(){
    $("#new-member-form").show();
    $("#update-form").show();
    $("#reg-another-update").hide();
});

$('#reg-another-update').click(function(){
    $("#new-member-form").show();
    $("#reg-another-update").hide();
    $("#update-form").hide();
});

$('#submit-team-btn').click(function(){
    $("#update-form").attr("disabled", "disabled");
});

});

1 个答案:

答案 0 :(得分:1)

似乎我有正确的解决方案,但语法略有错误。我做了以下修改,我的代码现在可以正常工作了。

卡片锚点参考应编写如下:

<a :href="'#teamModal' + index" data-toggle="modal">

模式ID:

<div class="modal fade" :id="'teamModal' + index" v-for="(team, index) in teams" :key="team[0].teamname" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">