1

I' trying to pass the value of a button in a datatables to a function. However I'm just getting the value of the first row. Hope you can help me. I just need to alert the parameter then I'll take care of the rest.

here's the code

memberlist.php

<?php
header('Content-Type: application/json; Charset="UTF-8"');
$columns = array( 
// datatable column index  => database column name
    0 => 'HoId',
    1 => 'Name',
    2 => 'ClusterName',
    3 => 'MemStatus',
    4 => 'EmailId',
    5 => 'RegDate'
);
include 'database.php';
$sql = "SELECT mi.HoId, CONCAT(mi.FirstName,' ',mi.LastName) as Name,mi.Cluster,mi.MemStatus,mi.EmailId,mi.MobileNo,mi.RegDate, ci.ClusterName, ci.CHname"; 
$sql2 = " from tblmemberinfo mi LEFT JOIN clusterinfo ci on ci.id = mi.cluster";
$sql3 = $sql.$sql2;

$res = mysqli_query($conn, $sql3) or die("Error: ".mysqli_error($conn));
$dataArray = array();
while( $row = mysqli_fetch_array($res) ) {


                 $HoId = $row['HoId'];
                 $Name = $row['Name'];
                 $ClusterName = $row['ClusterName'];
                 $MemStatus = $row['MemStatus'];
                 $EmailId = $row['EmailId'];
                 $RegDate = $row['RegDate'];
                 
                $dataArray [] =  array("HoId" => $HoId,
                                        "Name" => $Name,
                                        "ClusterName" => $ClusterName,
                                        "MemStatus" => $MemStatus,
                                        "EmailId" => $EmailId,
                                        "RegDate" => $RegDate);


}
$str = mb_convert_encoding($dataArray, "UTF-8");
echo json_encode($str);

?>

action.js = I just need to alert the parameter then I'll take care of the rest.

$(document).ready(function() {

$('#memberlist').DataTable( {
    "ajax": {
            "type" : "POST",
            "url" : "includes/memberlist.php",
            "dataSrc": function ( jsondata ) {
                return jsondata;
                
            }       
            },
            
    "columns": [
            { "data": "HoId" },
            { "data": "Name" },
            { "data": "ClusterName" },
            { "data": "MemStatus" },
            { "data": "EmailId" },
            { "data": "RegDate" },
            {   data: null, 
                render: function(data, type, row) {
                    return '<button id="memberlist_but" type="button" value="'+ data.HoId +'"class="btn btn-block btn-primary">View</button> '
                    

                }},

        ],

    } );
}
japs
  • 11
  • 1

1 Answers1

0

I think you don't know about the response data for datatable, very well. This is an example. (from https://datatables.net/examples/data_sources/server_side.html)

{
   "draw":3,
   "recordsTotal":57,
   "recordsFiltered":57,
   "data":[
      [
         "Gloria",
         "Little",
         "Systems Administrator",
         "New York",
         "10th Apr 09",
         "$237,500"
      ],
      [
         "Haley",
         "Kennedy",
         "Senior Marketing Designer",
         "London",
         "18th Dec 12",
         "$313,500"
      ],
      
      ...

      [
         "Jennifer",
         "Chang",
         "Regional Director",
         "Singapore",
         "14th Nov 10",
         "$357,650"
      ]
   ]
}

As you can see, after you receive jsondata in dataSrc callback, the array must be found at jsondata.data, and also jsondata.recordsTotal and jsondata.recordsFiltered have to be initialized from server.

Additionally, please try to analyze the response data from 'really-working' server with Chrome Dev tools.

enter image description here

coding monster
  • 384
  • 4
  • 18
  • got the parameters in my datatables.. I just want to alert it to a javascript function. In Dev tool I can see all parameters I need each row so no problem with that.. Problem is I cant pass it to js function or jus alert it and I'll do the rest.. Note: I know how to use dev tools. – japs Apr 22 '21 at 06:33
  • Sorry for that. But, you missed `jsondata.data`. Response data - `$dataArray` is only an array, that is why you got errors. Capsulate `$dataArray` to `{..., data:$dataArray, ...}`. – coding monster Apr 22 '21 at 07:10