i'm trying to get my head around DataTables, but finding it difficult. I have a form where the user can paste 1+ rows of data with 1+ columns of information.
This could look like the following:
a b c d e f g h i j
443 39 70 10 38 11 161 15 477 318
99 452 229 11 63 23 38 104 190 100
After i have processed and validated it, it is available as the following (PHP var_dump() ):
array (size=2)
1 =>
array (size=10)
0 => string '443' (length=3)
1 => string '39' (length=2)
2 => string '70' (length=2)
3 => string '10' (length=2)
4 => string '38' (length=2)
5 => string '11' (length=2)
6 => string '161' (length=3)
7 => string '15' (length=2)
8 => string '477' (length=3)
9 => string '318' (length=3)
2 =>
array (size=10)
0 => string '99' (length=2)
1 => string '4s52' (length=4)
2 => string '22s9' (length=4)
3 => string '11' (length=2)
4 => string '63' (length=2)
5 => string '23' (length=2)
6 => string '38' (length=2)
7 => string '104' (length=3)
8 => string '190' (length=3)
9 => string '100' (length=3)
or
array (size=2)
1 =>
array (size=10)
'a' => string '443' (length=3)
'b' => string '39' (length=2)
'c' => string '70' (length=2)
'd' => string '10' (length=2)
'e' => string '38' (length=2)
'f' => string '11' (length=2)
'g' => string '161' (length=3)
'h' => string '15' (length=2)
'i' => string '477' (length=3)
'j' => string '318' (length=3)
2 =>
array (size=10)
'a' => string '99' (length=2)
'b' => string '4s52' (length=4)
'c' => string '22s9' (length=4)
'd' => string '11' (length=2)
'e' => string '63' (length=2)
'f' => string '23' (length=2)
'g' => string '38' (length=2)
'h' => string '104' (length=3)
'i' => string '190' (length=3)
'j' => string '100' (length=3)
I say "or" because i can output either way - if the json is better with the column names, i'll do it the second way.
my HTML code (using Twig) is:
<table class="table" id="Panda1"></table>
<script type="text/javascript">
$('#{{ Panda1 }}').dataTable( {
"data": [{{ contentsArray | json_encode() | raw }}],
"columns": [
{% for column in ColumnArray %}
{ "title": "{{ column }}" },
{% endfor %}
]
} );
</script>
My Output rendered is:
<script type="text/javascript">
$('#Panda1').dataTable( {
"data": [{"1":{"a":"443","b":"39","c":"70","d":"10","e":"38","f":"11","g":"161","h":"15","i":"477","j":"318"},"2":{"a":"99","b":"4s52","c":"22s9","d":"11","e":"63","f":"23","g":"38","h":"104","i":"190","j":"100"}}],
"columns": [
{ "title": "a" },
{ "title": "b" },
{ "title": "c" },
{ "title": "d" },
{ "title": "e" },
{ "title": "f" },
{ "title": "g" },
{ "title": "h" },
{ "title": "i" },
{ "title": "j" },
]
} );
</script>
My questions are:
- how do i correctly handle a PHP array?
- Do i need to use mData ?
- Do i need to use columnDefs ?
- In columns, do i need "title" and "data" ?