Bootstrap type ahead bind values with knockoutjs not working

前端 未结 1 1881
北荒
北荒 2021-01-15 01:54

I have a ViewModel that I created with knockout which contains all the info for my product. And It looks like this:

var ProductViewModelDS = function (data)         


        
相关标签:
1条回答
  • 2021-01-15 02:09

    Looking at the link you provided, have you not got your target and source the wrong way round?

    Basically, it appears that the type ahead plugin wants an array of string values it can use for the auto-complete suggestions. Add a SearchText property to the ProductViewModel that loops through all the products and puts the three fields you want into the searchterm array.

    var ProductViewModel = function () {
        self.SearchText = ko.computed(function()
        {
            var searchableTerms = [];
            ko.utils.arrayForEach(self.Products(), function (item)
            {
                searchableTerms.push(item.ProductSKUName());
                searchableTerms.push(item.ProductSKUStockCode());
                searchableTerms.push(item.ProductSKUManufacturePartNumber());
            });
            return searchableTerms;
        });
    };
    

    Then update the html to something like:

    <div class="well">
        <input type="text" data-bind="typeahead: { target: Products, source: SearchText }" />
    </div>
    
    0 讨论(0)
提交回复
热议问题