0

I am searching jquery autocomplete to search for entities on a server. I would like to be able to create new entities from the search result list by including an item "Create entity" as the last result, or only result when there is no result found. From the docs it looks like I would use the select event to create a new entity when I select "Create entity from the results list. I am, however, a bit unsure about the right way to add this extra item to the list, including when there are no search results.

I am using JQuery UI 1.10

4

1 回答 1

0

首先,您可以覆盖源方法以返回一组自定义响应。在这种情况下,创建实体总是附加到返回的列表中。然后处理选择事件以提示实体。

 var availableTags = ['car', 'duck', 'house'];
 $('#elementID').autocomplete({
      source: function( request, response ) {
          var responses = $.ui.autocomplete.filter(availableTags, request.term);
          responses.push('Create Entity');
          response( responses );
      },
      select: function( event, ui ) {
          var choice = ui.item.value;
          if (choice == 'Create Entity') {
              var selection = prompt('New Entity', 'default value');
              availableTags.push(selection);
              console.log(selection);
              this.value = selection;
              return false;
          }
      }
  });

jsFiddle

于 2013-07-01T14:12:12.570 回答