Tabulator使用

Tabulator

  1. 验证validator
  2. cell编辑-自定义editor样式
  3. 列相关属性
  4. 创建Tabulator
  5. 列全选,取消全选(包含定义)
  6. 解析Tabulator相关
  7. 默认绑定的resize事件的取消

1-validator(时间edit的success函数):

startTime ,endTime(数据format验证结果)
   //定义在列模块,验证失败会添加tabulator-validation-fail的class样式
   validator:"regex:^([0-9]){2}:([0-9]){2}$"
   Validate.prototype.validators
   //validator初始化
   Validate.prototype.initializeColumn 
   // 验证函数
   Validate.prototype.validate             

2-自定义editor模板(达到某个条件时cell可以编辑)

/**
* param
* cell:编辑的元素本身
* onRendered:编辑过程回调函数(编辑时获取焦点,tabulator会自动添加【tabulator-editing】class属性)
* success :编辑成功回调函数
* cancel : 编辑失败回调函数
*/
isShiftCellEditable = function (cell,onRendered,success,cancel) {
	var cellEditedStatusDOM = $("<input>").attr("value",cell._cell.value);
	onRendered(function () {
		//TODO preventScroll
        cellEditedStatusDOM[0].focus({ preventScroll: true });
        cellEditedStatusDOM[0].style.height = "100%";
    });
	function onChange(){
		success(cellEditedStatusDOM[0].value);
	}
	 //在该函数的调用列,cell标签发生change事件+失去焦点事件,触发onchange事件,修改cell的值。
	cellEditedStatusDOM[0].addEventListener("change", onChange);
	cellEditedStatusDOM[0].addEventListener("blur", onChange);
	//当cell的status列的值为“1”时,返回一个 input标签,否则返回false
	if(cell.getData().status == "1") {
		cellEditedStatusDOM.css({"padding"   : "4px",
			                     "width"     : "100%",
			                     "boxSizing" : "border-box"}) 
		return cellEditedStatusDOM[0];
    } else {
    	return false;
    }
}

editor是下拉框的实现方式(tabulator)

//接口在列的定义时,editor:select
//具体控制类:tabulator-edit-select-list
<div class="tabulator-edit-select-list" style="left: 1053px; top: 534.79px; min-width: 216px;">
	<div tabindex="0" class="tabulator-edit-select-list-item active">Disable</div>
	<div tabindex="0" class="tabulator-edit-select-list-item">Enable</div></div>

3-列相关属性

列不可排序

    headerSort:false,//表头不带排序图标,不可排序
    //使用Tabulator自定义的下拉框,值为0,显示Enable
    editor : "select", editorParams : { values:{"0":"Disable", "1":"Enable"} } }]
     {formatter:"rowSelection", titleFormatter:"rowSelection", hozAlign:"center", headerSort:false, cellClick:function(e, cell){
        cell.getRow().toggleSelect();
      }},

4-创建Tabulator

创建一个Tabulator的例子

 new Tabulator('#shiftTable',{
               //titleFormatter : "rowSelection",
        layout           : "fitDataStretch",
        //layout           : "fitColumns",
        //responsiveLayout : "hide",
        //可选行数1
        selectable : 1,
        //如果将data的设置直接写systemConfigInfo.mShifts,数据会浅拷贝
		data       : 你的数据,//注意数据对象的每一个的属性和columns中的每列的field对应。
		columns    : [{title : [[#{systemconfiguration.shift.id}]],       field:"shiftID",   headerSort:false, width:"15%"},
					        {title : [[#{systemconfiguration.shift.name}]],     field:"shiftName", headerSort:false, width:"35%", editor : isShiftCellEditable},
					        {title : [[#{systemconfiguration.shift.begintime}]],field:"beginTime", headerSort:false, width:"15%",
					        	       editor : isShiftCellEditable, validator:"regex:^([0-9]){2}:([0-9]){2}$"},
					        {title : [[#{systemconfiguration.shift.endtime}]],  field:"endTime",   headerSort:false, width:"15%",
					        	       editor : isShiftCellEditable, validator:"regex:^([0-9]){2}:([0-9]){2}$"},
					        {title : [[#{systemconfiguration.shift.status}]],   field:"status",    headerSort:false, width:"20%", 
					        	     editor : "select", editorParams : { values:{"0":"Disable", "1":"Enable"} },
					        	     formatter : function(cell, formatterParams){ return (cell.getValue() == "1" ? "Enable" :"Disable");}
					       }]
		
	});

5-列全选+取消全选

 //referDBTable是自己定义的Tabulator对象(使用方法如下两行代码)
 //全选
referDBTable.selectRow(referDBTable.rowManager.rows);
//取消全选
referDBTable.deselectRow(referDBTable.rowManager.rows); 
 //Tabulator.js源码提供的定义
  RowComponent.prototype.select = function () {

		this._row.table.modules.selectRow.selectRows(this._row);
	};

	RowComponent.prototype.deselect = function () {

		this._row.table.modules.selectRow.deselectRows(this._row);
	};
	//对用户提供的Tabulator对象的接口,在Filtering模块
	Tabulator.prototype.selectRow = function (rows) {

		if (this.modExists("selectRow", true)) {

			if (rows === true) {

				console.warn("passing a boolean to the selectRowselectRow function is deprecated, you should now pass the string 'active'");

				rows = "active";
			}

			this.modules.selectRow.selectRows(rows);
		}
	};

	Tabulator.prototype.deselectRow = function (rows) {

		if (this.modExists("selectRow", true)) {

			this.modules.selectRow.deselectRows(rows);
		}
	};

6-解析Tabulator

 //Tabulator源码相关options的位置
 Tabulator.prototype.defaultOptions

7-绑定resize事件削除

解决场景:画面有导航栏,例如有A,B,点击A,到A画面,点击B,到B画面,A,B画面都有Tabulator表格,当在A画面发生resize事件,A画面的Tabulator对象正常,B画面的Tabulator对象数据消失,是因为此时所有的Tabulator都进行了Redraw,B画面的Tabulator的Element(例如DIV对象)不显示。
解决方法;
//shiftTable是你创建Tabulator对象的名字
shiftTable.modules.resizeTable.clearBindings();

 //源码解析----为false时,绑定窗口的resize事件,进行redraw
if( typeof ResizeObserver !== "undefined" && table.rowManager.getRenderMode() === "virtual") {
      .........
} else {
    this.binding = function () {
		if (!table.browserMobile || table.browserMobile && !table.modules.edit.currentCell) {
					table.redraw();
				}
		};
		window.addEventListener("resize", this.binding);
}
 //table.browserMobile默认为false	
		ResizeTable.prototype.clearBindings = function (row) {
		if (this.binding) {
			window.removeEventListener("resize", this.binding);
		}

		if (this.observer) {
			this.observer.unobserve(this.table.element);
		}

		if (this.containerObserver) {
			this.containerObserver.unobserve(this.table.element.parentNode);
		}
	};
### 集成和使用 Tabulator 表格组件到 Vue3 Tabulator 是一个功能强大的 JavaScript 数据表格库,可以轻松地将其集成到基于 Vue 的项目中。以下是关于如何在 Vue3 中集成并使用 Tabulator 组件的具体方法。 #### 安装依赖项 为了在 Vue3 项目中使用 Tabulator,首先需要安装 `tabulator-tables` 和其对应的样式文件。可以通过 npm 或 yarn 来完成此操作: ```bash npm install tabulator-tables --save ``` 或者, ```bash yarn add tabulator-tables ``` #### 创建自定义封装的 Tabulator 组件 由于 Tabulator 并未原生支持 Vue.js,因此需要手动创建一个 Vue3 自定义组件来管理 Tabulator 实例及其生命周期。 ##### 步骤说明 1. **引入必要的模块** 在组件内部导入 Tabulator 库以及所需的 CSS 文件。 2. **初始化 Tabulator 实例** 使用 `mounted()` 生命周期钩子函数,在 DOM 节点挂载完成后实例化 Tabulator 对象。 3. **销毁 Tabulator 实例** 当组件卸载时调用 `destroy()` 方法释放资源,防止内存泄漏。 以下是一个完整的示例代码片段: ```vue <template> <div ref="tableRef"></div> </template> <script> import { onMounted, onUnmounted, ref } from 'vue'; import Tabulator from 'tabulator-tables'; // 导入 Tabulator JS import 'tabulator-tables/dist/css/tabulator.min.css'; // 导入 Tabulator 样式 export default { name: 'TabulatorComponent', setup() { const tableRef = ref(null); let tableInstance; const initializeTable = () => { if (tableRef.value) { tableInstance = new Tabulator(tableRef.value, { data: [], // 初始数据为空数组 layout: 'fitColumns', // 布局模式 columns: [ { title: 'Name', field: 'name' }, { title: 'Age', field: 'age' }, { title: 'Location', field: 'location' } ], }); } }; onMounted(() => { initializeTable(); }); onUnmounted(() => { if (tableInstance) { tableInstance.destroy(); // 销毁表实例 } }); return { tableRef, }; }, }; </script> <style scoped> /* 可选:添加额外的样式 */ </style> ``` 上述代码展示了如何通过组合 API (`setup`) 将 Tabulator 初始化为 Vue3 组件的一部分,并确保它能够正确响应组件的生命周期事件[^2]。 #### 动态更新数据 如果希望动态加载或更改表格中的数据,则可以在父级组件中传递 props 至该自定义组件,并监听这些 prop 的变化以重新设置数据源。 例如,修改上面的例子使其接受外部传入的数据: ```javascript props: ['rowData'], // 接收来自父组件的数据 watch: { rowData(newData) { this.tableInstance.replaceData(newData); // 更新表格数据 } } ``` 这样就可以实现更灵活的功能扩展[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值