繁体   English   中英

如何从 select2 下拉列表中删除用户输入

[英]How to remove user input from select2 dropdown list

我正在使用 select2 jQuery 插件和 ajax 从远程服务器自动完成。 但是用户输入会附加到下拉列表中。 有人可以帮助我如何不在 select2 自动完成下拉列表中显示用户输入吗?

这是我的js代码

$('#id_available_users').select2({
            placeholder: "Select an Existing User",
            allowClear: true,
            minimumInputLength: 3,
            tags: [],
            ajax: {
                url: '/api/users/',
                dataType: 'json',
                type: "GET",
                data: function (term) {
                    return {
                        query: term.term,
                    };
                },
                processResults: function (data) {
                if (data.length > 0)
                {
                return {
                    results: $.map(data, function (item) {
                        return {
                            text: item.name + ', ' + item.email + ', ' + item.location.city + ' ' + item.location.state + ' ' + item.location.zip + ' ' + item.location.country,
                            id: item.id,
                        }
                    })
                };
                }
                else return {results: [{ 'loading' : false, 'description' : 'No result', 'name' : 'no_result', 'text' : 'No result'}]}
            }
            },

        });

在此处输入图像描述

最后,我想通了。 这是我修复它的方法:

$('#id_available_users').select2({
        placeholder: "Select an Existing User",
        allowClear: true,
        minimumInputLength: 3,
        tags: [],
        templateResult: function formatState (state) {
            if (!state.id) {
                return state.text
            }
            if (!state.text.name && !state.text.email) {
               return; // here I am excluding the user input
            }
            var $state = '<p><b>'+ state.text.name + '</b>, ' + state.text.email + ', ' + state.text.location.city + ' ' + state.text.location.state + ' ' + state.text.location.zip + ' ' + state.text.location.country +'</p>';
            return $state;
        },
        ajax: {
            url: '/api/users/',
            dataType: 'json',
            type: "GET",
            data: function (term) {
                return {
                    query: term.term,
                };
            },
            processResults: function (data) {
            if (data.length > 0)
            {
            return {
                results: $.map(data, function (item) {
                    return {
                        text: item,
                        id: item.id,
                    }
                })
            };
            }
            else return {results: [{ 'loading' : false, 'description' : 'No result', 'name' : 'no_result', 'text' : 'No result'}]}
        }
        },
    });

在这里,我已经覆盖了templateResult并检查了该项目是否没有名称,并且 email (这将是用户输入)只是返回 true 不要将其添加到下拉列表中。

暂无
暂无

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

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