Browse Source

Categories by parent in front-end

Vova Tkach 5 years ago
parent
commit
ac10e81f77

+ 4 - 3
assets/template/header_html_file.go

@@ -116,14 +116,15 @@ var VarHeaderHtmlFile = []byte(`<!doctype html>
 						</button>
 						<div class="collapse navbar-collapse" id="navbarSupportedContent">
 							<ul class="navbar-nav mr-auto">
-							{{range $.Data.Shop.Categories 1}}
+							{{range $.Data.Shop.Categories 0 1}}
 								<li class="nav-item dropdown">
 									<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{.Name}}</a>
 									<div class="dropdown-menu" aria-labelledby="navbarDropdown">
 										<a class="dropdown-item" href="{{.Permalink}}">All products</a>
 										<div class="dropdown-divider"></div>
-										<a class="dropdown-item" href="#">Sub category 1</a>
-										<a class="dropdown-item" href="#">Sub category 2</a>
+										{{range $index, $subcat := $.Data.Shop.Categories .Id 1}}
+											<a class="dropdown-item" href="{{$subcat.Permalink}}">{{$subcat.Name}}</a>
+										{{end}}
 									</div>
 								</li>
 							{{end}}

+ 1 - 1
assets/template/sidebar_left_html_file.go

@@ -4,7 +4,7 @@ var VarSidebarLeftHtmlFile = []byte(`<div class="card mb-4">
 	<h5 class="card-header">Shop categories</h5>
 	<div class="card-body">
 		<ul class="m-0 p-0 pl-4">
-			{{range $.Data.Shop.Categories 0}}
+			{{range $.Data.Shop.Categories 0 1}}
 				<li class="{{if and $.Data.Shop.Category (eq $.Data.Shop.Category.Id .Id)}}active{{end}}">
 					<a href="{{.Permalink}}">{{.Name}}</a>
 				</li>

+ 37 - 18
engine/fetdata/shop.go

@@ -2,6 +2,7 @@ package fetdata
 
 import (
 	"math"
+	"sort"
 	"strings"
 
 	"golang-fave/engine/wrapper"
@@ -29,7 +30,7 @@ type Shop struct {
 	paginationPrev   *ShopPagination
 	paginationNext   *ShopPagination
 
-	bufferCats map[string][]*ShopCategory
+	bufferCats map[int]*utils.MySql_shop_category
 }
 
 func (this *Shop) load() *Shop {
@@ -554,20 +555,9 @@ func (this *Shop) PaginationNext() *ShopPagination {
 	return this.paginationNext
 }
 
-func (this *Shop) Categories(mlvl int) []*ShopCategory {
-	if this == nil {
-		return []*ShopCategory{}
-	}
+func (this *Shop) Categories(parent, depth int) []*ShopCategory {
 	if this.bufferCats == nil {
-		this.bufferCats = map[string][]*ShopCategory{}
-	}
-	key := ""
-	where := ``
-	if mlvl > 0 {
-		where += `AND depth.depth <= ` + utils.IntToStr(mlvl)
-	}
-	if _, ok := this.bufferCats[key]; !ok {
-		var cats []*ShopCategory
+		this.bufferCats = map[int]*utils.MySql_shop_category{}
 		if rows, err := this.wrap.DB.Query(`
 			SELECT
 				main.id,
@@ -614,7 +604,6 @@ func (this *Shop) Categories(mlvl int) []*ShopCategory {
 			WHERE
 				main.id > 1 AND
 				main.id <> main.parent_id
-				` + where + `
 			GROUP BY
 				main.id
 			ORDER BY
@@ -634,11 +623,41 @@ func (this *Shop) Categories(mlvl int) []*ShopCategory {
 					&row.A_depth,
 					&row.A_parent,
 				); err == nil {
-					cats = append(cats, &ShopCategory{object: &row})
+					this.bufferCats[row.A_id] = &row
+				}
+			}
+		}
+	}
+
+	depth_tmp := 0
+	result := []*ShopCategory{}
+
+	for _, cat := range this.bufferCats {
+		if parent <= 1 {
+			if depth <= 0 {
+				result = append(result, &ShopCategory{wrap: this.wrap, object: cat})
+			} else {
+				if cat.A_depth <= depth {
+					result = append(result, &ShopCategory{wrap: this.wrap, object: cat})
+				}
+			}
+		} else {
+			if cat.A_parent == parent {
+				if depth_tmp == 0 {
+					depth_tmp = cat.A_depth
+				}
+				if depth <= 0 {
+					result = append(result, &ShopCategory{wrap: this.wrap, object: cat})
+				} else {
+					if (cat.A_depth - depth_tmp + 1) <= depth {
+						result = append(result, &ShopCategory{wrap: this.wrap, object: cat})
+					}
 				}
 			}
 		}
-		this.bufferCats[key] = cats
 	}
-	return this.bufferCats[key]
+
+	sort.Slice(result, func(i, j int) bool { return result[i].Left() < result[j].Left() })
+
+	return result
 }

+ 4 - 3
hosts/localhost/template/header.html

@@ -114,14 +114,15 @@
 						</button>
 						<div class="collapse navbar-collapse" id="navbarSupportedContent">
 							<ul class="navbar-nav mr-auto">
-							{{range $.Data.Shop.Categories 1}}
+							{{range $.Data.Shop.Categories 0 1}}
 								<li class="nav-item dropdown">
 									<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{.Name}}</a>
 									<div class="dropdown-menu" aria-labelledby="navbarDropdown">
 										<a class="dropdown-item" href="{{.Permalink}}">All products</a>
 										<div class="dropdown-divider"></div>
-										<a class="dropdown-item" href="#">Sub category 1</a>
-										<a class="dropdown-item" href="#">Sub category 2</a>
+										{{range $index, $subcat := $.Data.Shop.Categories .Id 1}}
+											<a class="dropdown-item" href="{{$subcat.Permalink}}">{{$subcat.Name}}</a>
+										{{end}}
 									</div>
 								</li>
 							{{end}}

+ 17 - 17
hosts/localhost/template/sidebar-left.html

@@ -1,18 +1,18 @@
-<div class="card mb-4">
-	<h5 class="card-header">Shop categories</h5>
-	<div class="card-body">
-		<ul class="m-0 p-0 pl-4">
-			{{range $.Data.Shop.Categories 0}}
-				<li class="{{if and $.Data.Shop.Category (eq $.Data.Shop.Category.Id .Id)}}active{{end}}">
-					<a href="{{.Permalink}}">{{.Name}}</a>
-				</li>
-			{{end}}
-		</ul>
-	</div>
-</div>
-<!-- <div class="card mb-4">
-	<h5 class="card-header">Shop filter</h5>
-	<div class="card-body">
-		Filter
-	</div>
+<div class="card mb-4">
+	<h5 class="card-header">Shop categories</h5>
+	<div class="card-body">
+		<ul class="m-0 p-0 pl-4">
+			{{range $.Data.Shop.Categories 0 1}}
+				<li class="{{if and $.Data.Shop.Category (eq $.Data.Shop.Category.Id .Id)}}active{{end}}">
+					<a href="{{.Permalink}}">{{.Name}}</a>
+				</li>
+			{{end}}
+		</ul>
+	</div>
+</div>
+<!-- <div class="card mb-4">
+	<h5 class="card-header">Shop filter</h5>
+	<div class="card-body">
+		Filter
+	</div>
 </div> -->