passing variables to datatables columns variable

passing variables to datatables columns variable

jslearnerjslearner Posts: 1Questions: 0Answers: 0
edited December 2016 in Free community support

I am new to jquery. I am trying to pass the column data/names as variable from an array. The column assignment is not working. The commented section works, but the variable assignment to columns field and Func1 assignments are not working. Can somebody help how do I correctly write the variable assignments? Following is the snippet:

```

<html>
<head>


</head>
<body>
<script>
var dataSet = [
["Tiger Nixon1", "10", "20", "51", "300"],
["Tiger Nixon2", "10", "20", "51", "300"],
["Tiger Nixon3", "10", "20", "51", "300"]
];
$(document).ready(function() {
var colname1= [
{ title: "Name" },
{ title: "column1" },
{ title: "column2" },
{ title: "column3" },
{ title: "column4" }];
//-----------not working--------------------
var i = 2;
function myFunc1(item, index, arr) {
arr[index].title = item;
value2.innerHTML = colname1[index].title;
}
//-----------not working--------------------
var t = $('#example').DataTable({
data: dataSet,
columns: colname1.forEach(myFunc1, i)
});
//-----------working--------------------
/* var t = $('#example').DataTable({
data: dataSet,
columns: [
{ title: "Name" },
{ title: "column1" },
{ title: "column2" },
{ title: "column3" },
{ title: "column4" }]
});*/
});
</script>
<div > <span> this is test text</span> </div>
<div class="content"> <span id="value1"> this is test2 section</span> </div>
<div class="content"> <span id="value2"> this is test3 section</span> </div>
<div>
<table width="300" class="display" id="example"></table>
</div>
</body>
</html>

```
Appreciate your help in advance.

This discussion has been closed.