TypedInput value always is str

I can change payload type run time. Payload type always is "str" when i open properties again. :joy:

Noname

HTML

 <label for="node-input-payload"> payload</label>
  <input type="text" id="node-input-payload" style="width:200px;">
   <input type="hidden" id="node-input-payload-type" style="width:200px;">
<script type="text/javascript">
<script type="text/javascript">
    RED.nodes.registerType('iiotgw-dev in',{
        category: 'JinYi Studio',
        color:"#c6dbef",
        defaults: {
            name:   { value:"" },           
            pin:    { value:"", required:true, validate:RED.validators.number() },                      
            read:   { value:false },
            user:   { value:false },
            payload: { validate: RED.validators.typedInput("propertyType") },
            edge:   { value:1}
        },
        inputs:1,
        outputs:1,
        icon: "rpi.png",
        label: function() {            
            return this.name || 'GPIO'+this.pin ;
        },
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        },
        outputLabels: function() { return 'GPIO'+this.pin; },
        paletteLabel: "Dev in",
        oneditprepare: function() {   
            var setstate1 = function () {
                if ($('#node-input-user').is(":checked")) {
                    $("#node-set-payload").show();
                } else {
                    $("#node-set-payload").hide();
                }                
           };

            var setstate = function () {
                var n1 = Number($("#node-input-edge").val());
                // alert("val="+n1);                
                if (n1===1) {
                   $("#node-set-user").hide();
                } else {
                   $("#node-set-user").show();                   
                }      
                setstate1();
           };
           
           $("#node-input-edge").change(function () { setstate(); });          
           $("#node-input-user").change(function () { setstate1(); });
           setstate();
           $("#node-input-payload").typedInput({               
                type:"str",
                types:["str","num","bool"],
                typeField: "#node-input-payload-type"
            });          
        }
    });
</script>

You are setting it to string on initialisation.

Remove the type:"str" restart node-red, refresh browser, try it again.

2 Likes

I tried, the same result :joy: It always show "str" type.

$("#node-input-payload").typedInput({               
        types:["str","num","bool"],
        typeField: "#node-input-payload-type"
});

It is OK when i add ( payloadtype: {value: 'num' } ) for payloadtype :star_struck: :star_struck:

defaults: {
            name:   { value:"" },           
            pin:    { value:"", required:true, validate:RED.validators.number() },                      
            read:   { value:false },
            user:   { value:false },
            payload: { value: 1, validate: RED.validators.typedInput('payloadtype')},
            payloadtype: {value: 'num' },
            edge:   { value:1, required:true}
},

js , :boom:change node.payload to node.payloadtype selected data type !
var payload1 = RED.util.evaluateNodeProperty(node.payload, node.payloadtype, node);

Ah yes, I missed the fact you hadn't added the variable into the default object. That too is important. Note, you can set it to str in the default if you wish, the important part is that the variable is declared.

1 Like