javascript - 按字母表排列在不同列中的垂直顺序
问题描述
A | B | D | F
A | C | E | G
B | C |
我有一组数据:
- 需要按字母垂直对齐;
- 需要为大屏幕订购 4 列数组;
- 需要为中等屏幕订购 3 列数组;
- 需要为小屏幕订购 2 列的数组;
- 顺序必须按字母顺序排列;
3列示例
A | C | E
A | C | F
B | D | G
B |
CSS 中的column-count是目前最好的,但在响应式上工作错误
我在考虑 Vue.js,它取决于屏幕大小(可以添加/删除列)并在列之间移动数组数据
我为 HTML 使用引导程序
这是我所做的代码,但这还不足以满足需要https://codepen.io/alexander-chumak/pen/JvyLPJ的工作
解决方案
由于您使用的是 Vue,因此我将按列对项目使用计算值。这样,当 items 或 columnCount 更改时,您无需担心自己重新运行任何内容。
您可以执行以下操作:
data () {
return {
columnCount: 2,
items: [],
}
},
created () {
// You should debounce this callback.
window.addEvenetListener('resize', this.updateColumnCount)
this.updateColumnCount()
},
destroyed () {
window.removeEventListener('resize', this.updateColumnCount)
},
methods: {
updateColumnCount () {
// updat the columnCount here like: this.columnCount = 2
},
},
computed: {
itemsByColumn () {
return this.items.reduce((columns, item, index) => {
const columnNumber = index % this.columnCount
if (!columns[columnNumber]) {
columns[columnNumber] = []
}
columns[columnNumber].push(item)
return columns
})
},
},
然后,在您的模板中,您可以执行以下操作:
<div class="flex">
<div class="column" v-for="(column, index) in itemsByColumn" :key="index">
<div class="item" v-for="item in column" :key="item.id">
{{ item.name }}
</div>
</div>
</div>
这段代码没有经过测试,但它应该能让你大部分时间到达那里。
推荐阅读
- c++ - 有没有办法用clang忽略重新定义?
- python - 如何在 python 中创建具有唯一列值和另一个数据框列的值计数的数据框?
- python - 在 Python 中进行字符串匹配时,有没有办法提高匹配性能?
- typescript - How to change NestJS Decorator string parameter when importing library
- r - 使用 purrr 跳过由于数据中的空列表而遇到的错误
- html - 用于单页应用程序的 ARIA Live Regions
- python - 我该如何解决这个功能?
- c# - UWP:如何在不减速的情况下运行任务?
- typescript - 如果对象的布尔属性为真,Typescript 会阻止将对象添加到对象数组中
- javascript - 创建对象时函数关键字是可选的吗?