Get object from initDataTables function

Get object from initDataTables function

MikeFRMikeFR Posts: 4Questions: 2Answers: 0

Hello,

I would like to know how to get the Object of Datatable after using the function initDataTables to apply the draw function for filter by date range.

I added the extension like this:

    $.fn.dataTable.ext.search.push(
        function (settings, data, dataIndex) {
            const dateTimeCreationString = data[1]
            const selectedDates = dateRangeInput._flatpickr.selectedDates

            if (selectedDates.length !== 2) return true

            if (dateTimeCreationString) {
                const dateCreationArray = dateTimeCreationString.split(' ')[0].split('/')
                const dateCreation = new Date(dateCreationArray[2], dateCreationArray[1] - 1, dateCreationArray[0])

            if (dateCreation >= selectedDates[0] && dateCreation <= selectedDates[1]) {
                return true
            }
        }

        return false
        }
    )

When change the input with the dates, I launch the function draw:

 $(function () {
    dateRangeInput.flatpickr({
        locale: French,
        allowInput: true,
        mode: 'range',
        dateFormat: 'd/m/Y',
        onChange: function (selectedDates, dateStr, instance) {
            // launch the function draw here
        }
    })
    document.querySelector('#date-range-list-clear').addEventListener('click', function () {
        dateRangeInput._flatpickr.clear()
    })
})

Because before I had all the code in JS for create the Datatable.
Now I am using a bundle of Symfony and my JS code is like this:

$('#list-table').initDataTables({{ datatable_settings(table) }}, {
        "searching": true,
    });

How can I get the object after use the function initDataTables to apply the draw function?
I tried it in many ways and nothing is working.

Thank you by advance for your help.

Answers

  • rf1234rf1234 Posts: 2,988Questions: 87Answers: 421

    How can I get the object after use the function initDataTables to apply the draw function?
    I tried it in many ways and nothing is working.

    Have you tried chatGPT with this question? I just did it and I found the response really good! Probably will have the solution for you as well. Just use bing and click "chat". In the latest Windows version there is a bing icon in the search box at the bottom of your screen.

  • kthorngrenkthorngren Posts: 21,324Questions: 26Answers: 4,949

    The initDataTables is a Symphony method to integrate with Datatables. Here are some docs. I looked through and don't see anything about how to access the API. I suspect you will need to use the standard Datatables way of accessing the API. See the API docd for details.

    Kevin

  • MikeFRMikeFR Posts: 4Questions: 2Answers: 0

    Thank you for your fast answers!
    I didn't try ChatGPT in fact.

    I can't use the version in JS of DataTable, I have to do it with the Symfony bundle.

    But I found my solution.
    Finally, I just filter my array in PHP inside my controller in function of the date I send through a GET request.

    Thank you again!

  • jocelynjuliajocelynjulia Posts: 1Questions: 0Answers: 0
    edited October 2023

    I tried ChatGPT and ChatGPT's data is almost accurate. However, your data must be before 2022, if it is after 2022 you must put the information in and it will learn. You will eventually use it using that latest data.

Sign In or Register to comment.