我正在Asp.net MVC项目中使用JQuery自动完成插件。到目前为止,我从搜索方法中返回了一个字符串列表,并且这些字符串正确显示。
在我看来:
<script type="text/javascript">
$(function () {
$("#search").autocomplete({
source: '<%: Url.Action("Search") %>',
minLength: 1
});
});
</script>
控制器:
public JsonResult Search(string term)
{
//search Code here ...
var dt = ....;
var result = new List<string>();
//search Code returns a DataTable, which I convert into a List<string>
for (int i = 0; i < dt.Rows.Count; i++)
{
result.Add(...);
}
//return List<string> to the autocomplete plugin
return Json(result, JsonRequestBehavior.AllowGet);
}
我想知道是否有可能返回一个DataTable对象并在HTML表中显示该DataTable。还是可以返回HTML表?有人尝试过这样的事情吗?
非常感谢
Jaspis
步骤1:准备好Web Api
首先创建一个Web api方法,它将使用从自动完成文本框中发送的搜索词query返回项目列表(艺术家)。在本文中,我不使用数据库,而是使用List来使此示例尽可能简单。
下面是我如何定义Artist类的方法
public class Artist
{
public int Id { get; set; }
public int Name { get; set; }
}
下一步,我创建了一个Web Api GET方法,它将使用在自动完成文本框中输入的搜索词,并且在LINQ的帮助下,将返回匹配结果的列表。
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace Autocomplete.Controllers
{
public class ArtistApiController : ApiController
{
public List<Artist> ArtistList = new List<Artist>
{
new Artist{Id = 1, Name = "Sonu Nigam"},
new Artist{Id = 2, Name = "Sunidhi Chauhan"},
new Artist{Id = 3, Name = "Shreya Goshal"},
new Artist{Id = 4, Name = "Mohit Chauhan"},
new Artist{Id = 5, Name = "Nihkil Dsouza"},
new Artist{Id = 6, Name = "Kailash Kher"},
new Artist{Id = 7, Name = "Atif Aslam"},
new Artist{Id = 8, Name = "Ali Zafar"},
new Artist{Id = 9, Name = "Shafaqat Ali"},
new Artist{Id = 10, Name = "Shankar Madahevan"}
};
// GET api/values
public IEnumerable<Artist> Get(string query)
{
return ArtistList.Where(m => m.Name.Contains(query)).ToList();
}
}
}
我们的服务器端代码已准备就绪!该测试一下了。
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS82TWdEVC5wbmcifQ==” alt =“在此处输入图像描述”>
步骤2:客户端代码
在您的html中包含jquery-ui.js和jquery.ui.css
<script type="text/javascript" src="~/Scripts/jquery-1.7.1.min.js" ></script>
<script type="text/javascript" src="~/Scripts/jquery-ui-1.8.20.min.js" ></script>
<link href="~/Content/themes/base/jquery.ui.all.css" rel="stylesheet" />
<div id="body">
<label for="autocomplete-textbox">Search : </label>
<input type="text" id="autocomplete-textbox" />
</div>
<script type="text/javascript">
$(document).ready(function (){
$('#autocomplete-textbox').autocomplete({
source: function (request, response) {
// prepare url : for example '/api/artistapi?query=sonu
var autocompleteUrl = '/api/artistapi' + '?query=' + request.term;
$.ajax({
url: autocompleteUrl,
type: 'GET',
cache: false,
dataType: 'json',
success: function (json) {
// call autocomplete callback method with results
response($.map(json, function (data, id) {
return {
label: data.Name,
value: data.Id
};
}));
},
error: function (xmlHttpRequest, textStatus, errorThrown) {
console.log('some error occured', textStatus, errorThrown);
}
});
},
minLength: 2,
select: function (event, ui) {
alert('you have selected ' + ui.item.label + ' ID: ' + ui.item.value);
$('#autocomplete-textbox').val(ui.item.label);
return false;
}
});
});
</script>
这里要注意的一件事是,在成功方法中,我使用了以下代码:
response($.map(json, function (data, id) {
return {
label: data.Name,
value: data.Id
};
}));
使用data.Id和data.Name,因为在ajax响应(如下所示)中以这种格式返回数据。
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9aaUFCVi5wbmcifQ==” alt =“在此处输入图像描述”>“ >>
第3步:测试和输出:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9LeGpjUS5wbmcifQ==” alt =“在此处输入图像描述”>
取自here
您可以看一下custom data and display示例。