2012-05-28 175 views
1

我发现了这个jQuery,我想将它集成到我的项目中。我的问题是如何应用该“源”?jQuery自动完成集成

因为在我的输入我收到所有从数据库中,我不能在HTML或jQuery之前什么都不写!

其实我使用wicket组件来自动完成,但该组件是越野车,因为如果我调整浏览器的大小,列表不会在输入下方对齐,因此我试图找到更好的方法。

如果任何人都可以HEP我:)

var $element = $('.my-autocomplete'); 
var $testinput = $element.find('.my-autocomplete-input'); 

$testinput.autocomplete({ 
    minLength: 0, 
    autoFocus: true, 
    source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"] 
    }); 

小提琴例如:http://jsfiddle.net/CSypd/36/

谢谢!

+0

似乎工作,那里有什么问题? – Joseph

回答

2

客户端

$("#element _id").autocomplete({ 
       source: function (request, response) { 
        $.ajax({ 
         url: '@Url.Action("GetEmployeeDepartmentStuff")', 
         //generates into a url. eg http://www.site.com/MyPage/GetEmployeeDepartmentStuff?filter=.... 
         dataType: "json", 
         data: { 
          filter: request.term 
         }, 
         success: function (data) { 
          response($.map(eval(data), function (item) { 
           return { 
            label: item.em_name, 
            dp_Name: item.dp_Name 
           } 
          })); 
         } 
        }) 
       }, 
       maxLength: 2, 
       select: function (event, ui) {     
        $("#Deparment").attr('value', ui.item.dp_Name); 
       } 
      }); 

服务器端

[HttpGet] 
    [CompressFilter] 
    public ActionResult GetEmployeeDepartmentStuff(string filter = "") 
    { 
     SomeRepository repo = new SomeRepository(); 
     return repo.GetEmployeeDepartmentStuff(filter); //returns a JSON result 
    } 

不知道你用什么索绪尔为您的服务器端代码,但是你需要有一个Web方法/服务/暴露的东西接受参数“过滤器”(见上文),或其他任何你想发回....还不要忘记在你的标记中包含jQuery库

+0

非常感谢:) – mcmwhfy

0

最简单的想法是通过ajax调用获取源数组。

+0

,我该怎么做? – mcmwhfy