简体   繁体   English

Vuejs在模板中显示多个值

[英]Vuejs Show multiple values in template

I have multiple attachment links and attachment names.我有多个附件链接和附件名称。 This is what the console shows:这是控制台显示的内容:

在此处输入图像描述

The problem is that only the last link and name are shown:问题是只显示最后一个链接和名称:

在此处输入图像描述

I use this in the template:我在模板中使用它:

<div class="d-inline col-lg-20 col-md-60 col-sm-40" padding="20px" >
    <th>Files</th>
    <tr>
        <div class="d-inline col-lg-20 col-md-60 col-sm-40" padding="20px">
        {{attachmentName}}
        {{attachmentUrl}}
        </div>
    </tr>
</div>

This in data:这在数据中:

data() {
            return {
                selectedFile: "",
                progress: 0,
                attachmentName: [],
                attachmentUrl: [],
            };
        },

and this in JavaScript:这在 JavaScript 中:

$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items('" + itemId + "')/AttachmentFiles",
    method: "GET",
    async: false,
    headers: { "Accept": "application/json; odata=nometadata" },
    success: function (data) {
        $(data.value).each(function (i) {
            attachments.items.push({
                extension: this.FileName.substring(this.FileName.lastIndexOf('.') + 1),
                name: this.FileName,
                path: this.ServerRelativeUrl
            });
            attachmentLink = "site.sharepoint.com" + this.ServerRelativeUrl;
            attachmentName = this.FileName;

            self.attachmentUrl = attachmentLink;
            self.attachmentName = attachmentName;

            attachments.count++;
            console.log("attachments: " + attachmentLink);
            console.log("name: " + attachmentName);
        });
    }
});

The problem is these two lines:问题是这两行:

self.attachmentUrl = attachmentLink;
self.attachmentName = attachmentName;

I don't know how to fix this issue because the console.log shows 2 items, while the template only shows the last one.我不知道如何解决此问题,因为 console.log 显示 2 项,而模板仅显示最后一项。 I experimented a bit using for loops but can't get all the attachment links and names into the template.我使用 for 循环进行了一些实验,但无法将所有附件链接和名称都放入模板中。

Any help is appreciated!任何帮助表示赞赏!

I couldn't see where attachments is defined in your code but self.attachmentUrl and self.attachmentName are initialised as arrays but then your assigning them a string.我看不到attachments在您的代码中定义的位置,但self.attachmentUrlself.attachmentName被初始化为 arrays 但随后您为它们分配了一个字符串。

You could push to the array instead like self.attachmentUrl.push(attachmentLink);您可以推送到数组,而不是像self.attachmentUrl.push(attachmentLink); but then you have the problem of looping through two arrays to show your data (assuming you want to show the urls and names together).但是随后您遇到了循环两个 arrays 以显示您的数据的问题(假设您想同时显示网址和名称)。

I think it would be better to scrap your two arrays and just have one with properties url and name .我认为最好废弃你的两个 arrays 并只拥有一个具有urlname属性的。 I've created a fiddle below which does that using v-for to loop through the array.我在下面创建了一个小提琴,它使用v-for循环遍历数组。

 Vue.config.productionTip = false; Vue.config.devtools = false; new Vue({ el: "#app", data: () => { return { files: [] } }, methods: { downloadFiles() { let datas = [{ name: "Test", attachment: ".pdf" }, { name: "Hello World", attachment: ".png" }, { name: "SO", attachment: ".html" }] //Get data from api for (let i = 0; i < datas.length; i++) { this.files.push({ name: datas[i].name, attachment: datas[i].attachment }); //Work out what name and attachment are } } } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <button type="button" @click="downloadFiles">Download Files</button> <h5>Files</h5> <ul> <li v-for="file in files"> {{file.name}}{{file.attachment}} </li> </ul> </div>

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

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