Initially filter the API and apply over all datatables filters and searches

Initially filter the API and apply over all datatables filters and searches

maysumaysu Posts: 5Questions: 1Answers: 0
edited July 2021 in Free community support

I have an API for returning request items for datatables with this response:

{
    "data": [
        {
            "id": 13,
            "request": 39,
            "item": 1,
            "quantity": 1
        },
        {
            "id": 14,
            "request": 39,
            "item": 2,
            "quantity": 1
        },
        {
            "id": 15,
            "request": 40,
            "item": 2,
            "quantity": 1
        },
        {
            "id": 16,
            "request": 40,
            "item": 1,
            "quantity": 1
        }
    ],
    "recordsFiltered": 4,
    "recordsTotal": 4,
    "draw": 1
}

I can use this API perfectly with serverside True. But I want to filter it initially with {request:40} (or some other request id) and continue to use all database search and filtering applied over it (like normally). Like this is an API that shows only request:40 records. The request-id will change page to page, so I want to show only the selected request's records in those pages.

I searched a lot but didn't find anything useful for this.

This question has an accepted answers - jump to answer

Answers

  • maysumaysu Posts: 5Questions: 1Answers: 0

    I did find some search_value fields and this is what I need. I tried to change the search_value of the hidden request column query from the backend and it filtered how I want. But I couldn't find the set its value from JS to send it to the backend.

  • kthorngrenkthorngren Posts: 21,337Questions: 26Answers: 4,953

    Sounds like you want to use ajax.data as a function to send the search values to the server. See this example.

    Kevin

  • maysumaysu Posts: 5Questions: 1Answers: 0

    https://stackoverflow.com/q/48316191/14506165

    this is the answer to my question, thank you, I hope it works for someone too

    https://datatables.net/reference/option/searchCols

  • maysumaysu Posts: 5Questions: 1Answers: 0

    @kthorngren, thank you for helping. I did try that but sending data to API not helped, there is no custom filtering.

    I am filtering the data with searchCols, but when I enter something to search input, the initial filter got disabled right over. How can I force the request.id to be 40 always?

    I tried this and works how I want, but I don't understand why it doesn't work for me:
    http://live.datatables.net/ssp/server_processing.phpwaselab/266/edit

    $('#DataTable').DataTable({
        "searchCols": [
            null,
            { "search": 40},
        ],
        "serverSide": true,
        "ajax": items_api_url,
        "columns": [
            {
                "data": "id"
            },
            {
                "data": "request.id"
            },
            {
                "data": "item.code"
            },
        ],
        "columnDefs": [
            {
                targets: [
                    1
                ],
                visible: false
            }
        ]
    });
    

    Thanks

  • kthorngrenkthorngren Posts: 21,337Questions: 26Answers: 4,953
    Answer ✓

    How can I force the request.id to be 40 always?

    You will need to look at your server side processing script to find out why the column search is not being used when you use the search input. It works in this SSP example:
    http://live.datatables.net/haritune/1/edit

    Are you using a Datatables supplied server side script or your own?

    Kevin

  • maysumaysu Posts: 5Questions: 1Answers: 0
    edited July 2021

    Hi Kevin. I was using a package named django-rest-framework-datatables in my Django project. It is still in development. I checked the codes and saw that the server-side filtering does not work as expected when comes to searchCols. I fixed the code on my side, everything works as expected right now. Thank you for your help.

    Mustafa

Sign In or Register to comment.