Any way to used a initialised DataTable instance ?

Any way to used a initialised DataTable instance ?

supercoolsupercool Posts: 7Questions: 4Answers: 0

Hi allan and friends

I been stuck with something peculiar and i hope i find a quick fix .

I am using DT 1.10 with jquery 1.11.0 and in my case i have a multiple listing screens . I created a re-usable things at one JS file so i can re-use it .

So , now i am trying to re-use it(js file have initialised DT) and i am facing issue . which is rightly so , dataTable is getting re-initialised second time . Well i should have gave it a tought at initial go but i can't turn thing back now .

I am hoping i can find a way so i can use the instance i created in centralised JS and i will use that instance in my respective JavaScript files for adding some required functionality like default order , tabletools etc .

Any fix available is much appreciated

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    You can just use the $().DataTable() constructor without passing any options and DataTables will return an API instance for the table in question.

    Example: http://live.datatables.net/xifaxaho/1/edit .

    Allan

  • supercoolsupercool Posts: 7Questions: 4Answers: 0
    edited August 2014

    I tried your code but i need bit differently like you are using " order: [[2, 'desc']] " up front but as mentioned i am trying to set this in my second js file after datatable initialised i.e

    possible steps match my code :
    1) var table = $('#example').DataTable(); (my main js file have something like this)
    2) $('#example').DataTable( {
    order: [[2, 'desc']]
    }); (in sub-js files i have to use instance and set my required options like mentioned )

    If i do in this way i am gettiing re-initailise alert box intrestingly output is fine .i need to get rid of alert box .

    Bit deeper info :- In my cshtml i have a script tag under document.ready i have TableManaged.init(); . TableManged is my function which return datatable (which is located in centralised re-usable js ) .

    And i am using the above metioned code in document.ready in my js file ..

    so if my info makes any sense please share your view mate .

    Cheers
    Thanks allan

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    i am trying to set this in my second js file after datatable initialised i.e

    Use the order() API method if there is an existing table. As the error you are getting says, you cannot reinitialise a DataTable with different options. Once you have initialised a DataTable you must use the API to manipulate it.

    Allan

This discussion has been closed.