How to use table attributes for ajax url

How to use table attributes for ajax url

chalistchalist Posts: 3Questions: 1Answers: 0
edited November 2018 in Free community support

I have a table:

<table data-url="test.com"></table>

and in js:

$('table').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": {
        "url": "---------------------",
        "dataSrc": "data",
        "type": "POST"
    },
    "columns": [
        {"data": "id"},
        {"data": "title"},
        {"data": "title_en"},
        {"data": "status"},
        {"data": "city"},
        {"data": "html"}
    ]
});

I want to use each table's data-url for datatable ajax URL.
Can I do this?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947
  • chalistchalist Posts: 3Questions: 1Answers: 0

    @kthorngren Thanks dude, but I use table tag instead of ID.
    I want this: http://live.datatables.net/haretigo/2/

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    The Multiple Table example uses the same technique to initialize Datatables. It does state this though:

    The tables are independent for user control (i.e. user controlled paging on one table does not effect the others), but they do share the initialisation parameters

    Unfortunately the way you want to init the Datatables won't work for different URLs.

    Interesting idea though. Maybe someone can find a clever alternative.

    Kevin

  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin
    Answer ✓

    To some extent it is actually possible, but you should note that jQuery 3 changed things a little so it is no longer possible to do nested data with a double dash (e.g. data-ajax--url="..."). It would work if you are using jQuery 1 though.

    Allan

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    Forgot about setting the options with the data attributes. This looks like it works:
    http://live.datatables.net/dupuzubu/1/edit

    But I was not able to get data-ajax--url="..." working with jQuery 1.9.1. Looks like the jQuery data() method returns it like this: ajaxUrl.

    Learn something new each day :smile:

    Kevin

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

    Learn something new each day :smile:

    ....but forget something old...

  • kthorngrenkthorngren Posts: 21,303Questions: 26Answers: 4,947

    ....but forget something old...

    One item in multiple out :smile:

    Kevin

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

    ....tell me about it.... :'(

  • chalistchalist Posts: 3Questions: 1Answers: 0

    thanks, guys. That's worked.

This discussion has been closed.