1

I am trying to make a matrix table by combining values from array. First array holds information about user for specific day. Second array holds list of users, third array holds days in month.

First I get data from api.

var odgovor = JSON.parse(http.responseText);
dataSetMoj = odgovor.urvPoDanuZaMjesec;
dataSetMojUposlenik = odgovor.uposlenici;

Get days for month

var mjesecImaDana = getDaysInMonth(1,2018);

var daniMjeseca = [];
for (i = 1; i <= mjesecImaDana; i++) {
daniMjeseca.push([i]);
}

Then I create table header

var k = '<thead>';
k +='<tr><th style="background-color: #f8f7f7" >' + rb + '</th>';
k +='<th style="background-color: #f8f7f7" >' + up + '</th>';

daniMjeseca.forEach(function(daysInmonth){
    k += '<th style="background-color: #f8f7f7"> '+ daysInmonth +'</th>';
});
k += '</tr></thead>';

Table header display correct information, as could be seen in img.

Array rezultati holds information about each user on specific day

var rezultati = [];
for(i = 0;i < dataSetMoj.length; i++){ 
  rezultati.push(dataSetMoj[i].ime+'-'+dataSetMoj[i].datum+'-'+dataSetMoj[i].urv);
}

Then I create table body, In table body I should match user with date.

k += '<tbody>'

for(i = 0;i < dataSetMojUposlenik.length; i++){
    k+='<tr>';
    k+='<td> ' + a++  + '</td>';
    k+='<td> ' + dataSetMojUposlenik[i].ime + '</td>';
    daniMjeseca.forEach(function(daysInmonth){
       var dataSetVar = dataSetMojUposlenik[i].ime +'-'+daysInmonth;
        //here I should check if variable 'datSetVar' is set in array rezultati, if yes I should print dataSetMoj[i].urv from rezultati
        if(rezultati == dataSetVar ){
          k+='<td> ' + dataSetMoj[i].urv  + '</td>'; // It does not work, but I have no idea. I could not find solution any hint would help me. 
        }
      else{
        k+='<td> ' + c + '</td>';
      }

    });


k+='</tr>';

}

k+='</tbody>';

document.getElementById('tableData').innerHTML = k;

table

Array rezultati holds three element dataSetVar two. I am not able to make comparison got wrong answers.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mu Lem
  • 75
  • 9
  • What's the question? – Th0rndike Feb 05 '18 at 09:06
  • Maybe it's typo mistake, you set `datSetVar` but you compare with `dataSetVar` ? – DarknessZX Feb 05 '18 at 09:06
  • @Th0rndike, array rezultati holds three elements. I am comparing rezultati with dataSetVar that holds 2. Not sure if that is possible dont get results. I dont know if this is correct way to solve the trouble. Acctulay I have array and variable. In php I could use isset function but in js idk – Mu Lem Feb 05 '18 at 09:15
  • @DarknessZX yes you are right but it does not solve problem. – Mu Lem Feb 05 '18 at 09:18
  • I think you should `console.log` to see value of `rezultati` and `dataSetVar`. In case they are both Array, you can look into this https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript – DarknessZX Feb 05 '18 at 09:25

0 Answers0