아래의 ajax 호출에서 얻은 배열을이 knockout 매핑에 매핑했습니다.foreach 루프 녹아웃 제한
function InvoiceViewModel(data) {
var self = this;
self.survey = data;
}
아약스 전화
$.ajax({
url: 'http://localhost:43043/api/damage',
type: 'GET',
headers: { 'Accept': 'application/json' },
data: {
orderNumber: num,
category: cat
},
success:
function (data) {
var usingRoutData = document.URL;
ko.applyBindings(new InvoiceViewModel(data));
},
error: function() {
alert('failure');
}
});
내 배열
var test = {
Name: Blah,
Attributes: [
{Name: Test, Type: Photo, Year:1988},
{Name: Test, Type: Photo, Year:1988},
{Name: Test, Type: Photo, Year:1988}
]
};
내가 바인딩하고 어떻게 내 데이터
<div id="invoiceBodyWrapper">
<div data-bind="template: { name: 'invoice-template', foreach: surveys }">
</div>
<div class="invoiceWrapper">
</div>
<div id="completePictureWrapper" data-bind="template: { name: 'photo-template', foreach: new Array(Attributes) }"></div>
</div>
</script>
<script type="text/html" id="photo-template">
<!-- ko if: classification === 'photo' -->
<div id="pictureWrappers">
<img class="img" data-bind="attr: { src: 'http://myimagepath/download/full/' + $index()+1 }" />
</div>
<!-- /ko -->
</script>
<script src="~/Scripts/DamageInvoiceCreation.js"></script>
내 속성 foreach 루프가 3 개의 속성 중 2 개만 표시하도록하는 방법이 필요합니다. 나는 이것을 수행하는 방법에 대해서만 몇 가지 발견하고 그들은 매우 지나치게 복잡하게 보입니다. 나는 녹아웃에서 이것을하는 간단한 방법이 없다고 상상할 수 없다.
게시물에는 루프가 없습니다. 어떻게 당신의 견해에 구속력이 있습니까? – Romoku
[Knockout.js - 동적 열이지만 각 행에 대해 최대 5 개로 제한] (http://stackoverflow.com/q/7669946/580951) – Romoku
@Romoku 그 질문에 대해 배우려면 많이해야하지만 확실히 적용 가능한 코드. 좋은 링크! –