Browse Source

Multiple select for categories input

Vova Tkach 6 years ago
parent
commit
ccb478795e
4 changed files with 24 additions and 3 deletions
  1. 3 2
      modules/module_blog.go
  2. 4 1
      modules/module_blog_categories.go
  3. 9 0
      utils/utils.go
  4. 8 0
      utils/utils_test.go

+ 3 - 2
modules/module_blog.go

@@ -282,7 +282,8 @@ func (this *Modules) RegisterModule_Blog() *Module {
 								<div class="col-md-9">
 								<div class="col-md-9">
 									<div>
 									<div>
 										<select class="form-control" id="lbl_cats" name="cats[]" multiple>
 										<select class="form-control" id="lbl_cats" name="cats[]" multiple>
-											` + this.blog_GetCategorySelectOptions(wrap, 0, 0) + `
+											<!--<option value=""></option>-->
+											` + this.blog_GetCategorySelectOptions(wrap, 0, 0, []int{}) + `
 										</select>
 										</select>
 									</div>
 									</div>
 								</div>
 								</div>
@@ -410,7 +411,7 @@ func (this *Modules) RegisterModule_Blog() *Module {
 									<div>
 									<div>
 										<select class="form-control" id="lbl_parent" name="parent">
 										<select class="form-control" id="lbl_parent" name="parent">
 											<option value="0">&mdash;</option>
 											<option value="0">&mdash;</option>
-											` + this.blog_GetCategorySelectOptions(wrap, data.A_id, parentId) + `
+											` + this.blog_GetCategorySelectOptions(wrap, data.A_id, parentId, []int{}) + `
 										</select>
 										</select>
 									</div>
 									</div>
 								</div>
 								</div>

+ 4 - 1
modules/module_blog_categories.go

@@ -8,7 +8,7 @@ import (
 	"golang-fave/utils"
 	"golang-fave/utils"
 )
 )
 
 
-func (this *Modules) blog_GetCategorySelectOptions(wrap *wrapper.Wrapper, id int, parentId int) string {
+func (this *Modules) blog_GetCategorySelectOptions(wrap *wrapper.Wrapper, id int, parentId int, selids []int) string {
 	result := ``
 	result := ``
 	rows, err := wrap.DB.Query(
 	rows, err := wrap.DB.Query(
 		`SELECT
 		`SELECT
@@ -49,6 +49,9 @@ func (this *Modules) blog_GetCategorySelectOptions(wrap *wrapper.Wrapper, id int
 				if string(values[0]) == parentIdStr {
 				if string(values[0]) == parentIdStr {
 					selected = " selected"
 					selected = " selected"
 				}
 				}
+				if len(selids) > 0 && utils.InArrayInt(selids, utils.StrToInt(string(values[0]))) {
+					selected = " selected"
+				}
 				depth := utils.StrToInt(string(values[4])) - 1
 				depth := utils.StrToInt(string(values[4])) - 1
 				if depth < 0 {
 				if depth < 0 {
 					depth = 0
 					depth = 0

+ 9 - 0
utils/utils.go

@@ -301,3 +301,12 @@ func JavaScriptVarValue(str string) string {
 		-1,
 		-1,
 	)
 	)
 }
 }
+
+func InArrayInt(slice []int, value int) bool {
+	for _, item := range slice {
+		if item == value {
+			return true
+		}
+	}
+	return false
+}

+ 8 - 0
utils/utils_test.go

@@ -209,3 +209,11 @@ func TestJavaScriptVarValue(t *testing.T) {
 	Expect(t, JavaScriptVarValue(`It's "string"`), "It&rsquo;s &rdquo;string&rdquo;")
 	Expect(t, JavaScriptVarValue(`It's "string"`), "It&rsquo;s &rdquo;string&rdquo;")
 	Expect(t, JavaScriptVarValue(`It is string`), "It is string")
 	Expect(t, JavaScriptVarValue(`It is string`), "It is string")
 }
 }
+
+func TestInArrayInt(t *testing.T) {
+	slice := []int{1, 3, 5, 9, 0}
+	Expect(t, InArrayInt(slice, 1), true)
+	Expect(t, InArrayInt(slice, 9), true)
+	Expect(t, InArrayInt(slice, 2), false)
+	Expect(t, InArrayInt(slice, 8), false)
+}