我正在尝试对类别进行计数并输出 JavaScript 或 jQuery 中每个parent_id = 0
的结果(如果它具有相应的功能) 。
我有:
categories_id - parent_id
10 - 3
19 - 2
17 - 1
13 - 3
12 - 3
15 - 3
4 - 1
8 - 1
16 - 1
9 - 1
6 - 1
5 - 1
11 - 3
18 - 2
7 - 1
20 - 2
14 - 3
22 - 4
1 - 0
2 - 0
3 - 0
21 - 0
数据来自数据库。
然后,如果我从列表中访问categories_id
,我需要添加一个表示category X
具有XX categories
的计数器。
这是我需要它的脚本:
var dbSize = 5 * 1024 * 1024; // 5MB
var db = openDatabase("Oscommerce", "1.0", "Oscommerce Database", dbSize);
var categories={};
var list_str = '';
db.transaction(function (tx) {
tx.executeSql('SELECT c.categories_id, c.categories_image, c.parent_id, cd.categories_name, COUNT(p2c.categories_id) AS total FROM categories c INNER JOIN categories_description cd ON c.categories_id = cd.categories_id LEFT JOIN products_to_categories p2c ON p2c.categories_id = c.categories_id GROUP BY c.categories_id, cd.categories_name ORDER BY c.sort_order, cd.categories_name', [], function (tx, results) {
list_str += '<ul data-role="listview" data-inset="true" data-theme="d" data-count-theme="a" data-split-theme="a">';
var len = results.rows.length, i;
for (i = 0; i < len; i++) {
var r = results.rows.item(i);
categories[r.categories_id] = r;
//console.log(categories);
console.log(r.categories_id+'-'+r.parent_id);
}
for(key in categories)
{
var parent = 0;
var value=categories[key];
catId = value['categories_id'];
catName = value['categories_name'];
catImage = value['categories_image'];
parentId = value['parent_id'];
total = value['total'];
if (parentId == parent)
{
if (total == 0){
list_str += '<li id="'+ catId +'"><a class="parentlink" parentid="'+ parentId +'" catid="'+ catId +'" catname="'+ catName +'"><h3>' + catName + '</h3><p>' + catImage + '</p><div class="ui-li-count">'+ total +' products</div></a><a class="parentlink" parentid="'+ parentId +'" catid="'+ catId +'" catname="'+ catName +'">somelink</a></li>';
}else{
list_str += '<li id="'+ catId +'"><a class="parentlink" parentid="'+ parentId +'" catid="'+ catId +'" catname="'+ catName +'"><h3>' + catName + '</h3><p>' + catImage + '</p><div class="ui-li-count">'+ total +' products</div></a><a href="#">somelink</a></li>';
}
}
}
list_str += '</ul>';
$('#parents').html(list_str).find('ul').listview();
});
});