Random number in hidden field

Random number in hidden field

RappiRappi Posts: 82Questions: 18Answers: 1

Hello Allan,

I have a new question :D

I need a random number in a hidden field in editor.

What I have is this:

var editor = new $.fn.dataTable.Editor( {
        "ajax": "adm_artikel_processing.php",
        "table": "#pm_artikel",
        "fields": [
        
            {
                "label": "Name",
                "name": "tm_pen_artikel.Name"
            },
            {
                "label": "code",
                "name": "tm_pen_artikel.code",
                "def": function (val){
                    var min = 1111111111;
                    var max = 9999999999;
                    var zufall = Math.floor(Math.random() * (max - min + 1)) + min;
                    return val = zufall;
                },
                "type": "hidden"
            },
            {
                "label": "Beschreibung",
                "name": "tm_pen_artikel.Beschreibung"
            },
            {
                "label": "Preis",
                "name": "tm_pen_artikel.Preis",
                attr: {
                        type: 'number',
                        lang: 'de'
                }
            }
        ],

But with this solution, the number is every time the same.
I need a new number after every new entry and after edit an entry.

How can I do this? Is this possible?

Rappi

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,394Questions: 26Answers: 4,786
    edited February 2017 Answer ✓

    I tried your random code and it generates a different number each time i open the editor. I checked it but not hiding the field. Have you tried that to verify?

    EDIT: I'll let those more familiar with Javascript comment but I don't think you want function(val) but instead want function() and change your return statement to return zufall;.

    Kevin

  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394

    EDIT: I'll let those more familiar with Javascript comment but I don't think you want function(val) but instead want function() and change your return statement to return zufall;.

    @Kevin, I agree. That function made no sense to me, but I'm no expert.

  • RappiRappi Posts: 82Questions: 18Answers: 1

    Hi Kevin,

    thanks.

    EDIT: I'll let those more familiar with Javascript comment but I don't think you want function(val) but instead want function() and change your return statement to return zufall;

    That's it!

    Rappi

This discussion has been closed.