Vue: feature request - list rendering with first and last option

Created on 9 Nov 2016  路  2Comments  路  Source: vuejs/vue

I would like to suggest a feature which we could manage a first and a last pointer when looping through some object, as of below:
// Array $authors = [ 'john', 'mike', 'taylor'];
When using this array in a regular field, which is not listed (<li></li>), would be nice to have some thing like that, in order to identify when the loop starts and when it ends:
<template v-for="author in authors"> {{ author }}<span v-if="! loop.last">, </span><span v-if="! loop.last">.</span> </template>

Which would result to:

john, mike, taylor.

Just the idea, not sure if would be better to have loop.first/last or even author._first/last.

Thanks!

Most helpful comment

Just create a method:

Vue.prototype.$last = function (item, list) {
  return item === list[list.length - 1]
}

Then:

<span v-if="!$last(author, authors)">...</span>

All 2 comments

Just create a method:

Vue.prototype.$last = function (item, list) {
  return item === list[list.length - 1]
}

Then:

<span v-if="!$last(author, authors)">...</span>

Thanks!

Was this page helpful?
0 / 5 - 0 ratings