简体   繁体   English

搜索select有3个或更少选项时如何防止羊驼产生射电场

[英]How to prevent alpaca from generating radio field when the search select has 3 or less options

I have a div and a following javascript:我有一个 div 和一个以下 javascript:

let usersNotContributingIds = [ 19, 20, 21 ];
let usersNotContributingNames = [ "Flavius K.", "Pogchamp", "Lazy Gopnik" ];
let contributorToBeAddedId; // the value that will be used for further actions
$("#alpaca-search-contributing-users").alpaca({
            data: null,
            schema: {
                type: "object",
                enum: usersNotContributingIds,
            },
            options: {
                name: "pls",
                label: 'Contributor Fullname',
                optionLabels: usersNotContributingNames,
                helper: "Select user sou want to add as a contributor",
                id: "select2-search",
                focus: false,
                events: {
                    change: function() {
                        console.log(this.getValue().value);
                        contributorToBeAddedId = this.getValue().value
                    },
                    focus: function() {
                        console.log(this.name);
                    },
                    blur: function() {
                        console.log(this.name + ": blur");
                    },
                    ready: function() {
                        console.log(this.name);
                    }
                },

            },

            postRender: function(control) {
                $('#select2-search').select2();
            }
        });

Obviously, I want to get the newly set value, or anyhow access the selected value and use it.显然,我想获取新设置的值,或者无论如何访问所选值并使用它。 For example with AJAX and a button.例如 AJAX 和一个按钮。 The problem is, that when I have 3 or less options, Alpaca render the field not as a search, but as a radio-something and the this.getValue() is null.问题是,当我有 3 个或更少的选项时,Alpaca 不会将该字段呈现为搜索,而是呈现为无线电的东西,并且 this.getValue this.getValue()是 null。

Is there a way to force Alpaca to NOT USE THE RADIO BUTTONS?有没有办法强制羊驼不使用无线电按钮? I dont want to use them, even if I had only 1 option.我不想使用它们,即使我只有一个选项。 Documentation just promtly states, that if there are 3 or less options, it will generate radio buttons instead of select, but it says nothing about the fact, that it breaks everything and that I would not be able to retrieve the value the same way as with select field.文档只是迅速指出,如果有 3 个或更少的选项,它将生成单选按钮而不是 select,但它没有说明这一事实,它破坏了一切,我将无法以与带有 select 字段。

If I am doing something inefficiently or wrong, please tell me, I am new with Alpaca and I just want a neat select dropdown with search, that I can use to pick users from a list with any length.如果我做的事情效率低下或错误,请告诉我,我是 Alpaca 的新手,我只想要一个整洁的 select 下拉列表和搜索,我可以用它从任何长度的列表中选择用户。 Also, I would like the "null" or "none" option to not be there.另外,我希望“null”或“none”选项不存在。

To have your select component rendered you should use the option type and set it to "select" .要渲染您的 select 组件,您应该使用选项type并将其设置为"select"

The issue with the value is because you're using it wrong, to get the value in alpaca you only do this.getValue() and there's no need to add .value .该值的问题是因为您使用错误,要获取 alpaca 中的值,您只需执行this.getValue()并且无需添加.value

FYI: If you see the error " This field should have one of the values in Flavius K., Lazy Gopnik, Pogchamp. Current value is: 19 " you should update your enum array to have strings instead of ints let usersNotContributingIds = [ "19", "20", "21" ];仅供参考:如果您看到错误“此字段应具有 Flavius K.、Lazy Gopnik、Pogchamp 中的值之一。当前值为:19 ”您应该更新您的枚举数组以使用字符串而不是整数let usersNotContributingIds = [ "19", "20", "21" ]; . .

Here's a working fiddle for this.这是一个工作小提琴

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

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