proper way to pass variable to a js function in column

proper way to pass variable to a js function in column

hbanaharishbanaharis Posts: 32Questions: 14Answers: 3

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

Answers

  • hbanaharishbanaharis Posts: 32Questions: 14Answers: 3
    edited January 2021

    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;}}

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    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

  • hbanaharishbanaharis Posts: 32Questions: 14Answers: 3

    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()

  • hbanaharishbanaharis Posts: 32Questions: 14Answers: 3

    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

  • kthorngrenkthorngren Posts: 20,139Questions: 26Answers: 4,734

    If you use data: null the data parameter will contain the data for the full row. The row parameter is used to access the data for the full row. Although the data for data: null is the same as the row parameter I would suggest using row 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 of ajax and provide Javascript sourced data like this example.

    Kevin

  • hbanaharishbanaharis Posts: 32Questions: 14Answers: 3
    edited January 2021

    was able to resolve by checking if the variable was null, i.e.

    { data: null, render: function (data, type, row){ 
                var = data.myvar;  
                if (var!== null){
                    var = var.replace(/,/g, ' ')
                } else {
                    var = "";
                };
                return var;
            }},
    
  • hbanaharishbanaharis Posts: 32Questions: 14Answers: 3

    the next issue is that inline editing no longer works and I get this error:

    Unable to automatically determine field from source. Please specify the field name. For more information, please refer to https://datatables.net/tn/11

  • hbanaharishbanaharis Posts: 32Questions: 14Answers: 3
    Answer ✓

    resolved by following instructions from https://datatables.net/tn/11 and explicitly defining field name, i.e.

    { data: null, editField: "myvar", render: function (data, type, row){
                var = data.myvar; 
                if (var!== null){
                    var = var.replace(/,/g, ' ')
                } else {
                    var = "";
                };
                return var;
            }},
    
This discussion has been closed.