Individual column search with select input: fetch all column aggregated data from database

Individual column search with select input: fetch all column aggregated data from database

alzamboalzambo Posts: 38Questions: 17Answers: 1

Hi,
following this https://datatables.net/examples/api/multi_filter_select.html example with a server-side table, and having this code:

            this.api().columns('.selectSearch').every( function () {
                var column = this;
                var select = $('<select><option value=""></option></select>')
                        .appendTo( $(column.footer()).empty() )
                        .on( 'change', function () {
                            var val = $.fn.dataTable.util.escapeRegex(
                                    $(this).val()
                            );

                            column
                                    .search( val ? '^'+val+'$' : '', true, false )
                                    .draw();
                        } );

                column.data().unique().sort().each( function ( d, j ) {
                    select.append( '<option value="'+d+'">'+d+'</option>' )
                } );
            } );

The select field is populated with the paginated records, but i would like to have every record from database and not just the ones I'm seeing, so I think I need to perform another query to aggregate data.

Would it be possible?
Thank you
Alex

This question has accepted answers - jump to:

Answers

  • alzamboalzambo Posts: 38Questions: 17Answers: 1

    ...up...

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    The problem is that you are using a client-side API to get the options available (column().data()). But since you are using server-side processing, only the data for the current display is available at the client-side.

    You'd need to get the full list of options from the server in order to populate the select options. One method for doing that would be to add those options to your JSON data for the initial data load (draw=1) and use that information in initComplete to create the select lists.

    Allan

  • alzamboalzambo Posts: 38Questions: 17Answers: 1

    Thank you Allan for pointing me to the right direction... but should final JSON data be structured?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    I don't really understand what you mean I'm afraid. JSON is structured data by definition.

    Allan

  • alzamboalzambo Posts: 38Questions: 17Answers: 1
    Answer ✓

    Sorry, my question was unclear:
    i'm using, and still learning, yajra/laravel-datatables and I din't know how to send (append) additional data (in this case the <option> list) but, after some search, I got it and I can access that additional data in initComplete.

    Thank you for your help!!

    Alex

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Good to hear you found the solution - thanks for posting back.

    Allan

This discussion has been closed.