我有使用制表符的可编辑表格。
一切正常,但问题是单击“保存按钮”时我无法获取所有行值
我正在尝试的是:
$(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');
});
});
<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>
运行时报错
TypeError: $.widget is not a function
当我尝试使用时:
var data = $("#example-table").tabulator("getData");
我收到这个错误:
TypeError: $(...).tabulator is not a function
使用时:
var data = $('#example-table').getData();
错误是:
TypeError: $(...).getData is not a function
当我尝试使用这种方法定义表格时:
$("#example-table").tabulator({ //table setup options });
它没有显示任何内容(空白)。
有人能告诉我这是怎么回事吗?或者给我引用如何获取所有行值?
您也可以在 jsfiddle 中尝试,检查 here
最佳答案
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();
原答案:
问题是因为您在函数内声明表变量,所以它的范围是有限的。
如果您在全局范围内声明它,然后在函数内分配它,那么它将在任何地方都可以访问。
//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();
关于javascript - 如何获取所有行值制表符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54286603/