javascript - Pass Parameter Value from Datatables Button to a Function

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> '
}},
],
} );
}
Answer
Solution:
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 receivejsondata
indataSrc
callback, the array must be found atjsondata.data
, and alsojsondata.recordsTotal
andjsondata.recordsFiltered
have to be initialized from server.
Additionally, please try to analyze the response data from 'really-working' server with Chrome Dev tools.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: constant expression contains invalid operations
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.