state save in not working column search

state save in not working column search

dcxfdcxf Posts: 5Questions: 1Answers: 0

$(document).ready(function () {

       $('#myProjectTable thead tr')
    .clone(true)
    .addClass('filters')
    .appendTo('#myProjectTable thead');
         var table =    $("#myProjectTable")

                .dataTable({
                    stateSave: true,

                        scrollX: true,



                    dom: 'Blfrtip',

                    columnDefs: [{ className: "dt-body-right", visible: false}],
                      lengthMenu: [
        [ 10, 25, 50, -1 ],
        [ '10 rows', '25 rows', '50 rows', 'Show all' ]
    ],

    buttons: [
        'copyHtml5',
        'excelHtml5',
        'csvHtml5',
        'pdfHtml5',
         {
            extend: 'print',
            exportOptions: {
                columns: ':visible'
            }
        },
        'colvis'

    ],
    orderCellsTop: true,
    fixedHeader: true,
    initComplete: function () {
        var api = this.api();

        // For each column
        api
            .columns()
            .eq(0)
            .each(function (colIdx) {
                // Set the header cell to contain the input element
                var cell = $('.filters th').eq(
                    $(api.column(colIdx).header()).index()
                );
                var title = $(cell).text();

                $(cell).html('<input type="text" placeholder="' + title + '" />');


                // On every keypress in this input
                $(
                    'input',
                    $('.filters th').eq($(api.column(colIdx).header()).index())
                )
                    .off('keyup change')
                    .on('change', function (e) {
                        // Get the search value
                        $(this).attr('title', $(this).val());
                        var regexr = '({search})'; //$(this).parents('th').find('select').val();

                        var cursorPosition = this.selectionStart;
                        // Search the column for that value
                        api
                            .column(colIdx)
                               .search(
                                this.value != ''
                                    ? regexr.replace('{search}', '(((' + this.value + ')))')
                                    : '',
                                this.value != '',
                                this.value == ''
                            )
                            .draw();
                    })
                    .on('keyup', function (e) {
                        e.stopPropagation();

                        $(this).trigger('change');
                        $(this)
                            .focus()[0]
                            .setSelectionRange(cursorPosition, cursorPosition);


                    });
            });
    },



                });

} );

Answers

  • dcxfdcxf Posts: 5Questions: 1Answers: 0

    state save in not working for column search

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

    Here's an example with that working, that should get you going: https://live.datatables.net/guqoqeno/1/edit

    Colin

  • dcxfdcxf Posts: 5Questions: 1Answers: 0

    this is also not working

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

    Works for me (Chrome/Ubuntu) - if I type "Ash" into the search filter for the name column, and then press "Run with JS" again, "Ash" is still present.

    In what way isn't it working for you?

    Colin

  • dcxfdcxf Posts: 5Questions: 1Answers: 0

    can you please do change in my above written code

  • dcxfdcxf Posts: 5Questions: 1Answers: 0

    i have two search panel global as well as column search
    it works in global search but not works in column search

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

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

Sign In or Register to comment.