3

I'm trying hard to understand the DataTables documentation, and I'm looking for examples, but I just don't get how to handle null values.

In SQL, I send a JSON string back to my JS app. I discovered I had to use INCLUDE_NULL_VALUES -- so, I'm no longer missing anything in the JSON array/object.

Here's how I'm populating the table. My ary is an array of objects with data like this:

ary = [{"Effective Date":"2001-07-01","Schedule":"UR-FUPA","Description":"High Up Dude","Calculated":"No","Base Schedule":"","Factor":null}, {...}, {...}]

$("#tblSomeTable").DataTable({
    "data": ary,
    "columns": [
        {
            "data": "Effective Date"
        },
        {
            "data": "Schedule"
        },
        {
            "data": "Description"
        },
        {
            "data": "Calculated"
        },
        {
            "data": "Base Schedule"
        },
        {
            "data": "Factor"
        }
    ],
    "columnDefs": [{
        "targets": _all,
        "defaultContent": ""
    }],
    "paging": false,
    "ordering": true,
    "info": false,
    "searching": false,
});

So, how do I set the default content to an empty string if the cell has no data?

You can see above that I tried to use the "defaultContent" option to target all columns, but that is not working.

I always appreciate your help. I'm not asking you to do my work, I just need an example I can understand, and your guidance in the right direction.

0

2 Answers 2

12

You need to specify _all as string as target option takes integer and string values.

So just update columnDefs as`

"columnDefs": [{
    "targets": '_all',
    "defaultContent": ""
}],

Your Example forked jsfiddle working demo: https://jsfiddle.net/mmushtaq/5pgp2479/

Source : columnDefs.targets

Sign up to request clarification or add additional context in comments.

2 Comments

I'm marking this as the answer: (1) the quote missing around '_all was only part of my problem; (2) also, not shown, I was targeting the wrong table #id; (3) ultimately, I didn't need to specify columnDefs at all, so long as empty cells are included in the JSON object as either "" or null via for json path, INCLUDE_NULL_VALUES
it solved my problem, while I have several objects in my json data and sometimes one object is null, so the sub property name is missing
0

I don't believe there is current support for setting defaultContent in columnDefs. You would need to set it per column.

1 Comment

I tested and found that I can do it both per column, or through columnDefs. In fact, I didn't need to worry about it at all. I found that as long as the JSON object contained "null" or an empty empty string, it would render the table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.