A dot in data name fires the datatables.net/manual/tech-notes/4 error.

A dot in data name fires the datatables.net/manual/tech-notes/4 error.

cFreedcFreed Posts: 21Questions: 4Answers: 0

(using DataTables cdn.datatables.net/1.10.13)

The above mentioned error is fired when populating a table if a row contains a property name including a dot ".", for instance something like:

someRow = {
    id: 123,
    name: 'my name',
    abbr.term: 'the content'
};

At a first glance It made me suspect that, somewhere during the process, the property name was sort of used directly, so the dot was syntactically considered as a separator and the job fails.
But it appears (though obviously I can't be sure for all) that it happens only with this particular character.
Notably all works fine with abbr term, abbr-term, abbr+term, abbr & term, and so on.
So it remains a mystery for me.

Having debugged I can certify it happens precisely when executing DataTables.row.add(someRow);.

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    Dot notation is commonly used in Javascript to denote properties of objects.
    It is unwise to use it in any other context.

  • sliekenssliekens Posts: 97Questions: 17Answers: 2

    How did you configure the columns.data?

    My guess is that you did something like this:

    columns: [
      { data: 'id' },
      { data: 'name' },
      { data: 'abbr.term' }
    ]
    

    That configuration works only when abbr contains a nested object.

    someRow = {
      id: 123,
      name: 'my name',
      abbr: {
        term: 'the content'
      }
    };
    

    If you cannot change the format of the data then you must escape the dot with \\.

    columns: [
      { data: 'id' },
      { data: 'name' },
      { data: 'abbr\\.term' }
    ]
    
  • cFreedcFreed Posts: 21Questions: 4Answers: 0

    Reading the responses, I realize I didn't explain the issue correctly.

    My main error is the way I exposed the involved object.
    It's a JSON object (a SQL query result coming from server) and its real representation should be:

    someRow = {
        "id": "123",
        "name": "my name",
        "abbr.term": "the content"
    }
    

    (the erroneous representation in my OP comes from I stupidly copied the simplified one used by Firefox when it exposes an object content)

    The reason why a property name can include any special character comes from the fact that the row is a dynamic SQL query result, so the actual column names may be absolutely anything.
    For instance in the exposed case the query might be something like:
    SELECT id, name, some_pretty_long_column_identifer AS "abbr.term" FROM ..."

    Now let's go back to the issue when populating DataTables row.

    @sliekens is right when he guesses how I'm first populating columns, with the slight difference that it happens dynamically, depending on the returned query result content (but the actual columns content is exactly what he exposed).

    Then each property name is used as a simple string (where a special character should have no special sense): so I don't understand why the dot should be escaped.
    May be naive but I'd expect that, during the DataTables.row.add(someRow) process, things happen schematically like this:

    columns.forEach(function(column) {
      cellValue = someRow[column[data]] || '';
    });
    

    This way all should work fine regardless which special characters feature in column[data].
    Conversely if things happen differently (but I struggle to imagine how?) I'd expect that other syntactically significant characters for Javascript fire the error too.

  • sliekenssliekens Posts: 97Questions: 17Answers: 2
    edited August 2017

    The reason why the dot is significant is because DataTables tries to expand dot-notation in the way that JavaScript does.

    It does this by splitting on unescaped dots.
    https://github.com/DataTables/DataTables/blob/7494262db18b1cbc69dcc1d64a5a8d0cd757d58c/media/js/jquery.dataTables.js#L2623-L2690

    Apparently regex is used to ignore escaped dots.
    https://github.com/DataTables/DataTables/blob/7494262db18b1cbc69dcc1d64a5a8d0cd757d58c/media/js/jquery.dataTables.js#L2568-L2578

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin
    Answer ✓

    Yes exactly. The dot-notation is very useful for nested objects in JSON. Hence the need to escape the dot as @sliekens says.

    Allan

  • cFreedcFreed Posts: 21Questions: 4Answers: 0

    Aaah! So it's by design.
    So sure there is no more mystery.

    But then I'd like to understand why it's done so, and for which benefit.
    Can you point me to part of the documentation where I could learn about this capability?

    Thanks in advance.

  • cFreedcFreed Posts: 21Questions: 4Answers: 0

    Sorry, I lately discovered @allan 's comment, after sending my last one.
    With the link it contained, I have a response to my need of documentation.
    So don't care.
    Thanks to all of you.

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin

    The documentation at columns.data that @sliekens linked to also contains information about this.

    Good to hear it all makes sense now :)

    Allan

  • cFreedcFreed Posts: 21Questions: 4Answers: 0

    Uh! I didn't even noticed this other link from @sliekens.

    And from this one I learn yet more about not only dot notation but also array and function notations.
    Really great!

    Thanks a lot for the info... and for this awesome tool.

This discussion has been closed.