Reputation: 2732
I have a table and I need to read a row and get all the ex. th. I wrote something like this, but it gives me the same array twice ['id','test']
.
How should I do this? As I have to process each th
in php class to build a table.
<script type="text/javascript">
$(document).ready(function(){
var table = $("table");
var headerCells = table.find("thead th");
var headers = [];
//get header cells
headerCells.each(function(i) {
i = $(this).text();
headers[headers.length] = i;
});
//convert array to json
var jsonArray = JSON.stringify(headers);
//prepare POST data
var dataToPost = { 'jsonArray':jsonArray };
console.log(headers);
$.ajax({ url: '/js/call.php',
data: {data: dataToPost},
type: 'get',
success: function(output) {
//output = eval( '(' + output + ')' );
alert (output);
}
});
}); //end jQ
</script>
Upvotes: 0
Views: 1915
Reputation: 9202
The problem is the ajax request
//prepare POST data
var dataToPost = { 'jsonArray':jsonArray };
console.log(headers);
$.ajax({ url: '/js/call.php',
data: {data: dataToPost},
type: 'get',
success: function(output) {
alert(output);
}
});
dataToPost is an object but string is expected, solution:
//prepare POST data
var dataToPost = { 'jsonArray':jsonArray };
console.log(headers);
$.ajax({ url: '/js/call.php',
data: dataToPost,
type: 'get',
success: function(output) {
alert(output);
}
});
See example
Upvotes: 0
Reputation: 76870
You are doing something strange when you create the array
//get header cells
headerCells.each(function(i) {
i = $(this).text();
headers[headers.length] = i;
});
i'd do:
//get header cells
headerCells.each(function(i) {
i = $(this).text();
headers.push(i);
});
Upvotes: 1
Reputation: 10305
your probably need to change this:
headerCells.each(function(i) {
i = $(this).text();
headers[headers.length] = i;
});
into
headerCells.each(function(idx, elm) {
var text = $(elm).text();
headers[idx] = text;
});
Upvotes: 1