Vue中Hottable的使用

安装

npm install handsontable @handsontable/vue

引入

<template>
    <HotTable :root="root" :data="tableValue" ref="testHot" :settings="hotSettings"></HotTable>
</template>

<script>
import HotTable from '@handsontable/vue';
export default {
    components: { HotTable }
}
</script>

<style src="../node_modules/handsontable/dist/handsontable.full.css"></style>

属性及使用

以下内容有涉及到dom操作可以看我的另一篇文章dom操作

<script>
export default {
    data() {
		return {
			hotSettings: {
			    rowHeaders: true,//行表头,可以使布尔值(行序号),可以使字符串(左侧行表头相同显示内容,可以解析html),也可以是数组(左侧行表头单独显示内容)。
			    minSpareCols: 2, //列留白
			    minSpareRows: 2,//行留白
			    minRows: 20, //最小行列
			    maxRows: 20000, //最大行列
			    currentRowClassName: 'currentRow', //为选中行添加类名,可以更改样式
			    currentColClassName: 'currentCol',//为选中列添加类名
			    autoWrapRow: true, //自动换行
			    manualColumnFreeze: true, //手动固定列
			    manualColumnMove: true, //手动移动列
			    manualRowMove: true,   //手动移动行
			    manualColumnResize: true,//手工更改列距
			    manualRowResize: true,//手动更改行距
			    comments: true, //添加注释
			    customBorders: [], //添加边框
			    columnSorting: true, //排序
			    stretchH: "all", //根据宽度横向扩展,last:只扩展最后一列,none:默认不扩展
			    fillHandle: true, //选中拖拽复制 possible values: true, false, "horizontal", "vertical"
			    fixedColumnsLeft: 2,//固定左边列数
			    fixedRowsTop: 2,//固定上边列数
			    mergeCells: [
			        //合并
			        // {row: 1, col: 1, rowspan: 3, colspan: 3},  //指定合并,从(1,1)开始行3列3合并成一格
			        // {row: 3, col: 4, rowspan: 2, colspan: 2}
			    ],
			    colHeaders: ['表头'],//自定义列表头or 布尔值
			    colWidths: ['列宽度'],//设置每一列的宽度
			    columns: [{ data: "字段名", type: "text"文本/"dropdown"下拉菜单/"date"日期, readOnly: true//只读 },],//添加每一列的数据类型和一些配置
			    cells: this.rowReadonly,            //为单元格设置属性
			    afterRender: this.setRowColStyle    //渲染完成后调用
			}
		}
	},
	methods:{
		//设置表格的锁定函数,满足条件的变为readonly
		rowReadonly(row, col, prop) {
		    var cellProperties = {};
		    if(col==0){//列数是1的
		        cellProperties.readOnly = true;
		    }
		    if(col==9 && row < 2){//第10列第3行的
		        cellProperties.readOnly = true;
		    }
		    if(row==9){//行数是10的
		        cellProperties.readOnly = true;
		    }
		    return cellProperties;
		},
		
		// 渲染结束后调用
		setRowColStyle(){
		    const testHot = this.$refs.testHot.hotInstance;
		    let col = '';
		    let colname = ''
		    
		    this.hotSettings.columns.some((item, index) => {//获取想要操作的字段col
		        if (item.data == "字段名") {
		            col = index;
		            colname = item.data
		            return true;
		        }
		    });
		    this.tableValue.forEach((item, row) => {
		        if (row >= 0 && colname == '字段名') {
		            try {//对该列数据操作的方法,这里举例把他替换为按钮
		                const cellele = testHot.getCell(row, col);
		                const cellmet = testHot.getCellMeta(row, col);
		                let btn = document.createElement("a");
		                cellele.style = "color: aquamarine !important;text-align: center;";
		                btn.textContent = "操作";//按钮文字设置
		                btn.style = "font-size:10px; border-bottom: 1px solid #2d8cf0";//按钮行内样式设置
		                btn.addEventListener("click", event => {});//点击事件的设置
		                // console.log(cellele);
		                cellele.innerHTML = ""; // 清空单元格子集
		                cellele.appendChild(btn); //添加按钮到单元格
		            } catch (e) {
		                console.log(e);
		            }
		        }
		    });
		}
	}
}
</script>

官方文档

https://handsontable.com/features

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3使用Handsontable的updateData,需要先引入Handsontable库,并在Vue组件使用Handsontable组件。 具体步骤如下: 1. 安装Handsontable库 ``` npm install handsontable ``` 2. 在Vue组件引入Handsontable ``` import Handsontable from 'handsontable'; ``` 3. 在Vue组件使用Handsontable组件,并传入数据 ``` <template> <div> <hot-table :data="data"></hot-table> </div> </template> <script> import Handsontable from 'handsontable'; export default { name: 'MyComponent', data() { return { data: [ ["", "Ford", "Volvo", "Toyota", "Honda"], ["2019", 10, 11, 12, 13], ["2020", 20, 21, 22, 23], ["2021", 30, 31, 32, 33] ] }; }, components: { HotTable: Handsontable.vue.HotTable } }; </script> ``` 4. 在需要更新数据的方法,调用Handsontable的updateData方法 ``` methods: { updateData() { this.data[1][1] = 100; this.data[2][1] = 200; this.data[3][1] = 300; this.$refs.hotTable.hotInstance.updateData(this.data); } } ``` 其,`$refs.hotTable.hotInstance`用于获取Handsontable实例,`updateData`方法用于更新数据。 完整的示例代码如下: ``` <template> <div> <hot-table :data="data" ref="hotTable"></hot-table> <button @click="updateData">Update Data</button> </div> </template> <script> import Handsontable from 'handsontable'; export default { name: 'MyComponent', data() { return { data: [ ["", "Ford", "Volvo", "Toyota", "Honda"], ["2019", 10, 11, 12, 13], ["2020", 20, 21, 22, 23], ["2021", 30, 31, 32, 33] ] }; }, components: { HotTable: Handsontable.vue.HotTable }, methods: { updateData() { this.data[1][1] = 100; this.data[2][1] = 200; this.data[3][1] = 300; this.$refs.hotTable.hotInstance.updateData(this.data); } } }; </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梓齐丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值