开发者

Design question Active Record Search vs. Javascript Search

I have 300 records in a table that include the following: Name, address, city, state and zip code.

  1. I want to pull All 300 records once when a user access my application, perhaps in JSON format (but not necessarily, could be an array or ruby object), hidden out of sight, but on the client side.
  2. I want to search those records on the Client side

Are there any simple Javascript/JQuery Libraries in existence where I could search the client side data and simply display the matching开发者_JS百科 result. Are there any disadvantages I should consider with this approach?

Feature: User visits app, User sees a search bar, User types in "Toledo" and the app returns 2 matching toledo records.


It is pretty straight forward. Not sure what you need a library for, other than to do the DOM manipulation once you've found your results.

function searchArrayFields(search, objectArray)
{
     var results = [];
     for (var i=0; i < objectArray.length; i++)
     {
        for (var key in objectArray[i])
        {
            if (objectArray[i][key].indexOf(search) > -1)
            {
                results.push(objectArray[i]);
                break;
            }
        }
     }
     return results;
}

Here's some example dom stuff too.

function buildAddress(row)
{
   var retVal = '<div class="container"><div class="name">'+row.name + '</div>';
   retVal += '<div class="address-line-1">'+ row.address+'</div>';
   retVal += '<div class="address-line-2"><span class="city">'+ row.city+'</span>';
   retVal += ' <span class="state">'+ row.state+'</span>';
   retVal += ', <span class="zip">'+row.zip+'</span></div></div>';
   return retVal;
}
function displayAddresses(search)
{
   var rows = searchArrayFields(search, global_db);
   var retVal = [];
   for (var i=0; i < rows.length; i++)
   {
       retVal.push(buildAddresses(rows[i]));
   }
   $('#target').html(retVal.join(''));
}

Assumes your data gets stored like this, which would be a standard JSON setup:

[{name:'John Doe', address:'111 Main Street', city:'Toledo', state:'OH',zip:44343}];


Maybe the jQuery autocomplete plugin would work well for this situation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜