Datatable Dropdown reload

Datatable Dropdown reload

nhakk7nhakk7 Posts: 3Questions: 2Answers: 0

Hello,
I have a DataTable Server-Side with a dropdown filters comming from Mysql database

My filters have dependencies, i have 4 filters, Brand, Model, Year, Doors.

Filters model year and doors are empty, only show options after select the brand.

The problem:
I select brand "AUDI", then in dropdown modelos i select "A4 Avant" and works fine. But if i want to change the brand, i select BMW, my datatable returns "Empty Results" and i have two options:

I need to select some model of BMW, and after select model work fine.
I need to select another brand, and work fine, example, i'm on Brand AUDI, model A4 AVANT, i select brand Mercedes, no results, i select brand BMW, full results from BMW.

I try some codes in this forum and on other foruns and can't put to work, i want, when i select "Brand" always reload datatable...

Live example:
https://new.dga.pt/produtos/datatables.php

The code: (My code have PHP, so you cant preview result, but can watch the HTML, the script for datatable is on javascript field.

https://live.datatables.net/carofiba/1/watch

Thank You!

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Answer ✓

    You are encountering a similar issue as we have in SearchPanes for the cascade.

    What you need to do is have your event listener for the Marca field reset the values for the other three inputs - then make the Ajax request. For example:

            $('#searchByMarca').change(function(){
                $('#searchByModelo').val('');
                $('#searchByPortas').val('');
                $('#searchByAno').val('');
                dataTable.draw();
            });
    

    Do the same for the other dropdowns, just removing itself, so finally you end up with the Ano search just calling draw as it currently is.

    Allan

  • rf1234rf1234 Posts: 2,809Questions: 85Answers: 406

    I try some codes in this forum and on other foruns and can't put to work, i want, when i select "Brand" always reload datatable...

    Just use a "change" event handler and do an ajax reload of the data table.
    https://datatables.net/reference/api/ajax.reload()

  • nhakk7nhakk7 Posts: 3Questions: 2Answers: 0

    Yes allan, that makes sense, i tried this code, but inside the script to call the the other options based on last choice. When you sent me that code, i just copy and put inside a new script on the end of page and worked with no problems.

    Solved.

    Newbie error... i'm a begginer in JS.
    Thank you Allan.

Sign In or Register to comment.