admin管理员组文章数量:1414605
I wanted to display three different lists of json in a table. The first contains URLs, the second URLs to google maps and the last messages.
To render it I have a template like this:
<script type="text/x-template" id="grid-template">
<table class="table">
<thead>
<tr>
<th v-for="key in columns"
@click="sortBy(key)"
:class="{ active: sortKey == key }">
${ key | capitalize }
<span class="arrow" :class="sortOrders[key] > 0 ? 'asc' : 'dsc'">
</span>
</th>
</tr>
</thead>
<tbody>
<tr v-for="entry in filteredData">
<td v-for="key in columns">${isUrl(key,entry[key])}</td>
</tr>
</tbody>
</table>
</script>
Vueponent('demo-grid', {
template: '#grid-template',
delimiters: ['${', '}'],
props: {
data: Array,
columns: Array,
filterKey: String
},
data: function () {
var sortOrders = {}
this.columns.forEach(function (key) {
sortOrders[key] = 1
})
return {
sortKey: '',
sortOrders: sortOrders
}
},
puted: {
filteredData: function () {
var sortKey = this.sortKey
var filterKey = this.filterKey && this.filterKey.toLowerCase()
var order = this.sortOrders[sortKey] || 1
var data = this.data
if (filterKey) {
data = data.filter(function (row) {
return Object.keys(row).some(function (key) {
return String(row[key]).toLowerCase().indexOf(filterKey) > -1
})
})
}
if (sortKey) {
data = data.slice().sort(function (a, b) {
a = a[sortKey]
b = b[sortKey]
return (a === b ? 0 : a > b ? 1 : -1) * order
})
}
return data
}
},
filters: {
capitalize: function (str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
},
methods: {
sortBy: function (key) {
this.sortKey = key
this.sortOrders[key] = this.sortOrders[key] * -1
},
isUrl: function (key, str){
if (key == "url"){
var exp = /^((?:https?|ftp):\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/ig;
return str.replace(exp,"<a :href='$1'>$1</a>");
}
else{
console.log("ej")
return str
}
}
}
})
I wanted to create a hyperlink when the columns' key name is "url"
, but vue auto-escapes this. How can I manage to replace text for a hyperlink?
An example of the data:
[{
"date": "2017-08-09 18:25:06.226631",
"id": 5,
"private": true,
"reviewed": false,
"title": "To Protect Voting, Use Open-Source Software - NYTimes",
"url": "j.mp/2upm633",
"user": 1
}]
I wanted to display three different lists of json in a table. The first contains URLs, the second URLs to google maps and the last messages.
To render it I have a template like this:
<script type="text/x-template" id="grid-template">
<table class="table">
<thead>
<tr>
<th v-for="key in columns"
@click="sortBy(key)"
:class="{ active: sortKey == key }">
${ key | capitalize }
<span class="arrow" :class="sortOrders[key] > 0 ? 'asc' : 'dsc'">
</span>
</th>
</tr>
</thead>
<tbody>
<tr v-for="entry in filteredData">
<td v-for="key in columns">${isUrl(key,entry[key])}</td>
</tr>
</tbody>
</table>
</script>
Vue.ponent('demo-grid', {
template: '#grid-template',
delimiters: ['${', '}'],
props: {
data: Array,
columns: Array,
filterKey: String
},
data: function () {
var sortOrders = {}
this.columns.forEach(function (key) {
sortOrders[key] = 1
})
return {
sortKey: '',
sortOrders: sortOrders
}
},
puted: {
filteredData: function () {
var sortKey = this.sortKey
var filterKey = this.filterKey && this.filterKey.toLowerCase()
var order = this.sortOrders[sortKey] || 1
var data = this.data
if (filterKey) {
data = data.filter(function (row) {
return Object.keys(row).some(function (key) {
return String(row[key]).toLowerCase().indexOf(filterKey) > -1
})
})
}
if (sortKey) {
data = data.slice().sort(function (a, b) {
a = a[sortKey]
b = b[sortKey]
return (a === b ? 0 : a > b ? 1 : -1) * order
})
}
return data
}
},
filters: {
capitalize: function (str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
},
methods: {
sortBy: function (key) {
this.sortKey = key
this.sortOrders[key] = this.sortOrders[key] * -1
},
isUrl: function (key, str){
if (key == "url"){
var exp = /^((?:https?|ftp):\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/ig;
return str.replace(exp,"<a :href='$1'>$1</a>");
}
else{
console.log("ej")
return str
}
}
}
})
I wanted to create a hyperlink when the columns' key name is "url"
, but vue auto-escapes this. How can I manage to replace text for a hyperlink?
An example of the data:
[{
"date": "2017-08-09 18:25:06.226631",
"id": 5,
"private": true,
"reviewed": false,
"title": "To Protect Voting, Use Open-Source Software - NYTimes.",
"url": "j.mp/2upm633",
"user": 1
}]
Share
Improve this question
edited Aug 9, 2017 at 18:02
thanksd
55.7k23 gold badges165 silver badges154 bronze badges
asked Aug 9, 2017 at 17:45
Yábir GarciaYábir Garcia
3394 silver badges18 bronze badges
1 Answer
Reset to default 4You can format the url in the template like so:
<tr v-for="entry in filteredData">
<td v-for="key in columns">
<template v-if="key === 'url'">
<a :href="entry[key]">${ entry[key] }</a>
</template>
<template v-else>
${ entry[key] }
</template>
</td>
</tr>
本文标签: javascriptVue js insert link in tableStack Overflow
版权声明:本文标题:javascript - Vue js insert link in table - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745195082a2647097.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论