admin管理员组文章数量:1294664
There is an option to display more
text, or less
, but the @click
event applies for the all elements
and the all elements display all text.
I understand, that I need to pass some value to identify individual element, that the value would not apply to all elements, but at the moment stuck and would appriciate help.
HTML:
<tr v-for="script in scripts.slice(0, 5)">
<td>{{script.key}}</td>
<td v-if="!readMore">{{script.script_content.substring(0, 200) + ".."}}</td>
<td v-if="readMore">{{script.script_content}}</td>
<button @click="showMore" v-if="!readMore" class="btn btn-primary">Show more</button>
<button @click="showLess" v-if="readMore" class="btn btn-primary">Show less</button>
</tr>
Vue data and methods:
data: () => ({
readMore: false,
}),
methods: {
showMore() {
this.readMore = true;
},
showLess() {
this.readMore = false;
},
Array values:
How could I make a method apply only for an individual element?
In this situation, there is no need to create a ponent, because this case will be used only on one page.
There is an option to display more
text, or less
, but the @click
event applies for the all elements
and the all elements display all text.
I understand, that I need to pass some value to identify individual element, that the value would not apply to all elements, but at the moment stuck and would appriciate help.
HTML:
<tr v-for="script in scripts.slice(0, 5)">
<td>{{script.key}}</td>
<td v-if="!readMore">{{script.script_content.substring(0, 200) + ".."}}</td>
<td v-if="readMore">{{script.script_content}}</td>
<button @click="showMore" v-if="!readMore" class="btn btn-primary">Show more</button>
<button @click="showLess" v-if="readMore" class="btn btn-primary">Show less</button>
</tr>
Vue data and methods:
data: () => ({
readMore: false,
}),
methods: {
showMore() {
this.readMore = true;
},
showLess() {
this.readMore = false;
},
Array values:
How could I make a method apply only for an individual element?
In this situation, there is no need to create a ponent, because this case will be used only on one page.
Share Improve this question edited Apr 23, 2020 at 7:43 ViktorasssIT asked Apr 23, 2020 at 7:10 ViktorasssITViktorasssIT 3872 gold badges9 silver badges28 bronze badges 2-
Try creating only one
<td>
and one button withtoggle()
function onclick, which change text version to display, (full or cut) – winiar Commented Apr 23, 2020 at 7:20 - Thanks. Could you make an example with jsfiddle? – ViktorasssIT Commented Apr 23, 2020 at 7:24
2 Answers
Reset to default 6Create an object to hold a hash of readMore
toggles, one for each item:
data: () => ({
readMore: {},
}),
The keys will be the item id. Test those in the template and also set them in the buttons by passing that id:
<td v-if="!readMore[script.id]">{{script.script_content.substring(0, 200) + ".."}}</td>
<td v-if="readMore[script.id]">{{script.script_content}}</td>
<button @click="showMore(script.id)" v-if="!readMore[script.id]" class="btn btn-primary">Show more</button>
<button @click="showLess(script.id)" v-if="readMore[script.id]" class="btn btn-primary">Show less</button>
And set values in methods
by passing the item id and toggling it:
methods: {
showMore(id) {
this.$set(this.readMore, id, true);
},
showLess(id) {
this.$set(this.readMore, id, false);
},
}
Use index instead of this.$set
data: () => ({
readMore: [false],
}),
methods: {
showMore(index) {
this.readMore[index] = true;
},
showLess(index) {
this.readMore[index] = false;
},
<tr v-for="(script,index) in scripts.slice(0, 5)">
<td>{{script.key}}</td>
<td v-if="!readMore[index]">{{script.script_content.substring(0, 200) + ".."}}</td>
<td v-if="readMore[index]">{{script.script_content}}</td>
<button @click="showMore(index)" v-if="!readMore[index]" class="btn btn-primary">Show more</button>
<button @click="showLess(index)" v-if="readMore[index]" class="btn btn-primary">Show less</button>
</tr>
本文标签:
版权声明:本文标题:javascript - How to create a show moreless button which applies to individual element with vue.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741604670a2387904.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论