自动完成在asp.net中选择多个标签

时间:2015-12-15 12:54:17

标签: c# asp.net web-services autocomplete tokenize

任何人都可以告诉我如何在自动完成中使用标记化进行多项选择,我确保,我只想通过web.net服务使用asp.net web 我的代码:

$(function () {
    // Web servcice javascript code for City
    $("[id*=ctl00_ContentMain_TextBoxSkills]").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '<%=ResolveUrl("~/WebServices/WebServiceSkills.asmx/GetAutoCompleteData")%>',
                data: "{ 'username': '" + request.term + "'}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.d.length > 0) {
                        response($.map(data.d, function (item) {
                            return {
                                label: item.split('-')[0],
                                val: item.split('-')[1]
                            };
                        }))
                    } else {
                        response([{ label: 'No results found.', val: -1 }]);
                    }
                }
            });
        },
        select: function (e, u) {
            if (u.item.val == -1) {
                return false;
            }
        }
    });
});

我想使用Web服务从数据库中获取数据并在前端显示以进行多项选择

Web Service:
DataTable dt = userRegistrationHelper.GetSkillsList(username);
        DataRow[] rows = null;
        rows = dt.Select(string.Format("SkillName = {0}", username));
        string[] result = new string[rows.Length];
        for (int i = 0; i <= rows.Length - 1; i++)
        {
            result[i] = rows[i]["SkillName"].ToString();
        }
        return result;

1 个答案:

答案 0 :(得分:0)

Autocomplete with multiple words or values with comma separated 

   $(function () {
     $("[id*=ctl00_ContentMain_TextBoxSkills]").autocomplete({
    source: function(request, response) {
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
     url: '<%=ResolveUrl("~/WebServices/WebServiceSkills.asmx/GetAutoCompleteData")%>',
    data: "{'username':'" + extractLast(request.term) + "'}",
    dataType: "json",
    success: function(data) {
    response(data.d);
    },
    error: function(result) {
    alert("Error");
    }
    });
    },
    focus: function() {
    // prevent value inserted on focus
    return false;
    },
    select: function(event, ui) {
    var terms = split(this.value);
    // remove the current input
    terms.pop();
    // add the selected item
    terms.push(ui.item.value);
    // add placeholder to get the comma-and-space at the end
    terms.push("");
    this.value = terms.join(", ");
    return false;
    }
    });
    $("[id*=ctl00_ContentMain_TextBoxSkills]").bind("keydown", function(event) {
    if (event.keyCode === $.ui.keyCode.TAB &&
    $(this).data("autocomplete").menu.active) {
    event.preventDefault();
    }
    })
    function split(val) {
    return val.split(/,\s*/);
    }
    function extractLast(term) {
    return split(term).pop();
    }
    });
相关问题