eisenbraun / columns

A jQuery plugin that converts JSON data into searchable, sortable, HTML tables
http://eisenbraun.github.io/columns
MIT License
104 stars 47 forks source link

Bug or Feature? -- Table will not search on numeric data #4

Closed gdvallance closed 9 years ago

gdvallance commented 9 years ago

I want to begin by saying "Thank you," for a wonderful well documented project.

I am writing because I would like to know whether "... all data is searchable" (default option for searchableField) means that numeric data is searchable too.

I ask because it appears that numeric data is NOT searchable. Is this correct or is it a bug?

To see what I mean I have created a basic page (see below). Everything except the nhs_number is searchable.

@gdvallance

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/classic.css">
</head>
<body>
    <h1>Test</h1>
    <p>A page to demonstrate that numeric data is not searched.</p>

    <div id="patient-list"></div>

    <script src="lib/jquery-1.11.0.min.js"></script>
    <script src="js/jquery.columns.min.js"></script>
    <script>
        // Mock data 
        var data = {
            "record": [
                {
                    "id": 1,
                    "first_name": "Beau",
                    "surname": "Scott",
                    "birthdate": "",
                    "address": "7127 Sociosqu Ave",
                    "postcode": "FN4Y 9GH",
                    "nhs_number": 2147483647,
                    "gender": "Male"
                },
                {
                    "id": 2,
                    "first_name": "Garrison",
                    "surname": "Sheppard",
                    "birthdate": "",
                    "address": "906-7498 Facilisis St.",
                    "postcode": "BD4J 6OX",
                    "nhs_number": 1611531426,
                    "gender": "Male"
                },
                {
                    "id": 3,
                    "first_name": "Erich",
                    "surname": "Crane",
                    "birthdate": "",
                    "address": "231-2271 Curabitur St.",
                    "postcode": "U9G 8WA",
                    "nhs_number": 2147483647,
                    "gender": "Male"
                }
            ]
        };

        // Turn object into an array that Columns can use.
        // The above is an exemplar object returned from a REST call to DreamFactory.

        var report = [];
        for (var i=0; i<data.record.length; i++) {
            report.push(data.record[i]);
        }

        $(document).ready(function () {
            $('#patient-list').columns({
                data: report
            });
        });
    </script>
</body>
</html>
eisenbraun commented 9 years ago

When searching for data element with a type of number, like nhs_number, your search query must match the number exactly. The current version of Columns does not have an option to change this. However, for a type of string, your search query only needs to match any part of the string.

So, quickest way to fix your problem is to convert nhs_number to a string. See below:

for (var i=0; i<data.record.length; i++) {
     data.record[i].nhs_number = data.record[i].nhs_number.toString();  //convert number to string
     report.push(data.record[i]);
}

Thanks, Michael

gdvallance commented 9 years ago

Many thanks for your prompt reply and helpful suggestion on how to address the issue. It is greatly appreciated. From my point of view the issue is closed.

Perhaps a small update to the searchableFields part of the documentation stating something like "While all data is searchable searches on numeric fields must be exact before they will return a result whereas search on text fields will return partial results." would be useful. If nothing else, this will clarify the scope of the search functionality so that you are not bothered in the future by people such as myself who do at least try and RTFM before asking questions. :-)

@gdvallance