Searching option selected in a datatable

Searching option selected in a datatable

VicostoVicosto Posts: 4Questions: 0Answers: 0
edited May 2021 in Free community support

Hi,
I want to create a filter by clicking on a button. It must seek a specific value/text present in options of a select element.
I search a lot on this forum and on stackOverflow but I didn't find a solution that works for my problem.
Have you a link or a solution for my issue ?
If you want some code to illustrate my problem, say it to me.

Thank you in advance,
Vicosto

Replies

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    This example shows how to search with select values. You could modify that to trigger the search on a button's click, rather than the select's change,

    Colin

  • VicostoVicosto Posts: 4Questions: 0Answers: 0
    edited May 2021
    var dataTableD = $("#listeD").DataTable({
            "ajax":{
                url : "ajax.php",
                method : "POST",
                dataSrc : "data"
            },
                z"oLanguage": { "sUrl": "locales/dataTables.txt" },
                "order": [[ 1, "asc" ],[ 5, "asc" ]],
                "iDisplayLength": 50,
                "lengthMenu": [[10, 25, 50, 100, -1], [10, 25, 50, 100, "Tous"]],
            "columns": [
                        { title: "info1", data:'info1' },
                        { title: "info2", data:'info2' },
                        { title: "info3", data:'info3' },
                        { title: "info4", data:'info4' },
                        { title: "info5", data:'info5' },
                { title: "info6", data:'info6' },
                { title: "info7", data:'info7' },
                { title: "info8", data:'info8' },
                { title: "info9", data:'info9' },
                { data: "info"}
                    ],
                "columnDefs": [
                {
                    "targets": [6,7],
                    "orderable": false
                },
                {
                    "targets": [9],
                    "visible": false,
                    "searchable": false
                },
                {
                    "targets": [6],
                    "className": "importante"
                }
            ],
            "createdRow": function( row, data, dataIndex ) {
                switch(data["info"]){
                    case "classRed":
                        $(row).addClass('redRow');
                        break;
                    case "classGreen":
                        $(row).addClass('greenRow');
                        break;
                    default:
                        break;
                }
            }   
            });
    $("#buttonTreated").click(function() {
        dataTableD.columns( 6 ).search($('option[value="auc"]:not(:selected)'));
    });
    
    $("#button2").click(function() {
        dataTableD.columns( 6 ).search($('option[value="auc"]:selected'));
    });
    

    It's the initialization of my datatable.

  • VicostoVicosto Posts: 4Questions: 0Answers: 0
    edited May 2021

    Thanks Colin.
    I was not understandable in my old comment and the alternative you give me is such better that the one I had, however that does not solve my issue.
    So, there is a HTML select element in every datatable rows and I want to search if a specific option is selected in this HTML select element in datatable rows.

    Example of HTML select element in datatable rows :

    Vicosto

  • VicostoVicosto Posts: 4Questions: 0Answers: 0

    I tried that but it doesn't work :

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    To do that, you would need to create a custom search. Here is the documentation, and here are a few examples,

    Cheers,

    Colin

This discussion has been closed.