How to validate if {data: 1} exists?

How to validate if {data: 1} exists?

cris19ncris19n Posts: 55Questions: 18Answers: 0

How can I validate if there is data in the json response?

example:

    'ajax': {
                'url':'serverside/show.materia.prima.php'
            },
            //"oSearch": {"sSearch": varsearch},

            "columns": [
                { data: "cod_piku" },
                { data: 1 }

            ],

I need to be able to ask if it exists, because in ajax,

I'm sending data: 1 if it meets a condition,

if not, then I only send cod_piku.

and it is necessary to validate if data: 1 comes because otherwise I have an error Cannot read property 'style' of undefined TypeError: Cannot read property 'style' of undefined

Answers

  • kthorngrenkthorngren Posts: 21,173Questions: 26Answers: 4,923

    Add defaultContent to the column and you shouldn't get errors, like this:

            "columns": [
                { data: "cod_piku" },
                { data: 1, defaultContent: '' }
     
            ],
    

    Kevin

  • cris19ncris19n Posts: 55Questions: 18Answers: 0

    It only works when there is data: "cod_piku" and data: 1, I don't know if I understand:

    Example I have two th, if the condition is met both th are drawn.

    but if it is not fulfilled, only one is drawn.

    and it is the same as I need to validate in js Datatables.

    if there is both data: cod_piku and data: 1
    this would be the result:

    "columns": [
    { data: "cod_piku" },
    { data: 1 }]
    if only data exists: cod_piku

    "columns": [
    { data: "cod_piku" }]

    this doesn't work for me, I keep getting the same error:
    { data: 30, "defaultContent": "" }

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    this doesn't work for me, I keep getting the same error:

    I think you're going to need to parse the returned data within ajax.dataSrc, and if that second element is missing, add it in. As DataTables is initialised with the two headers, it's going to expect to see two elements in that data set.

    Colin

This discussion has been closed.