2016-06-25 26 views
0

圖像可以具有JSON格式的[1,2,3,4]狀態(佔位符,處理,接受,拒絕)。我正在計算一個函數來檢查屬性並選擇正確的圖像放在Mustache HTML中。當追加模板時在json屬性上更改圖像

我試過如下:

for (let i=0;i<101;i++) { 
     var output = Mustache.render(template, view[i]); 
     $('#entryTable tbody').append(output); 

     if (view[i].status == 2) { 
      this.getElementById('picture1').src= 'img/analyzing.png'; 
     } else if (view[i].status == 3) { 
      this.getElementById('picture1').src= 'img/irrelevant.png'; 
      console.log(this) 
     } else if (view [i].status == 4) { 
      this.getElementById('picture1').src= 'img/relevant.png'; 
     } 

但「這」指的是文件,而不是圖像 - >它改變只有第一個圖像(所有條目,反覆)看來。

接下來,我想:(?)

for (let i=0;i<101;i++) { 
     var output = Mustache.render(template, view[i]); 
     $('#entryTable tbody').append(function() { 

//the default image in the template is state 1, hence it checks for state 2 and above 

     if (view[i].state == 2) { 
      this.getElementById('picture1').src= 'img/analyzing.png'; 
     } else if (view[i].state == 3) { 
      this.getElementById('picture1').src= 'img/irrelevant.png'; 
      console.log(this) 
     } else if (view [i].state == 4) { 
      this.getElementById('picture1').src= 'img/relevant.png'; 
     } 

     return output; 
}); 

這似乎是工作,但只追加一個條目。

我對'this'沒有經驗,我如何才能將它帶到正確的上下文/範圍以更改條目特定圖像?

回答

0

解決

if (view[i].state === 1) { 
      $("#entryTable tbody tr .status1").eq(i).attr("src", "img/analyzing.png"); 
     } else if (view[i].state === 3) { 
      $("#entryTable tbody tr .status1").eq(i).attr("src", "img/irrelevant.png"); 
     } else if (view[i].state === 4) { 
      $("#entryTable tbody tr .status1").eq(i).attr("src", "img/relevant.png"); 
     } 
} 
相關問題