How to get all row values tabulator?

瘦欲@ 提交于 2019-12-23 18:54:33

问题


I have editable table using tabulator.

Everything is ok, but the problem is I can't get all row value when click "save button"

What I'm trying is:

$(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>

When running it shows error

TypeError: $.widget is not a function

When I try using:

var data = $("#example-table").tabulator("getData");

I get this error:

TypeError: $(...).tabulator is not a function

when using:

var data = $('#example-table').getData();

the error is:

TypeError: $(...).getData is not a function

When I try to define the table using this method:

$("#example-table").tabulator({ //table setup options });

it's not showing anything (blank).

Can someone tell me what's wrong? Or give me the reference how to get all row values?

You can try in jsfiddle too, check here


回答1:


The issue is because you are declaring the table variable inside a function, so its scope is limited.

If you declare it in the global scope, and then assign it inside the function it will then be accessible everywhere.

//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:


Instead of, var data = $('#example-table').getData();

Use, var data = table.getData();

what you are doing is chaining getData() on jquery function but you have to implement getData() on Tabulator table instance which is table var which you created above.

Hope this helps. Thanks!




回答3:


Based on my last comment who declared the table inside function.

This is my way to get specific value when click save button

$(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>

And if you looking get the all value and declared table like usually, you can use

var data = table.getData();

like Gaurav's answer. Thankyou, hope this help someone.



来源:https://stackoverflow.com/questions/54286603/how-to-get-all-row-values-tabulator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!