I'm using datatables via ajax and display the table like this
var table = $('#data').DataTable( {
"ajax": "initTable.php",
"columns": [
{ "data": "orderid" },
{ "data": "first_name"},
{ "data": "last_name"},
{ "data": "unix" },
{ "data": "final_total" }
]
} );
I've tried
{ "data": "first_name" + "data": "last_name"},
But I get an error and table is not displayed. So how can I change the render to display first name next to last_name in the same cell not in the next cell
[UPDATE]
Tried
"ajax": "initTable.php",
"columns": [
{ "data": "orderid" },
{ "data": "first_name"},
{"data": "last_name"},
{ "data": "unix" },
{ "data": "final_total" }
],
"columnDefs": [
{
"render": function ( data, type, row ) {
return data + row[2];
},
"targets": 1
},
{ "visible": false, "targets": [ 2 ] }
]
(Note: I have to define column rows because I get many columns (about 20) and want to display just 4 or 5)
But I get the first name followed by 'undefined' something like "Andy undefined"
Use the code below:
{
"render": function ( data, type, row ){
return row["first_name"] + " " + row["last_name"];
},
"targets": 1
},
Also there is no need to include last_name column if you're hiding it.
Related
I am getting an array from an API that varies in number of levels but follows the same basic structure - here is a truncated sample as this particular repsonse is 25K lines:
{
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Ordinary Income/Expenses"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Rows": {},
"Summary": {
"ColData": [
{
"value": "Gross Profit"
},
{
"value": ""
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Income"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "40000 Sales Income",
"id": "31"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Rows": {
"Row": [
{
"ColData": [
{
"value": "2022-01-24"
},
{
"value": "Invoice",
"id": "148774"
},
{
"value": "208232"
},
{
"value": "Hyatt:#211102",
"id": "7568"
},
{
"value": "JN",
"id": "4100000000000368107"
},
{
"value": "CAPTIVE AIRE"
},
{
"value": "11000 Accounts Receivable",
"id": "80"
},
{
"value": "38748.00"
},
{
"value": "38748.00"
}
],
"type": "Data"
},
I need to traverse the json, and where there is data in both [Header][ColData][value] AND [Header][ColData][id] extract the value, id (in this snippet "value": "40000 Sales Income", "id": "31") and the data that immediately follows the "value"/"id" in [Rows][Row][Rows][Row][ColData] (in this snippet starting with "ColData": [{"value": "2022-01-24"...)
[Rows][Row][Rows][Row][ColData] will have one to a few hundred subarrays. I can extract the data from the subarrays once they are found - it's just managing the varying depths of the array that is warping my brain.
[Rows][Row][Rows][Summary] can be discarded as well.
I have tried multiple foreach loops - but by time I get 5 or 6 levels deep it gets very confusing. The number of Header sections varies depending on the report type. The [Rows][Row] nesting is multiple layers deep... I'm sure there has to be a better way than nesting foreach loops...
This is what I've come up with. Kindly modify it to meet your need.
$array = json_decode($json, true);
function traverse_some_array($array){
$result = [];
foreach($array['Rows']['Row'] ?? [] as $row){
if( !empty($row['Header']['ColData']) ){
foreach($row['Header']['ColData'] as $coldata){
if( isset($coldata['value'], $coldata['id']) ){
// there is data in both [Header][ColData][value] AND [Header][ColData][id]
// extract the value, id (in this snippet "value": "40000 Sales Income", "id": "31")
$extract_data = [
'value' => $coldata['value'],
'id' => $coldata['id']
];
// the data that immediately follows the "value"/"id" in [Rows][Row][Rows][Row][ColData]
$immediate_coldata = $row['Rows']['Row'] ?? [];
// you can do what ever you want with the results, eg return it or push it to a result array
$result[] = [
'extract_data' => $extract_data,
'immediate_coldata' => $immediate_coldata
];
}else{
// continue traversing the array
$result = array_merge($result, traverse_some_array($row));
}
}
}
}
return $result;
}
print_r(traverse_some_array($array));
After passing json_encode variable, not able to loading "sProcessing" text before loading data. I am trying to load more than 50000 records.
Javascript Code
<script>
var data = <?php echo json_encode($data); ?>;
$(document).ready(function() {
$('#datatable').dataTable({
"aaData": data,
"bProcessing": true,
"aoColumns": [
{ "data": "submissions_dt" },
{ "data": "pettycash_dt" },
{ "data": "type" },
{ "data": "description" },
{ "data": "voucher_no" },
{ "data": "amount", className: "right" }
],
"oLanguage": {
"sProcessing": "Fetching Data, Please wait..."
},
order: [[2, 'desc']],
"lengthMenu": [[50, 100, -1], [50, 100, "All"]],
"columnDefs": [
{ "orderable": false, "targets": [0,1,2,3,4,5] },
{"targets": [ 2 ], "visible": false }
]
});
});
</script>
I am using Data Table AJAX, get data from JSON, data table pagination, search bar not working. This is my code.Get data from (progress_play_transactions.php) file in JSON
progress_play_transactions.php (code)
$data[] = array(
"OriginalTransactionID" => "$OriginalTransactionID",
"PlayerID" => "$PlayerID",
"CasinoName" => "$CasinoName",
"TransactionDate" => "$TransactionDate",
"TransactionType" => "$TransactionType",
"Currency" => "$Currency",
"CurrencySymbol" => "$CurrencySymbol",
"TransactionOriginalAmount" => "$TransactionOriginalAmount",
"TransactionDetails" => "$TransactionDetails",
"Status" => "$Status"
);}
echo json_encode(array("result"=>$data,
"recordsTotal" => intval( $totalrecords ),
"recordsFiltered" => intval($totalrecords)
)
);
progress_play.php (code)
$(document).ready(function () {
$('#examples').DataTable({
"bProcessing": true,
"serverSide": true,
"ajax": {
"url": "progress_play_transactions.php",
"type": "json",
"dataSrc": "result"
},
"columns": [
{ "data": "OriginalTransactionID" },
{ "data": "PlayerID" },
{ "data": "CasinoName" },
{ "data": "TransactionDate" },
{ "data": "TransactionType" },
{ "data": "Currency" },
{ "data": "CurrencySymbol" },
{ "data": "TransactionOriginalAmount" },
{ "data": "TransactionDetails" },
{ "data": "Status" }
],
});
});
I'm having some trouble using the MongoDB aggregation framework to count event types in my database. How do I calculate the sum of the value.count field for each unique 3rd index of the _id.val field?
The basic structure of my data looks like:
{ _id: { evt: "click", val: [ "default", "125", "311", "1" ] }, value: { count: 1 } }
{ _id: { evt: "click", val: [ "default", "154", "321", "2" ] }, value: { count: 2 } }
{ _id: { evt: "click", val: [ "default", "192", "263", "1" ] }, value: { count: 4 } }
The values in the val field denote ["type","x","y","time"], respectively.
I'm trying to extract the 3rd index, or time value of the _id.val key. The output I'm looking to achieve:
1: 5
2: 2
I've been trying to do it via this PHP:
$ops2 = array(
array(
'$match' => $q2
),
array(
'$group' => array(
'_id' => array(
'evt' => '$_id.evt',
'time' => '$_id.val.3'
),
'count' => array('$sum' => '$value.count' )
)
)
);
But it doesn't appear to like the 3 index in the group array
The data you are working with looks like it has come as the output of a mapReduce operation already, since it has that specific "_id" and "value" structure that mapReduce prodcues. As such you may be better off going back to the logic of how that process is implemented and follow the same to just extract and total what you want, or at least change it's output form to this:
{
_id: {
evt: "click",
val: { "type": "default", "x": "125", "y": "311", "time": "1" }
},
value: { count: 1 }
},
{
_id: {
evt: "click",
val: { "type": "default", "x": "154", "y": "321", "time": "2" }
},
value: { count: 2 }
},
{
_id: {
evt: "click",
val: { "type": "default", "x": "192", "y": "263", "time": "1" }
},
value: { count: 4 }
}
As the problem is that the aggregation framework "presently" lacks the ability to address the "indexed" position of an array ( real "non-associative" array and not PHP array ) and would always return null when you try to do so.
Lacking the ability to go back to the original source or mapReduce operation, then you can write a mapReduce operation on this data to get the expected results ( shell representation since it's going to be a JavaScript anyway ):
db.collection.mapReduce(
function() {
emit({ evt: this._id.evt, time: this._id.val[3] }, this.value.count)
},
function(key,values) {
return Array.sum(values)
},
{ out: { inline: 1 } }
)
Which returns typical mapReduce output like this:
{
"_id" : {
"evt" : "click",
"time" : "1"
},
"value" : 5
},
{
"_id" : {
"evt" : "click",
"time" : "2"
},
"value" : 2
}
If you were able to at least transform the current output collection to the form suggested at first above, then you would run with the aggregation framework like this instead ( again common representaion ):
{ "$group": {
"_id": {
"evt": "$_id.evt",
"time": "$_id.val.time"
},
"count": { "$sum": "$value.count" }
}}
Which of course would yield from the altered data:
{ "_id" : { "evt" : "click", "time" : "2" }, "count" : 2 }
{ "_id" : { "evt" : "click", "time" : "1" }, "count" : 5 }
In future releases of MongoDB, there will be a $slice operator which allows the array handling, so with your current structure you could do this instead:
{ "$group": {
"_id": {
"evt": "$_id.evt",
"time": { "$slice": [ "$_id.val", 3,1 ] }
},
"count": { "$sum": "$value.count" }
}}
Which allows picking of the "third" index element from the array, albeit that this will of course still return an "array" as the element like this:
{ "_id" : { "evt" : "click", "time" : [ "2" ] }, "count" : 2 }
{ "_id" : { "evt" : "click", "time" : [ "1" ] }, "count" : 5 }
So right now, if you can change your initial mapReduce output then do it. Either to the form as shown here or just work with modifications to the initial query to get the end result you want here. Modifying to the recommened form will at least allow the .aggregate() command to work as is shown in the second example here.
If not, then mapReduce is still the only way at present for writing, as shown in the "first" example.
At first, I think you may have something wrong in your understanding of Mongo...Because each document in mongo should have its unique _id, to identify itself from others. So I have add a _id to each object, and change your origin "_id" field to "data". Now the structure is:
/* 1 */
{
"_id" : "ubLrDptWvJE7LZqDF",
"data" : {
"evt" : "click",
"val" : [ "default", "125", "311", "1" ]
},
"value" : {
"count" : 1
}
}
/* 2 */
{
"_id" : "C2QCEhvCsp3xG6EKZ",
"data" : {
"evt" : "click",
"val" : [ "default", "154", "321", "2" ]
},
"value" : {
"count" : 2
}
}
/* 3 */
{
"_id" : "bT72z7gMKoyX5JfHL",
"data" : {
"evt" : "click",
"val" : [ "default", "192", "263", "1" ]
},
"value" : {
"count" : 4
}
}
I am not sure how to do this query in PHP, Because I only know a little PHP...... But I could give you an example of using aggregation in Javascript, its code and output are as follows:
Here are some useful link: using mongo in PHP
I wish it can help you solve your problem perfectly :-)
I've found this piece of code which looks to work for some people,
$dataArr['aaData'] = Array();
while($row = $res->fetch_assoc()){
$r = Array();
foreach($row as $key=>$value){
$r[] = "$key $value";
}
$dataArr['aaData'][] = $r;}
header('Content-Type: application/json');
echo json_encode($dataArr);
/* The output will be of the form,
{"aaData": [ [
[
"colname data"
...
],
] */
but I get an error " Table id =datos Invalid json response.
My Datatable columns do not have the same name as my db columns, and I'm not sure how to handle that.
Here is my js :
$(document).ready(function() {
function getCpAndVille(data, type, dataToSet) {
return data.cp + " " + data.ville;
}
$('#datos').dataTable({
"order": [[ 3, "desc" ]],
"bProcessing": true,
"sAjaxSource": 'ajx/datatable_process_search.php',
"aoColumnDefs": [
{ "sName": "Réf.", "aTargets": [ 0 ] },
{ "sName": "Poste", "aTargets": [ 1 ] },
{ "sName": "Type de contrat", "aTargets": [ 2 ] },
{ "sName": "Date de publication", "aTargets": [ 3 ]},
{ "sName": "Lieu", "aTargets": [ 4 ], "mData": getCpAndVille },
{ "sName": "Descriptif", "aTargets": [ 5 ] }
],
Can someone tell me the right php code I should use, please, because it's quite hard to find it, and I don't understand the example given in http://www.datatables.net/examples/data_sources/server_side.html
Thanks in advance.
Console Network --> {"aaData":[["job_id 9261","job_intitule Assistant Achats","job_contrat Int\u00e9rim","job_date_insertion 2015-02-20","cp 06110","job_ville 19"], and so on...
For those who are looking for a serverside script including JOIN table
https://www.daniweb.com/web-development/php/threads/467455/convert-datatables-to-server-side-processing