如果单击列表项,我需要按字母顺序显示名称。为此,我正在使用 Vue.js 和 Flex Grid。
我有一个名为 ListAll 的列表项,
单击 ListAll 时,我必须显示 nameList 数组中按名称分组的所有记录。我在显示分组名称时遇到问题。我正在使用 Flex Grid 来显示名称。显示 A 的记录后,B 的记录应从新行开始,但 B 记录不会换行。如何使用 Vue js 或 ES6 或 CSS 语法在每组名称后添加换行符?
我已经添加了这样的换行符<div class="break"></div>
,但它并没有打破记录。
使用这样的 flexbox 网格 CSS 类,记录显示在 4 列中,没有换行符,
Apples Apricots Avocados Almond
Abiu Berry Bananas Boysenberries
Blueberries Breadfruit Carambola Cantaloupe
Cranberries Cherries Custard-Apple
预期结果:
Apples Apricots Avocados Almond
Abiu
Berry Bananas Boysenberries Blueberries
Breadfruit
Carambola Cantaloupe Cranberries Cherries
Custard-Apple
AlphabeticalFruitsName.vue
<template>
<div id="wrapper">
<ul class="listitems">
<li><a
href="javascript:;" @click="userSelection('ListAll')">ListAll</a>
</li>
</ul>
<div class="grid-container">
<div v-for="(name, index) in nameList" :key="name" class="grid-item">
<ul v-for="letter in alphabets" :key="letter">
<li v-if="name.startsWith(letter)"><a>{{ name }}</a></li>
<div class="break"></div>
</ul>
</div>
</div>
</div>
</template>
<script>
export default {
name: 'AlphabeticalFruitsName',
data() {
return {
nameList: [],
alphabets: ['A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',],
names: [
{ name: 'Apples' },{ name: 'Apricots' },{ name: 'Avocados' },{ name: 'Apple Berry' },
{ name: 'Apple guava' },{ name: 'Almond' },{ name: 'Almond' },{ name: 'African Honeysuckle' },
{ name: 'African Plum' },{ name: 'Abiu' },{ name: 'Bananas' },{ name: 'Boysenberries' },
{ name: 'Blueberries' }, { name: 'Bing Cherry' },{ name: 'Breadfruit' },{ name: 'Cantaloupe' },
{ name: 'Carambola' },{ name: 'Cherimoya' },{ name: 'Cherries' },{ name: 'Cranberries' },
{ name: 'Custard-Apple' },{ name: 'Dates' }, { name: 'Honeysuckle' },{ name: 'Gooseberries' },
{ name: 'Grapefruit' },{ name: 'Grapes' },{ name: 'Guava' },{ name: 'Tangerine' },
{ name: 'Kiwi' },{ name: 'Lychee' },{ name: 'Strawberries' }, { name: 'Watermelon' }],
};
},
methods: {
userSelection(listItemName) {
this.nameList = [];
if (listItemName === 'ListAll') {
for (const value of this.names) {
this.nameList.push(value.name);
}
}
},
},
};
</script>
<style lang="scss" scoped>
.listitems {
display: flex;
justify-content: center;
}
ul {
list-style-type: none;
}
.grid-container {
display: grid;
grid-template-columns: auto auto auto auto;
padding: 10px;
}
.grid-item {
text-align: center;
}
.new-line:last-child {
content: '\a';
white-space: pre;
}
.break {
flex-basis: 100%;
height: 0;
}
</style>