【问题标题】:Typeahead/Bloodhound - number of resultsTypeahead/Bloodhound - 结果数量
【发布时间】:2015-09-18 15:17:45
【问题描述】:

我一直在一个项目中使用 typeahead 和 Bloodhound,但我发现它非常不直观且令人沮丧。需要一秒钟才能完成的事情最终会在一个小时的研究中结束。

无论如何,我正试图提醒我的猎犬的结果数量。

这是我目前的代码

 var pisList = new Bloodhound({
                datumTokenizer: Bloodhound.tokenizers.whitespace,
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                prefetch: {
                    url: "../Helper/LookUpPIs?list=" + $list,
                    cache: false
                }
            });

            alert(pisList.length);

            //Typeahead on project numbers
            $('.pis').typeahead({
                hint: true,
                higlight: true,
                minLength: 1
            },
                {
                    name: 'pis',
                    source: pisList
                });

            //Display project details distribution panel
            $('input.pis').on('typeahead:selected', function (event, selection) {
                var result = selection.match(/\((.*)\)/);
                getPiInformation(result[1]);
            });
            return false;
        }

现在我的警报返回我未定义。我尝试了多种变体,但都没有成功。

如果您有任何提示,请告诉我,并提前致谢。

【问题讨论】:

  • 显然没那么容易
  • 发帖我需要帮助

标签: jquery typeahead bloodhound


【解决方案1】:
alert(

    Object.keys(pisList.index.datums).length 

);

【讨论】:

    【解决方案2】:

    这是一种愚蠢的方式,但它确实有效。我必须创建一个隐藏字段,遍历 datumtokenizer 中的每个结果并增加值。

    var pisList = new Bloodhound({
                    //datumTokenizer: Bloodhound.tokenizers.whitespace,
                    datumTokenizer: function (d) {
                        //count number of PIS found and put their ID in the field
                        $('#numberofPIs').val(Number($('#numberofPIs').val()) + Number(1));
                        return tokens;
                    },
                    queryTokenizer: Bloodhound.tokenizers.whitespace,
                    prefetch: {
                        url: "../Helper/LookUpPIs?list=" + $list,
                        cache: false
                    }
                });
    

    可能不是最好和最干净的方法,但它确实有效

    【讨论】:

      猜你喜欢
      • 2014-05-08
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      相关资源
      最近更新 更多