Martin Bouhier
Martin Bouhier

Reputation: 230

Call js function and send some data when click an option from dropdown in Datatable row

I'm looking for call a js function after click on my dropdown list. The code works, when I click it, it call myFunction. My problem is I need to add the datatable row into myFunction as a parameter.

My error: ReferenceError: Can't find variable: meta

I'm noob with javascript and html languages.

<script>
function myFunction(selectObject, row) {
    var value = selectObject.value; 
    alert("Actualización Status: " + value + row);
}
</script>

<script>
    var json={{ posts_list | safe }};
    $(document).ready(function() {
        var table = $('#posts_table').DataTable( {
            dom: 'Bfrtip',
            buttons: [
                'copy', 'csv', 'excel', 'print'
            ],
            "scrollX": true,
            data: json.data,
            columns:[
                {defaultContent: ""},
                {data: "_id"},
                {
                    "data": "status",
                    "render": function(data, type, row, meta){
                        if (data == 'no_denunciado') {
                            data = '<select id="status" onchange="myFunction(this, meta.row)">' +
                            '<option value="no_denunciado">' + data + '</option>' +
                            '<option value="nuevo">Nuevo</option>' +
                            '<option value="en_gestion">En Gestion</option>' +
                            '<option value="baja_edenor">Baja</option>' +
                            '</select>';
                        }

Upvotes: 0

Views: 169

Answers (1)

user4308987
user4308987

Reputation:

meta.row is being used out of scope of the function you need to put in the data not the variable meta.data as that will fall out of scope like so.

change

data = '<select id="status" onchange="myFunction(this, meta.row)>'

to

data = '<select id="status" onchange="myFunction(this, \''+meta.row+'\')">'

EDIT from the chat I see that you want to string an object I wrote a quick function

function ObjectToString(myObject){
var keys = Object.keys(myObject);
var returnedString="";
keys.forEach(key=>{returnedString+="["+key+":"+myObject[key]+"]";});
return returnedString;
}

Upvotes: 1

Related Questions