简体   繁体   English

typeahead.js,localStorage和大型json文件

[英]typeahead.js, localStorage and large json File

I've got a json file wich has a size of 1Mb. 我有一个大小为1Mb的json文件。 I tried to implement typeahead.js with a simple example like this : 我尝试使用如下的简单示例实现typeahead.js:

    <div class="container">
    <p class="example-description">Prefetches data, stores it in localStorage, and searches it on the client: </p>
    <input id="my-input" class="typeahead" type="text" placeholder="input a country name">
  </div>

   <script type="text/javascript">
    // Waiting for the DOM ready...
    $(function(){

      // applied typeahead to the text input box
      $('#my-input').typeahead({
        name: 'products',

        // data source
        prefetch: '../php/products.json',

        // max item numbers list in the dropdown
        limit: 10
      });

    });
  </script>

But when I launch it chrome says: 但是当我推出它时,chrome说:

Uncaught QuotaExceededError: Failed to execute 'setItem' on 'Storage': Setting the value of '__products__itemHash' exceeded the quota. 未捕获的QuotaExceededError:无法在“存储”上执行“setItem”:设置“__products__itemHash”的值超出了配额。

What can I do? 我能做什么? I'm using the typeahead.min.js 我正在使用typeahead.min.js

You are seeing that error because typeahead prefetch uses localStorage to store the data. 您看到该错误,因为typeahead prefetch使用localStorage来存储数据。

Firstly, storing 1MB of data on the client side is not really good in term of user experience. 首先,在客户端存储1MB的数据在用户体验方面并不是很好。

Given that, you can still solve the problem with multiple-datasets. 鉴于此,您仍然可以使用多个数据集解决问题。 This is just a workaround and may not be the most elegant solution but it works perfectly. 这只是一种解决方法,可能不是最优雅的解决方案,但它完美无缺。

The sample data I tested with was >1MB and looks like this 我测试的样本数据大于1MB,看起来像这样

在此输入图像描述

You can view the sample here (It takes a while to open) 你可以在这里查看样本(打开需要一段时间)

Procedure: 程序:

  1. First download the entire data using $.getJSON 首先使用$.getJSON下载整个数据
  2. Then split the data into chunks of 10,000 (just a magical number that worked for me across browsers. Find yours) 然后将数据拆分为10,000个块(只是一个在浏览器中适用于我的神奇数字。找到你的)
  3. Created sets of bloodhounds for each chunk and store everything in an array. 为每个块创建一组猎犬并将所有内容存储在一个数组中。
  4. Then initialize typeahead with that array 然后使用该数组初始化typeahead

Code: 码:

$.getJSON('data.json').done(function(data) { // download the entire data
  var dataSources = [];
  var data = data['friends'];
  var i, j, data, chunkSize = 10000; // break the data into chunks of 10,000
  for (i = 0, j = data.length; i < j; i += chunkSize) {
    tempArray = data.slice(i, i + chunkSize);
    var d = $.map(tempArray, function(item) {
      return {
        item: item
      };
    });
    dataSources.push(getDataSources(d)); // push each bloodhound to dataSources array
  }
  initTypeahead(dataSources); // initialize typeahead 
});

function getDataSources(data) {
  var dataset = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('item'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local: data,
    limit: 1 // limited each dataset to 1 because with 76,000 items I have 8 chunks and each chunk gives me 1. So overall suggestion length was 8
  });
  dataset.initialize();
  var src = {
    displayKey: 'item',
    source: dataset.ttAdapter(),
  }
  return src;
}

function initTypeahead(data) {
  $('.typeahead').typeahead({
    highlight: true
  }, data); // here is where you use the array of bloodhounds
}

I created a demo here with 20 items and chunkSize of 2 just to show how multiple-datasets would generally work. 我在这里创建了一个包含20个项目和chunkSize为2的演示,以显示多个数据集通常如何工作。 (Search for Sean or Benjamin) (搜索肖恩或本杰明)

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM