2016-06-25 25 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"); 
     } 
} 
相关问题