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

时间:2019-01-21 09:13:37

标签: javascript jquery html 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');
	});
});
<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 });

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

有人可以告诉我怎么了吗?还是给我参考如何获取所有行值?

您也可以在jsfiddle中尝试,检查here

3 个答案:

答案 0 :(得分:1)

而不是var data = $('#example-table')。getData();

使用,可变数据= table.getData();

您正在做的是在jquery函数上链接getData(),但必须在Tabulator表实例(即上面创建的表var)上实现getData()。

希望这会有所帮助。 谢谢!

答案 1 :(得分:1)

问题在于,因为您在函数中声明了表变量,所以其范围受到限制。

如果您在全局范围内声明它,然后在函数内部分配它,那么它将随处可见。

//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();

答案 2 :(得分:0)

基于我在函数中声明该表的最后一条评论。

这是我单击“保存”按钮时获取特定值的方法

$(document).ready(function() {

function generateGrid(){
  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", editor:"input", formatter:
       		function (row, formatterParams){
            let rowId = row.getData().id;
            let color = row.getData().col;
            
            return '<input type="text" class="txtAU" name="txtAU" id="txtAU'+rowId+'" value="' + color +'"/>';
            }
       },
       {title:"Date Of Birth", field:"dob", sorter:"date", align:"center"},
     ],
     footerElement:"<button id='save' class='pull-left' title='Save'>Save</button> ",
});
}
	
  $(document).on("click", "#view", function(){
  	generateGrid();
  	$('#example-table').show();
  });
  
	$(document).on("click", "#save", function(){
		
    var data_ = "";
    var i = 1;
    $("input[name='txtAU']").each(function(){
        data_=data_+$(this).val()+";";
        i++;
    });
    console.log(data_);
   	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>



<button id='view' class='pull-left' title='Save'>Show Table</button> 

<div id="example-table" style="display:none"></div>

如果您像往常一样获取所有值和声明的表,则可以使用

  

var data = table.getData();

就像高拉夫的答案。谢谢,希望对您有所帮助。