简体   繁体   English

自动完成逗号分隔文本框中的多个值。 我想用列表替换硬编码的值 <String> 在控制器类中可用

[英]Autocomplete comma separated multiple values in textbox. I want to replace hardcoded values with List<String> available in controller class

I am struggling to replace the JSON source list with my List avaliable in controller class. 我正在努力用控制器类中可用的列表替换JSON源列表。

The below code works fine. 下面的代码工作正常。 But I want to replace the hardcoded list "availableTags" with my List<String> by redirecting the call to controller method (using url). 但是我想通过将调用重定向到控制器方法(使用url)来用我的List<String>替换硬编码列表“ availableTags”

Please advise. 请指教。

jquery code: jQuery代码:

$(function () {

    var availableTags = [
        "ActionScript",
        "AppleScript",
        "Asp",
        "BASIC",
        "C",
        "C++",
        "Clojure",
        "COBOL",
        "ColdFusion",
        "Erlang",
        "Fortran",
        "Groovy",
        "Haskell",
        "Java",
        "JavaScript",
        "Lisp",
        "Perl",
        "PHP",
        "Python",
        "Ruby",
        "Scala",
        "Scheme"];

    function split(val) {
        alert($ {
            acTypes
        });
        return val.split(/,\s*/);
    }

    function extractLast(term) {
        return split(term).pop();
    }

    $("#tags")
    // don't navigate away from the field on tab when selecting an item
    .bind("keydown", function (event) {
        if (event.keyCode === $.ui.keyCode.TAB && $(this).data("ui-autocomplete").menu.active) {
            event.preventDefault();
        }
    })
        .autocomplete({
        minLength: 0,
        source: function (request, response) {
            // delegate back to autocomplete, but extract the last term
            response($.ui.autocomplete.filter(
            availableTags, extractLast(request.term)));
        },
        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;
        }
    });
}); 

input textbox field: 输入文本框字段:

<div class="ui-widget">
    <label for="tags">Tag programming languages:</label>
    <input id="tags" size="50">
</div>

when I implement single value autocomplete I am able to redirect to url. 当我实现单值自动完成功能时,我可以重定向到url。 Please see below for Single value text box autocomplete. 请参见下面的“单值”文本框自动完成功能。

jquery code: jQuery代码:

$("#tags").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "/test/testPublisher/ajaxList.htm",
            minLength: 1,
            delay: 300,
            data: {
                query: request.term,
                rowLimit: 10,
                companyStateBothFlag: 0
            },
            dataType: "text",
            success: function (data) {
                var dataArray = data.split("\n");
                response($.map(dataArray, function (item) {
                    return {
                        label: item,
                        value: item == "No results returned for this query" ? "" : item.split(" ")[0]
                    }
                }))
            }
        })
    }
});

You should be able to do this by using the extractLast function and sending up the result of calling that function on request.term : 您应该能够通过使用extractLast函数并在request.term上发送调用该函数的结果来做到这一点:

.autocomplete({
    minLength: 0,
    source: function (request, response) {
        // extract the last term:
        var term = extractLast(request.term);

        $.ajax({
            url: "/test/testPublisher/ajaxList.htm",
            minLength: 1,
            delay: 300,
            data: {
                query: term,
                rowLimit: 10,
                companyStateBothFlag: 0
            },
            dataType: "text",
            success: function (data) {
                var dataArray = data.split("\n");
                response($.map(dataArray, function (item) {
                    return {
                        label: item,
                        value: item == "No results returned for this query" ? "" : item.split(" ")[0]
                    };
                }))
            }
        });
    },
    /* other options omitted */
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM