【发布时间】:2017-11-15 23:44:36
【问题描述】:
预期行为:
单击表格行应仅在右侧 div 中显示所选图像。
当前状态:
单击每个表格行会显示选定的图像,而不切换正在选择的图像。另一个错误是再次单击上一个表格行会创建图像的另一个副本。
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;
}
【问题讨论】:
标签: javascript jquery html angular typescript