如何获取所有行值制表符?

dia*_*nto 8 html javascript jquery tabulator

我有使用制表符的可编辑表格。

一切正常,但问题是单击“保存按钮”时我无法获取所有行值

我正在尝试的是:

$(document).ready(function() {
var tabledata = [
 	{id:1, name:"Oli Bob", age:"12", col:"red", dob:""},
 	{id:2, name:"Mary May", age:"1", col:"blue", dob:"14/05/1982"},
 	{id:3, name:"Christine Lobowski", age:"42", col:"green", dob:"22/05/1982"},
 	{id:4, name:"Brendon Philips", age:"125", col:"orange", dob:"01/08/1980"},
 	{id:5, name:"Margret Marmajuke", age:"16", col:"yellow", dob:"31/01/1999"},
 ];
 
var table = new Tabulator("#example-table", {
 	height:205,
 	data:tabledata,
 	layout:"fitColumns",
 	columns:[
	 	{title:"Name", field:"name", width:150},
	 	{title:"Age", field:"age", visible: false},
	 	{title:"Favourite Color", field:"col"},
	 	{title:"Date Of Birth", field:"dob", sorter:"date", align:"center"},
 	],
 	footerElement:"<button id='save' class='pull-left' title='Save'>Save</button> ",
});

	$(document).on("click", "#save", function(){
 		/* var data = $("#example-table").tabulator("getData"); */
 		var data = $('#example-table').getData();
   	    console.log('save clicked');
	});
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://unpkg.com/tabulator-tables@4.1.4/dist/css/tabulator.min.css" rel="stylesheet">
<script type="text/javascript" src="https://unpkg.com/tabulator-tables@4.1.4/dist/js/tabulator.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/tabulator/4.1.5/js/jquery_wrapper.js"></script>


<div id="example-table"></div>
Run Code Online (Sandbox Code Playgroud)

运行时显示错误

类型错误:$.widget 不是函数

当我尝试使用:

var data = $("#example-table").tabulator("getData");
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

类型错误:$(...).tabulator 不是函数

使用时:

var data = $('#example-table').getData();
Run Code Online (Sandbox Code Playgroud)

错误是:

类型错误:$(...).getData 不是函数

当我尝试使用此方法定义表时:

$("#example-table").tabulator({ //table setup options });
Run Code Online (Sandbox Code Playgroud)

它没有显示任何内容(空白)。

有人可以告诉我出了什么问题吗?或者给我参考如何获取所有行值?

你也可以在 jsfiddle 中尝试,在这里查看

Oli*_*erd 7

4.5+ 版更新:

在 4.5+ 版本中,有一个函数可让您在无法访问表变量时使用查询选择器查找表。 http://tabulator.info/docs/4.6/options#find-table

const table = Tabulator.prototype.findTable('#example-table')[0];
const data = table.getData();
Run Code Online (Sandbox Code Playgroud)

原答案:

问题是因为您在函数内声明 table 变量,所以它的范围是有限的。

如果您在全局范围内声明它,然后在函数内部分配它,那么它就可以在任何地方访问。

//global scope
var table

//assign inside table - notice no "var"
function generateGrid(){
    table = new Tabulator("#example-table", { ....
}

//you can then use it everywhere
var data = table.getData();
Run Code Online (Sandbox Code Playgroud)