Ant Design of Vue中a-table组件斑马线

        当使用Ant Design of Vue前端UI中的表格组件a-table时,Ant官方默认是没有像eleme UI中表格组件el-table的stripe(斑马线)属性的。

        设置antdv中a-table组件的斑马线的方法有:

①CSS样式设置

/* 表格斑马样式 **/
/deep/.ant-table-tbody tr:nth-child(2n)
{
  background-color:#d9d4d4;
}

注:此①方法可能会不起效!

②JS函数设置

  // 对表格行进行斑马行格式显示
  renderStripe () {
	const table = document.getElementsByClassName('ant-table-row')
	if (table.length > 0) {
	  const rowList = [...table]
	  for (let i = 0; i < rowList.length; i++) {
		const row = rowList[i]
		const index = rowList.indexOf(row)
		if (index % 2 != 0) {
		  row.style.backgroundColor = '#d9d4d4'
		} else {
		  row.style.backgroundColor = '#ffffff'
		}
	  }
	}
  }

在updated或者mounted中延时调用!

updated() {
  this.renderStripe()
}

注:此②方法,如果a-table组件中columns列定义了fixed: 'left'或fixed: 'right',则会出现斑马线错乱现象,特别的难看,没有之一,如图所示:

最终解决办法:使用a-table组件的rowClassName来处理

先定义两个斑马线样式:

.odd {
  background-color: #ffffff;
}
.even {
  background-color: #d9d4d4;
}

再在a-table组件定义处理:

:rowClassName="(record, index) => index % 2 == 0 ? 'odd' : 'even'"

最终a-table组件的斑马线都不会错乱!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 antd-vue 的 a-table 组件使用上下键选择行,您需要编写一些 JavaScript 代码来实现这个功能。您可以使用 `keydown` 事件来监听键盘事件,并根据按下的键来执行相应的操作。以下是一个可以实现上下键选择行的示例代码: ```vue <template> <a-table :columns="columns" :data-source="data" @row-click="handleRowClick" :row-class-name="rowClassName" /> </template> <script> export default { data() { return { selectedRow: null, data: [ { id: 1, name: 'John Doe' }, { id: 2, name: 'Jane Smith' }, { id: 3, name: 'Bob Johnson' }, ], columns: [ { title: 'ID', dataIndex: 'id', }, { title: 'Name', dataIndex: 'name', }, ], }; }, methods: { handleKeyDown(event) { if (event.keyCode === 38) { // up arrow key if (this.selectedRow === null) { this.selectedRow = this.data[this.data.length - 1]; } else { const index = this.data.indexOf(this.selectedRow); this.selectedRow = this.data[index === 0 ? this.data.length - 1 : index - 1]; } } else if (event.keyCode === 40) { // down arrow key if (this.selectedRow === null) { this.selectedRow = this.data[0]; } else { const index = this.data.indexOf(this.selectedRow); this.selectedRow = this.data[(index + 1) % this.data.length]; } } }, handleRowClick(row) { this.selectedRow = row; }, rowClassName(row) { return row === this.selectedRow ? 'selected-row' : ''; }, }, mounted() { window.addEventListener('keydown', this.handleKeyDown); }, beforeDestroy() { window.removeEventListener('keydown', this.handleKeyDown); }, }; </script> <style scoped> .selected-row { background-color: #e6f7ff; } </style> ``` 在这个示例,我们使用 `selectedRow` 变量来跟踪当前选的行。当用户按下上下箭头键时,我们会根据当前选的行来更新 `selectedRow` 变量。我们还在表格的 `@row-click` 事件处理用户单击行的情况,以更新 `selectedRow` 变量。最后,我们使用 `rowClassName` 属性来为选的行添加一个特殊的 CSS 类,以便用户可以看到它被选了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值