HTML5 data-* attribute for ajax url and dataSrc

HTML5 data-* attribute for ajax url and dataSrc

tstartstar Posts: 36Questions: 0Answers: 0

Hello!

I cannot understand, how correctly define HTML5 data-ajax attribute for repeat this code:

ajax: {
    url: "/blablabla",
    dataSrc: ""
}

If I write data-ajax="/blablabla" - its work, but I need to define dataSrc also.

If I write data-ajax="{url: '/blablabla', dataSrc: ''}" - then I got Ajax error and very strange request from datatable: "GET /blablabla/%7B'url':%20'/blablabla',%20'dataSrc':%20''%7D?_=1438855845083"

Where am I making a mistake?

Thanking you in advance.

Replies

  • allanallan Posts: 63,262Questions: 1Answers: 10,423 Site admin

    No you aren't making any mistakes. This looks like a limitation in the current implementation for these data attributes. Thanks for bringing it to my attention. I can't promise to have it fixed soon (I've got a large backlog!) but I have added it to my list.

    Allan

  • tstartstar Posts: 36Questions: 0Answers: 0

    Excuse me, it's still my fault. Everything works perfectly, if pass the right quoted parameters. Example:

    data-ajax="{"url":"/blablabla","dataSrc":""}"
    
  • allanallan Posts: 63,262Questions: 1Answers: 10,423 Site admin

    You might also be able to do:

    data-ajax='{"url": ... }'
    

    since it is valid for HTML attributes to use ' as the quote.

    Interesting - I need to document that! Thanks for posting back.

    Allan

  • tstartstar Posts: 36Questions: 0Answers: 0

    The problem is that I also tried Your variant. I tried a lot of different options and only last worked. I understood when I wanted to add a parameter "columns".

    "columns": [
        { "data": "Name" },
        { "data": "Begin" }
    ]
    

    Here was the exact same problem. I began to try various options for recording the attribute. After I wrote it as:

    data-columns="[{"data":"Name"},{"data":"Begin"}]"
    

    everything worked perfectly.
    As a result, there is nothing wrong in this form of transmission parameters. In any case, for me it's not a problem. I use:

    data-columns="@JsonConvert.SerializeObject(new[] { new { data = "Name" }, new { data = "Begin" } })"
    

    in my ASP.NET MVC project and get the desired result automatically.

    Thanks for your work!

  • allanallan Posts: 63,262Questions: 1Answers: 10,423 Site admin

    Sounds good - thanks for the feedback!

    Allan

  • tstartstar Posts: 36Questions: 0Answers: 0

    I have a problem again. Now I can not define a parameter "render" for columns. This should be an external function. In the code, I can write:

    columns: [
        { "data": "Test", "render": Render.Test}
    ]
    

    Please note that the attribute "render" has no double quotes around "Render.Test".

    But the record in the attribute I can only record data-columns='[ {"data":"Test", "render": "Render.Test"} ]' which is equivalent to the code:

    columns: [
        { "data": "Test", "render": "Render.Test"}
    ]
    

    Note the double quotes around "Render.Test".

    Maybe there's another way to pass function through the HTML5 data-* attribute?

    Thank you for your help.

  • allanallan Posts: 63,262Questions: 1Answers: 10,423 Site admin

    The problem is that JSON has no way to represent a Javascript function - so this simply will not work I'm afraid. This is exactly the reason why DataTables can't be configured by Ajax, only load data - it can't contain functions. You could use an eval() but it opens all sorts of security issues. Sorry.

    Allan

  • tstartstar Posts: 36Questions: 0Answers: 0

    Also, we can not pass local variables via HTML5 data-* attributes. I understand that through the HTML5 data-* attributes and JSON we can send only text strings.

    Probably it is necessary to highlight local variables by using special characters like "render": "[Render.Test]" or "render": "$Render.Test" and add the code to handle such cases. Then, setting the DataTables it can be fully implemented through HTML5 data-* attributes without a Javascript code.

This discussion has been closed.