Can not filter numbers only when I have numbers and text in a column

Can not filter numbers only when I have numbers and text in a column

samsamtestsamsamtest Posts: 52Questions: 15Answers: 1

Hi,
I need some help,
I can not filter numbers only when I have numbers and text in a column.
I am using regex etc.
The search works fine when its only text or number etc. If it both text and number then it only finds the ones with text only.

I have attached part of the code and images

`function ShowTableData(url, Table_ID, columnsUrl, placeholderId, tableTitle,searchValue) {
    var closeClass = $("." + Table_ID).closest(".card-box").removeClass('hide');

    $("." + Table_ID).html(tableTitle);
    $.get(columnsUrl, function (data, status) {

        if (data.trim() != '' && data.trim() != null) {
            var jsonData = JSON.parse(data);
            $.each(jsonData.columnTitle, function (i, title) {
                $("#" + Table_ID + " thead tr").append("<th>" + title + "</th>");
                var titleValue = title.trim() +'/'+jsonData.columnDb[i].trim();
                $("#" + Table_ID + " tfoot tr").append("<th>" + titleValue + "</th>");
            });

            /*$.each(jsonData.columnTitle, function (i, title) {
             $("#" + Table_ID + " thead tr").append("<th>" + title + "</th>");
             $("#" + Table_ID + " tfoot tr").append("<th>" + title + "</th>");
             });*/
        }
        setTimeout(function () {
            var DataTable = $("#" + Table_ID).DataTable({

                "ajax": url,
                   //"scrollx":        200,
                   //"deferRender":    true,
                   //"scroller":       true,
                   "deferRender":    true,


                dom:
                "<'row'<'col-sm-12 table-controls'fBl>>" +  "<'row'<'col-sm-12'tr>>" +
                "<'row'<'col-sm-3'i><'col-sm-12'p>>",
                buttons: [
                    {extend: "copy", className: "buttonsToHide"},
                    {extend: "csv", className: "buttonsToHide"},
                    {extend: "excel", className: "buttonsToHide"},
                    {extend: "pdf", className: "buttonsToHide"}
                ],

                "search": {
                    "regex": true,
                    //"input": true,
                    //"caseInsen": true
                    //"smart": false
                },


            });
            $("#" + Table_ID + "_wrapper .dt-buttons").append(tableRefreshButton(Table_ID));
            $("#" + Table_ID + "_wrapper .dt-buttons").append(tableToolsButton(Table_ID));
            $('body').on("click", ".CopyData", function () {
                var tableId = $(this).attr("data-tableId");
                $("#" + tableId + '_wrapper  button.buttons-copy').click();
            });

            $('body').on("click", ".ExportReporttoCSV", function () {
                var tableId = $(this).attr("data-tableId");
                $("#" + tableId + '_wrapper  button.buttons-csv').click();
            });

            $('body').on("click", ".ExportReporttoExcel", function () {
                var tableId = $(this).attr("data-tableId");
                $("#" + tableId + '_wrapper  button.buttons-excel').click();
            });

            $('body').on("click", ".ExportReporttoPdf", function () {
                var tableId = $(this).attr("data-tableId");
                $("#" + tableId + '_wrapper  button.buttons-pdf').click();
            });
            $('body').on("click", ".RefreshPage", function () {
                var tableId = '';
                var columnName = '';
                var searchArray = [];
                var searchValueParam = '';
                var obj = {};
                var searchStr = "";
                var searchValue = '';
                var url = $(location).attr('href');
                $(".search_by").each(function(index ) {

                    searchValue = $( this ).val();

                    if(searchValue != '' && searchValue != null){
                        searchStr += (searchValue + '~');
                        columnName = $(this).attr("data-columnname");
                        tableId = $(this).attr("data-placeholderId");
                        obj[columnName] = searchValue;
                    }
                });

                searchArray.push(obj);
                JSON.stringify(searchArray);
                searchValueParam = JSON.stringify(searchArray);
                url = removeUrlParameter(url, 'pholderid');
                url = removeUrlParameter(url, 'searchvalue');
                url = url + '&pholderid=' + tableId + '&searchvalue=' + searchValueParam;
                window.location.href = url;
            });

            $("#" + Table_ID + " tfoot th").each(function () {
                var title = $(this).text().split('/');
                if (title[0].toLowerCase() != 'action' && title[0].toLowerCase() != 'actions') {

                    if (searchValue.trim() != '' && searchValue.trim() != null) {
                        var searchValueJson = JSON.parse(searchValue);
                        var inpputValue = '';
                        $.each(searchValueJson, function (key, value) {
                            if(key == title[1]) {
                                inpputValue = 'value="'+value +'"';
                            }
                        });

                    }
                    $(this).html('<input type="text" '+inpputValue+' class="form-control form-control-sm search_by" data-placeholderId="' + placeholderId + '" data-columnName="' + title[1] + '" placeholder="Search ' + title[0] + '" />');
                } else {
                    $(this).html('');
                }
            });

            DataTable.columns().every(function () {

                var thatOne = this;
                $('input', this.footer()).on('keyup change', function () {
                    if (thatOne.search() != this.value) {
                        thatOne
                            .search(this.value, true, false)
                            .draw();
                    }
                });
            });
        }, 100);
    });
}

$(document).ready(function () {
    $('body').on("keyup change", ".search_by", function (event) {
        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (keycode == '13') {
            var tableId = $(this).attr("data-placeholderId");
            var url = $(location).attr('href');
            var searchValue = $(this).val();
            if (searchValue.trim() != '' || searchValue.trim() != null) {
                url = removeUrlParameter(url, 'pholderid');
                url = removeUrlParameter(url, 'searchvalue');
                url = url + '&pholderid=' + tableId + '&searchvalue=' + searchValue;
                window.location.href = url;
            }
            return false;
        }
    });

});

function removeUrlParameter(url, parameter) {
    var urlParts = url.split('?');

    if (urlParts.length >= 2) {
        // Get first part, and remove from array
        var urlBase = urlParts.shift();

        // Join it back up
        var queryString = urlParts.join('?');

        var prefix = encodeURIComponent(parameter) + '=';
        var parts = queryString.split(/[&;]/g);

        // Reverse iteration as may be destructive
        for (var i = parts.length; i-- > 0;) {
            // Idiom for string.startsWith
            if (parts[i].lastIndexOf(prefix, 0) !== -1) {
                parts.splice(i, 1);
            }
        }

        url = urlBase + '?' + parts.join('&');
    }

    return url;
}
`




This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    Hi @samsamtest ,

    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

  • samsamtestsamsamtest Posts: 52Questions: 15Answers: 1

    Hi okay thanks, I will send a link to the page soon.

  • samsamtestsamsamtest Posts: 52Questions: 15Answers: 1
    edited March 2019

    HI, I am not allowed to show the hole page in this moment... Is it any way I can send a video clip on what I am doing. with the code that I have sent ?

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    Can you put together a simple test case with an example of your data and just the code needed to replicate the issue? We don't need the full solution. We just need enough to see what you are doing.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • colincolin Posts: 15,144Questions: 1Answers: 2,586
    Answer ✓

    Yep, as Kevin said, we need to see something. In this example here, if you search for "22" or "K2", the matches are as expected - so the problem is going to be something with your data. Without seeing it behave in that way, i'll be impossible to diagnose.

    C

  • samsamtestsamsamtest Posts: 52Questions: 15Answers: 1

    Hi,
    Thanks for the help..
    I am building a example now from the code. I should be ready soon. :-)

  • samsamtestsamsamtest Posts: 52Questions: 15Answers: 1

    THIS IS SOLVED NOW. I ADDED A NEW CODE WHERE I COULD SELECT THE TYPE OF THE COLUMN.

This discussion has been closed.