Expected behavior:
Clicking table row should display only the selected image in a div on the right.
Current State:
Clicking each table row displays the selected image without toggling the image being selected. Another error is that clicking the previous table row again creates another copy of the image.
Plunker
http://plnkr.co/edit/26VanqdKqltbD6Bdclg9?p=preview
media-image.component.html
<div id="media-content-container">
<div id="media-content-wrapper">
<div id="upload-selected-images">
<input
type="file"
id="files"
name="files[]"
(change)="handleFileSelect($event)"
multiple />
</div>
<table class="table" id="list">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Size</th>
</tr>
</thead>
<tbody>
<tr
*ngFor="let image of images, let i = index "
(click)="displayImage(files[i])">
<td>{{image.fileName}}</td>
<td>{{image.fileType}}</td>
<td>{{image.fileSize}}</td>
</tr>
</tbody>
</table>
</div>
<div id="selected-image">
</div>
</div>
media-image.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-media-image',
templateUrl: './media-image.component.html',
styleUrls: ['./media-image.component.css']
})
export class MediaImageComponent implements OnInit {
images:any = [];
files:FileList;
ngOnInit(){
}
handleFileSelect(evt) {
this.files = evt.target.files; // FileList object
console.log("this.files:: ", this.files);
for (let i = 0, f; f = this.files[i]; i++) {
if (!f.type.match('image.*')) {
continue;
}
this.images.push({
fileName: encodeURI(f.name),
fileType: f.type || 'N/A',
fileSize: `${f.size} bytes`
});
console.log("images:: ", this.images);
}
}
displayImage( f ){
let reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e){
// Render thumbnail.
let div = document.createElement('div');
// div.className = 'ad-images';
// div.style.cssText = 'display:none';
div.innerHTML = ['<img style="height:80px;" src="', e.target.result,
'" title="', encodeURI(theFile.name), '"/>'].join('');
document.getElementById('selected-image').insertBefore(div, null);
}
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
media-image.component.css
#media-content-container{
display: flex;
justify-content: space-between;
align-items: center;
margin: 2em 0;
}