proper way to pass variable to a js function in column
proper way to pass variable to a js function in column
hbanaharis
Posts: 32Questions: 14Answers: 3
in Editor
i note that whilst this works under columns, returning the value of myvar
{ data: null, render: function (data, type, row, meta){return data.myvar}}
which is equivalent to
{ data: 'myvar' }
this doesn't and returns a null
{ data: null, render: function (data, type, row, meta){return data.myvar.replace(/[,]+/g, " ")}}
what's the proper way to pass the myvar variable to a js function?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Further to the previous question:
assigning a value works
{ data: 'myvar', render: function (data, type, row){var mystring = data; return mystring;}}
as does checking the type using typeof
{ data: 'myvar', render: function (data, type, row){var mystring = data; return typeof mystring;}}
but this returns
null
{ data: 'myvar', render: function (data, type, row){var mystring = data; return mystring.length;}}
I think this will be specific to your data. This example here is using your format in the final case in your last post - and it's working as expected and not returning null.
Can you update that example to use your data please so we can see what's happening,
Colin
Many thanks Colin, its rather strange as the console.log(data) shows a string and this works
return data+data
but this show a null is not an object error
return data.toString()
i tested the pattern on another datatable implementation and it works. once I determine what causes this (its not the data itself) I'll report back
If you use
data: null
thedata
parameter will contain the data for the full row. Therow
parameter is used to access the data for the full row. Although thedata
fordata: null
is the same as therow
parameter I would suggest usingrow
just to be consistent with other columns where you define specific data properties, ie,data: 'myvar'
.If this doesn't help please provide or update Colin's test case with your specific data. You can use
data
instead ofajax
and provide Javascript sourced data like this example.Kevin
was able to resolve by checking if the variable was null, i.e.
the next issue is that inline editing no longer works and I get this error:
resolved by following instructions from https://datatables.net/tn/11 and explicitly defining field name, i.e.