내 모델에서 가져 오기 (모든 레코드와 필드가 있음)를 통해 가져온 데이터에서 제한된 필드가있는 JSON을 만드는 방법은 "getSegmentGroup"입니다.모델에서 JSON을 전달하여 JSON을 백본 js의 테이블로 표시하고 표시하는 방법은 무엇입니까?
내 모델에있어서 다음
getSegmentGroups: function(customerIds, column1Name, column2Name){
var segmentTableJson = [];
this.getSegment = new CustomerCollection();
this.getSegment.fetch({
success: function(response) {
var fetchData = response.toJSON();
for(var i in customerIds){
customerId = customerIds[i];
for(var j in fetchData){
if(fetchData[j].custId == customerId){
column_1 = fetchData[j][column1Name];
column_2 = fetchData[j][column2Name];
}else{
continue;
}
}
var item = {
"customerId" : customerId,
"column1" : column_1,
"column2" : column_2
};
segmentTableJson.push(item);
}
console.log(JSON.stringify(segmentTableJson));
var jsonTableFormat = JSON.stringify(segmentTableJson);
}
});
}
입력 customerIds 같다 JSON을 수득 다음
[7, 8]
추천 ID 배열이된다
[{"customerId":7,"column1":"[email protected]","column2":24},{"customerId":8,"column1":"raju","column2":34}]
이제 필요 json "jsonTableFormat"을 모델에서 전달하여 테이블에 json을 표시합니다.
어떻게 내가 백본에 새로 온 사람 때문에 내가 그것을 알아낼 수있는 테이블을
var segment_table_view = Backbone.View.extend({
el: $('#segment'),
initialize: function() {
this.model.bind("add", this.render, this);
},
//this creates new rows in the table for each model in the collection
render: function() {
_.each(this.model.models, function(data) {
this.$el.append(new segment_view({
model: data
}).render().el);
}, this);
return this;
}
});
//a (row) view to render each employee
var segment_view = Backbone.View.extend({
tagName: "tr",
template: _.template($("#segment-table").html()),
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
를 표시 할 segment_table_view에 전달 않습니다.
나는 json 응답에서 약간의 수정을하고있다. 왜냐하면 나는 (customerId 배열을 기반으로하여) 하나의 json을 나눠서 테이블 수로 표시하기를 원하기 때문이다. –
방금 필터링하는 경우 REST API를 필터링하여 필요로하는 2 가지 모델 만 얻을 수 있도록하는 것이 가장 좋지만 전체 컬렉션이 별도의 테이블에 사용 된 것으로 보입니다. 이 경우 밑줄을 사용하여 템플릿에 논리를 만들 수 있습니다. 좋은 연습을 위해서 컬렉션 자체를 편집하지 않아도됩니다. – Xerri
그래, 내가 백본 js에 처음 왔으니 너 라이트 야. 나는 이걸 시험해보고있어. 그래서 내가 화면에 출력을 볼 수 있습니다. –