boolean data and TypeError: sData.match is not a function

boolean data and TypeError: sData.match is not a function

rldean1rldean1 Posts: 141Questions: 66Answers: 1

If I try to return data that contains 0/1 or true/false, I get the "TypeError: sData.match is not a function". I must intercept and render the content for it to display properly, as shown below.

Is this normal?

table = $('#example').DataTable({
    dom: "Bfrtip",
    data: aoo,
    columns: [
        { data: 'PosTitle', title: 'PosTitle' },
        { data: 'PosCode', title: 'PosCode' },
        {
            data: 'Publish',
            title: 'Publish',
            visible: true,
            /*
             *  if the Publish column contains boolean data "true/false" or "0\1", 
             *  I must use the Render function
             */
            render: function (data, type, row) {
                return (data == true) ? 'true' : 'false';
            }
        }
    ],
    select: true,
    buttons: [
        { extend: "create", editor: editor },
        { extend: "edit", editor: editor },
        { extend: "remove", editor: editor }
    ],
});

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    I'm not clear if the above code is working or not. Doesn't surprise me that you would need to render the text if the data contains true or false. If the data contains 0 or 1 it should show 0 or 1 without using render.

    Maybe you can put together an example of your question:
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

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

    As Kevin said, the DataTable will just display the data that's sent - if you send 0/1, it'll display 0/1.

    I suspect there's a problem with this line though:

      return (data == true) ? 'true' : 'false'
    

    Data isn't a boolean, it's a string, so you'll need something like the following if you want to consolidate the booleans 1/true and 0/false into a single value:

       return ["1", "true"].includes(data)? 'true', 'false';
    

    Cheers,

    Colin

  • rldean1rldean1 Posts: 141Questions: 66Answers: 1
    Answer ✓

    @kthorngren and @colin

    I tried as hard as I could and I could not replicate the issue. In the response string, whether we're dealing with "0", 0, false, or "false", DataTables renders correctly in the simulator.

    This is not a critical situation, just a strange occurrence. I suppose I'll stumble upon the answer eventually.

    Here's a Plunker example: https://plnkr.co/edit/GtgiDI?p=preview

    Thanks, close this out!

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    I had wondered if returning a boolean value from a rendering function might cause this issue, but DataTables will cast it to be a string: http://live.datatables.net/xuneviho/1/edit .

    If anyone else finds this thread with the same issue, if you could show us an example of the issue happening, that would be great. Until then, we'll consider it done :).

    Allan

  • rldean1rldean1 Posts: 141Questions: 66Answers: 1

    @allan : I have discovered what led me to this question. I don't have an answer yet, but I wanted to comment here for you and posterity.

    When I use SQL's for json path, some datatypes like smallint, bit, and decimal() are not surrounded by double-quotes. You end up with something like this: "key": 0 instead of this: "key": "0"

    Anyway, in some other tests, specifically with decimal and smallint, I discovered that as long as the value in the key: value structure has double-quotes, DataTables will not require you to render the data in any special way. Basically, it avoids the "TypeError: sData.match is not a function" error.

    Anyway, I posted a comment on Stackoverflow with a better explanation: https://stackoverflow.com/questions/50936023/enforce-quotes-around-all-values-in-json-output

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    What's odd there is that DataTables will specifically convert integer data to be searchable as a string. Here is a little example showing it working: http://live.datatables.net/ziwuyake/1/edit .

    Allan

This discussion has been closed.