简体   繁体   English

在alpaca.js上使用令牌字段重置表单

[英]Resetting form with token field on alpaca.js

I have spent a couple of hours trying to figure out how to properly reset a form with token field on alpaca.js. 我花了几个小时试图弄清楚如何使用alpaca.js上的令牌字段正确重置表单。

Not sure if this is a bug, or I am doing something wrong. 不知道这是一个错误,还是我做错了什么。 I placed two reset buttons, a generic one and a second one that calls a function to reset the form. 我放置了两个重置按钮,一个是通用按钮,另一个是第二个按钮,它们调用一个函数来重置表单。

<script type="text/javascript">
var value = {};
$("#myform").alpaca({
 "schema": {
    "title": "token field",
    "type": "object",
    "properties": {
        "text_field": {"type": "text" },
        "token_field": { "type": "token"},
    }                       
},
"options": {  
    "fields": {
            "text_field": {
                    "label": 'this is a text field', 
                    "type": "text"
            },
            "token_field": {
                    "label": 'this is a token field', 
                    "type": "token",
                            "tokenfield": {
                    },
            },
    },
    "form": { 
            "buttons": {
            "load": {
                    label:"load saved values",
                    "click": function() {
                            $('form#' + this.attributes.id)[0].reset();
                            this.setValue(value);
                    },      
            },
            "reset": {},
            "fancy_reset": {
                    "label": "reset with function",
                    "click": function() {
                            console.log(this.attributes.id);
                            $('form#' + this.attributes.id)[0].reset();
                    },
            },
            "save": {
                    label:"save",
                    "click":function(){
                            value = this.getValue();
                            console.log(this.getValue());
                            console.log(value);
                    },
            },
            },
    },
},
});
</script>

Any insights on whats wrong here? 对这里出什么问题有任何见解吗?

I've been working with Alpaca for year but I haven't use that type of and after some research I haven't found any solution in the Alpaca lib to reset that type of field, so I tried to use jquery and bootstrap tokenizer methods . 我已经使用Alpaca一年了,但是我没有使用过这种类型,经过一些研究,我没有在Alpaca lib中找到任何解决方案来重置该类型的字段,因此我尝试使用jquery和bootstrap标记器方法

and I've came to this solution : 而我来到了这个解决方案:

  1. Create an eventlistener to the reset form event 为重置表单事件创建一个事件监听器

     $('form').on('reset', function(e) { var control = $("#myform").alpaca("get"); var field = control.childrenByPropertyId['token_field']; field.refresh(); }); 
  2. In your load function after this.setValue(value) add this code: this.setValue(value)之后的load函数中,添加以下代码:

     if (typeof value.token_field != undefined) { // getting Alpaca parent control (this holds everything) var control = $("#myform").alpaca("get"); // get the token field var field = control.childrenByPropertyId['token_field']; // setting the saved token to the field using bs-tokenizer method setTokens $(field.control).tokenfield('setTokens', value.token_field); } 

Here's a fiddle for that. 这是一个小提琴 Tell me if you still have any issue. 告诉我您是否还有任何问题。

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

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