我有一个本地JSON文件,其中包含“课程类别”和“标题”。每个类别都有多个标题,每个标题对应一个类别。
我创建了three separate divs:“热门课程”,“新添加的课程”,最后是“热门课程”,并且div由“类别”填充。单击类别将打开一个模态,该模式将再次显示类别名称和正确的类别描述。应该显示与该类别相关的所有标题,例如单击类别“动物”应显示“龙猫”,“ A”,“跳蚤”等。
我要这样做,以便仅单击类别 会显示与该类别关联的标题。我在模态中看到标题-但是每个类别中的I'm only seeing one Title。根据标题的顺序,我认为它们来自“所有课程”类别。我不能肯定地说是这种方式,但是当我单击“全部”下的不是的类别时,我会看到同一件事。
var testjson = {
"d": {
"results": [{
"Title": "Aardvark",
"Category": "Animals",
"Description": "a-desc",
"TopTrainingCourse": false,
"ID": 1,
"Modified": "2019-03-05T20:13:46Z",
"Created": "2019-03-05T20:13:36Z"
}, {
"Title": "Red Panda",
"Category": "Animals",
"Description": "a-desc",
"TopTrainingCourse": true,
"ID": 10,
"Modified": "2019-03-06T21:08:25Z",
"Created": "2019-03-06T21:08:25Z"
}, {
"Title": "Tennis",
"Category": "Sports",
"Description": "s-desc",
"TopTrainingCourse": true,
"ID": 11,
"Modified": "2019-03-06T21:08:35Z",
"Created": "2019-03-06T21:08:35Z"
}]
}
}
///// From topCourses.js /////
import testjson from './test.json';
export default class {
constructor() {
}
loadTopCourses() {
let topCrs = testjson.d.results
.filter(x => x.TopTrainingCourse === true)
.filter((el, idx, self) => { // no duplicates
return (idx === self.map(el => el.Category).indexOf(el.Category))
})
.map(x => {
return {
"Category": x.Category,
"Description": x.Description,
"Title": x.Title
}
});
let curIndex = 0;
$.each(topCrs, function(idx, val) {
curIndex++; // this line must be here---if it's moved down the 1st col doesn't show
let targetDiv = $("div.top-training-div > div[col='" + curIndex + "']");
let modalTrigger = $('<div />', {
'class': 'categoryName',
'data-category': val.Category,
'data-target': '#modal-id',
'data-toggle': 'modal',
'text': val.Category
});
targetDiv.append(modalTrigger);
if(curIndex == 4) {
curIndex = 0;
}
})
$('.categoryName').click(function(val) {
let cat = $(this).data('category');
$('.modal-title').text(cat);
// console.log($(this).data('category'));
$(".category-desc").empty();
var noDupeDescs = topCrs.filter(function(val) {
return val.Category == this;
}, cat)
.map(function(val) {
return val.Description;
});
$.each(noDupeDescs, function(idx, val) {
$(".category-desc").append(val + "<br />")
})
///
$(".training-titles").empty();
let titles = topCrs;
$.each(titles, function(idx, val) { // ----- thought something like this could help
for (var i = 0; i = titles.length; i++) {
if $(titles.contains(val.Title)
$(".training-titles").append("<li>" + val.Title + "</li>")
}
}
});
} // ------------------ loadTopCourses
}
<div class="modal fade" id="modal-id" role="dialog" >
<div class="modal-backdrop">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">CLOSE</span> <!-- × -->
</button>
</div>
<div class="modal-body">
<div class="category-desc"></div>
<ul class="training-titles"></ul>
</div>
</div>
</div>
</div> <!-- modal-backdrop -->
</div> <!-- modal fade -->
答案 0 :(得分:9)
看来您的错误是您迭代了topCrs来显示标题,但是topCrs是一个很小的数组,已经过滤并仅保留每个类别的一个标题,因为在此您始终将只能看到每个类别的一个标题。
您需要做的是再次遍历 testjson.d.results 中的所有标题,并按单击的类别进行过滤。
var titles = testjson.d.results.filter(x => x.Category === cat);
然后迭代按这样的类别过滤的标题
$.each(titles, function(idx, val) {
$(".training-titles").append("<li>" + val.Title + "</li>")
}