0

I have an array in my javascript funktion.

var data = ['Datum','Schicht','Schicht','Schicht','Dispo','Schicht','Schicht','Schicht','Schicht','Schicht','Schicht','Dispo'];

This array is displayed into a html table multiple times. The values should in the end be functions. Right now they are all just placeholders. What I want is to set them as functions, so when I use data[0] it calls the function 'date();' which displays me the current date. The function 'Schicht();' should be called on the placeholders seen above.

EDIT:

maybe I should post some more Information. So the array is put into an html table that i generate.

function tableCreate(){
        var header = ['Datum','VoMi +D','Vo + NaMi','NaMi I','NaMi Dispo','Abend I/a','Abend I/b','Abend II','Abend III','Abend IV','Abend V','Abend Dispo'];
        var data = ['Datum','Schicht','Schicht','Schicht','Dispo','Schicht','Schicht','Schicht','Schicht','Schicht','Schicht','Dispo'];

        var html = '<table><thead><tr>';
        for (var x=0; x < 12; ++x){
            html += '<th>' + header[x] + '</th>';
        }
        html += '</tr></thead><tbody>';
        for (var i = 0; i < 28; ++i) {
            html += '<tr>';
            for (var j = 0; j < 12; ++j) {
                //var func = windows[data[j]];
                html += '<td>' + [data[j] + '</td>';
            }
            html += "</tr>";
        }
        html += '</tbody></table>';

        $(html).appendTo('#tabs1-schicht');
    }

And the date function for example that I try to call is:

var date = function date(){
        var today = new Date();
        var dd = today.getDate();
        var mm = today.getMonth()+1; //January is 0!
        var yyyy = today.getFullYear();

        if(dd<10) {
            dd='0'+dd
        } 
        if(mm<10) {
            mm='0'+mm
        } 

        today = dd+'.'+mm+'.'+yyyy;
        console.write(today);
    }
CuChulla1nn
  • 9
  • 1
  • 7

2 Answers2

0

------- ANSWER AFTER YOUR EDIT --------

Your function date is not returning any value. Try this version:

    var date = function (){
        var today = new Date(),
            dd = today.getDate(),
            mm = today.getMonth()+1, //January is 0!
            yyyy = today.getFullYear();

    if(dd<10) {
        dd='0'+dd
    } 
    if(mm<10) {
        mm='0'+mm
    } 

    today = dd+'.'+mm+'.'+yyyy;
    if(console.log) console.log(today); 
    return today;
}

Now this should work

function test(){
    var data = [];
    data.push(date);

    var html = "<table><tr><td>" + data[0]() + "</td></tr></table>";
    if(console.log)console.log(html); // in chorme
}
test();
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
sabotero
  • 4,265
  • 3
  • 28
  • 43
  • I tried out your version. But I get no result. When I execute it, there is only a blanc screen. – CuChulla1nn Dec 17 '14 at 07:49
  • console.log works in chrome. Change `console.log` for `alert('hello world');` or something like that. Are you colling the `test` function – sabotero Dec 17 '14 at 08:51
0

I think your question is related with How to execute a JavaScript function when I have its name as a string. You can use it like

var Datum = function(args){
bla bla..
}
var data = ['Datum','Schicht','Schicht','Schicht','Dispo','Schicht','Schicht','Schicht','Schicht','Schicht','Schicht','Dispo'];

var func = window[data[0]];
func(arguments);

The other way is

var Datum = function(args){
bla bla...
}
var data = ['Datum','Schicht','Schicht','Schicht','Dispo','Schicht','Schicht','Schicht','Schicht','Schicht','Schicht','Dispo'];

eval(data[0] + "(arguments)");

or you can directly add functions to array like:

var Datum = function(){};
var Schicht = function(){};

var Data = [Datum, Schicht ...];
Data[0]();
Community
  • 1
  • 1
caslaner
  • 403
  • 3
  • 6