douou8954 2014-02-26 08:45
浏览 22

使用过滤显示搜索数据

I am developing an application with a flight search API (Multicom FindAndBook), which returns the large results based on the given input.

My problem is how can I filter the returned data (Price, Arrival Time, Departure Time, Number of Stops and Airline Name) ? I found the three solutions for the filtering

1) Filtering the data at client side using jQuery

But here I am filtering the data with jQuery but I am facing difficulty to paginate the filtered data.

2) Making the Ajax request to the server to filter using PHP

But it is taking more time to load the data.

3) Dumping the search results into WebSQL database and filtering the data at client.

Here I can filter the data but is it the proper way to filter data from the local database?

Can anyone tell me which is the proper way to get the high performance?

Edit:

The API is not providing the filtering but it is taking much time for every filter request (up to 30 sec). I decided that better to get the all the search data and do the filtering on the result data.

This is the Fiddle for my jQuery filtering:

$(function () {
  var minPrice = 299,
      maxPrice = 1099,
      $filter_lists = $("#filters ul"),
      $filter_checkboxes = $("#filters :checkbox"),
      $items = $("#computers li.system");

  $filter_checkboxes.click(filterSystem);

  $('#slider-container').slider({
      range: true,
      min: minPrice,
      max: maxPrice,
      values: [minPrice, maxPrice],
      slide: function (event, ui) {
          $("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);
          minPrice = ui.values[0];
          maxPrice = ui.values[1];
          filterSystem();
      }
  });
  $("#amount").val("$" + minPrice + " - $" + maxPrice);

  function filterSlider(elem) {
      var price = parseInt($(elem).data("price"), 10);
      console.log(price);
      return price >= minPrice && price <= maxPrice;
  }

  function filterCheckboxes(elem) {
      var $elem = $(elem),
          passAllFilters = true;
      $filter_lists.each(function () {
          var classes = $(this).find(':checkbox:checked').map(function () {
              return $(this).val();
          }).get();
          console.log('classes', classes);
          var passThisFilter = false;
          $.each(classes, function (index, item) {
              if ($elem.hasClass(item)) {
                  console.log('hasClass', item);
                  passThisFilter = true;
                  return false; //stop inner loop
              }
          });
          if (!passThisFilter) {
              passAllFilters = false;
              return false; //stop outer loop
          }
      });
      return passAllFilters;
  }

  function filterSystem() {
      $items.hide().filter(function () {
          return filterSlider(this) && filterCheckboxes(this);
      }).show();
  }
});
  • 写回答

1条回答 默认 最新

  • dongshuogai2343 2014-03-02 18:20
    关注

    I would go with 1st option and make it work. Other two are kind of hacks to achieve something that can be achieved in plain javascript. If there is no default jquery feature to do this which i doubt, You should store your data in javascript variable before displaying it on UI. Every time you change filters or sorting create your subset from original saved data. Pagination is simple all you have to do is get the total count of your subset of filtered data and render page nos. Math.ceil(tatalcount/pagesize) with each page click you would know the index of your subsuet of data and fetch acvordingly. I can only provide approach you need to code it yourself. But as isaid other two approches are not efficient.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)