3
votes

Need Value Already Set

I am using Kendo autocomplete to select an item from a list. I need to set the value of the text input field and make that actually use the value from the list.

Right now I am setting the text value of the input field to the value of the item in the table, though it is not actually using it as though you were to type it in and click on the auto-completed item.

Setting the default text :

IQueryable<StorageLocation> query = (from s in db.StorageLocations.Include("StorageLocationType") select s);
        DefaultStorageLocation = query.Count() != 1 ? "" : query.FirstOrDefault().TitleWithType;

Setting the value of the text box:

<input type="text" value="<%: Model.DefaultStorageLocation %>" name="StorageLocation" id="StorageLocation" originalname="StorageLocation" placeholder="" class="autoComplete" style="width: 158px;" />

My Kendo AutoComplete:

$('#StorageLocation' + rowID).kendoAutoComplete({
        dataTextField: 'title',
        dataValueField: 'id',
        minLength: 2,
        autoBind: false,
        select: function (e) {
            var dataItem = this.dataItem(e.item.index());
            $('#StorageLocationID' + rowID).val(dataItem.id);
        },
        dataSource: {
            type: 'json',
            serverFiltering: true,
            transport: {
                read: {
                    url: function (opt) {
                        return '/Autocomplete/StorageLocations?searchText=' + opt.filter.filters[0].value;
                    }
                }
            }
        }
    });

So in short.

  • I am setting the value of the text input
  • Need it to actually use the value as if it were selected from autocomplete
2

2 Answers

0
votes

It looks like you're trying to auto-select the first value when there is only one. I think the suggest property will do what you're looking for: http://docs.telerik.com/kendo-ui/api/javascript/ui/autocomplete#configuration-suggest

Alternatively, try the value method: http://docs.telerik.com/kendo-ui/api/javascript/ui/autocomplete#methods-value

0
votes

Controller searchResult search query

storageLocation = (DefaultWarehouseForIA && !StorageLocation.IsNull()) ? StorageLocation.TitleWithType : "",
defaultStorageLocationID = (DefaultWarehouseForIA && DefaultStorageLocationID.ToLong() > 0) ? DefaultStorageLocationID : null

JS values being filled when another text field is filled in.

select: function (e) {
    var dataItem = this.dataItem(e.item.index());
    $('#StorageLocation' + rowID).val(dataItem.storageLocation);
    $('#StorageLocationID' + rowID).val(dataItem.defaultStorageLocationID);
}

At this point I have figured it out. Have it working.

So now the value is being auto filled when another text field is being filled in. Explanation isn't too great here. But hey.