【问题标题】:Autocomplete Multicolumn MVC Dont show up the data自动完成多列 MVC 不显示数据
【发布时间】:2015-05-13 21:50:34
【问题描述】:

我已经成功地为自动完成实现了 jquery-ui,但我想要多列自动完成。我已经实现得太远了,但现在不知道如何从数据库中获取第一个、第二个和第三个值。

   $(function () {
        //overriding jquery-ui.autocomplete .js functions
        $.ui.autocomplete.prototype._renderMenu = function (ul, items) {
            var self = this;
            //table definitions
            ul.append("<table><thead><tr><th>ID#</th><th>Name</th><th>Company&nbsp;Name</th></tr></thead><tbody></tbody></table>");
            $.each(items, function (index, item) {
                self._renderItemData(ul, ul.find("table tbody"), item);
            });
        };
        $.ui.autocomplete.prototype._renderItemData = function (ul, table, item) {
            return this._renderItem(table, item).data("ui-autocomplete-item", item);
        };
        $.ui.autocomplete.prototype._renderItem = function (table, item) {
            return $("<tr class='ui-menu-item' role='presentation'></tr>")
              .data("item.autocomplete", item)
              .append("<td >" + item.id + "</td>" + "<td>" + item.value + "</td>" + "<td>" + item.cp + "</td>")
              .appendTo(table);
        };
            //random json values
        //var projects = [
        //    { id: 1, value: "Thomas", cp: 134 },
        //    { id: 65, value: "Richard", cp: 1743 },
        //    { id: 235, value: "Harold", cp: 7342 },
        //    { id: 78, value: "Santa Maria", cp: 787 },
        //    { id: 75, value: "Gunner", cp: 788 },
        //    { id: 124, value: "Shad", cp: 124 },
        //    { id: 1233, value: "Aziz", cp: 3544 },
        //    { id: 244, value: "Buet", cp: 7847 }
        //];
        $("#search").autocomplete({
            minLength: 3,
            source: '@Url.Action("GetMedicineName")',
        })
    });
</script>   

这是我的控制器

    Function GetMedicineName(term As String) As JsonResult
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("SHCTConnectionString").ConnectionString
        Dim conn As New SqlConnection(connectionString)
        Dim medicine As New List(Of String)
        Dim medicineID As New List(Of Integer)
        Dim medicineCompanyName As New List(Of String)
        Dim sSql = "Select TOP 20 medicine_id, medicine_name, medicine_company_name, location From UM_MEDICINE_MASTER WHERE hospital_id = 1 AND medicine_name LIKE '" & term & "%'"
        Dim cmd As SqlCommand = New SqlCommand(sSql, conn)
        conn.Open()
        Dim rst As SqlDataReader = cmd.ExecuteReader
        Do While rst.Read
            medicine.Add(rst!medicine_name)
        Loop
        medicine.ToList()
        Return Json(medicine, JsonRequestBehavior.AllowGet)
    End Function

我想要数据库中自动完成字段中的 ID、名称和公司名称的所有 3 个字段。当调用 var 项目时,所有数据都正确显示,但我想要数据库中的数据

【问题讨论】:

    标签: asp.net-mvc vb.net jquery-ui autocomplete


    【解决方案1】:

    您不能创建一个从数据库中填充的小类,然后将其作为列表返回吗?

    public class Medicine
    {
        public long id { get; set; }
        public string value { get; set; }
        public long cp { get; set; }
    }
    
    public JsonResult AutocompleteSuggestions(int conn_id, string term)
    {
    
    
        List<Medicine> Values = new List<Medicine>();
    
        Values.Add(new Medicine {
            id = 123,
            cp = 76876,
            value = "test"
        });
        Values.Add(new Medicine {
            id = 321,
            cp = 3444,
            value = "more"
        });
    
        return Json(Values, JsonRequestBehavior.AllowGet);
    
    }
    

    【讨论】:

    • 好吧,其实这是很老的帖子,我忘了回答我通过创建自己的变量得到了解决方案
    猜你喜欢
    • 1970-01-01
    • 2018-01-15
    • 2017-07-23
    • 1970-01-01
    • 2019-04-03
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    相关资源
    最近更新 更多