【问题标题】:Using object variables as data source for Materialize Autocomplete使用对象变量作为 Materialise 自动完成的数据源
【发布时间】:2018-04-01 12:34:05
【问题描述】:

我正在寻找熟悉 Materialize 框架的人。我正在使用内置的自动完成功能,但无法动态获取数据。示例如下所示:

$(‘input.autocomplete’).autocomplete({
data: {
“Apple”: null,
“Microsoft”: null,
“Google”: ‘https://placehold.it/250x250’
},
limit: 20, // The max amount of results that can be shown at once. Default: Infinity.
onAutocomplete: function(val) {
// Callback function when value is autcompleted.
},
minLength: 1, // The minimum length of the input for the autocomplete to start. Default: 1.
});

我想创建自己的对象以用作数据:

var itemNames = {};

function getNames() {

    var itemName;
    var itemIndex;
    var items = $(‘tr’);
    for (var i = 0; i < items.length; i++) {
    itemNames["${items[i].childNodes[0].innerText}"] = ‘img/md.png’;
    }

}

setTimeout(function(){
getNames();
}, 2000);

$(’.autocomplete’).autocomplete({
data: itemNames,
limit: 5,
onAutocomplete: function(val) {

  },
  minLength: 2
});

我从页面上的表格中获取此名称列表,该表格是从 firebase 填充的。两件事:

我必须延迟 getNames 函数,否则 itemNames 对象不会填充。

自动完成功能没有连接到 itemNames 对象。

如果我手动将对象设置为数据,自动完成功能会起作用,但它不喜欢该变量。没有抛出错误,只是没有建议数据。

有什么想法吗?我也试过将数据设置为函数,没有骰子。

【问题讨论】:

    标签: json autocomplete materialize


    【解决方案1】:

    将自动完成功能移入 setTimeout 似乎可行。不确定当对象更新时是否有任何长期影响。会回来报告的。

    setTimeout(function(){
         getNames();
         console.log($itemNames);
         $('.autocomplete').autocomplete({ data: $itemNames,
           limit: 5, onAutocomplete: function(val) {}, minLength: 2 });
       }, 2000);
    

    【讨论】:

      猜你喜欢
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 2013-07-03
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      相关资源
      最近更新 更多