我有使用制表器的可编辑表。
一切正常,但问题是,当单击“保存按钮”时,无法获得所有行值
我想要的是:
$(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不是一个函数
当我尝试使用:
var data = $("#example-table").tabulator("getData");我知道这个错误:
TypeError:$(.).tabulator不是函数
使用时:
var data = $('#example-table').getData();错误是:
TypeError:$(.).getData不是函数
当我尝试使用此方法定义表时:
$("#example-table").tabulator({ //table setup options });它没有显示任何东西(空白)。
有人能告诉我怎么回事吗?或者给我参考如何得到所有行的值?
你也可以试试小提琴,检查一下这里
发布于 2019-01-27 15:44:29
版本更新4.5+:
在version 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();https://stackoverflow.com/questions/54286603
复制相似问题