autocomplete

This commit is contained in:
sindhu
2024-05-08 13:36:37 +07:00
parent 48b4de265f
commit fb91440c81
7 changed files with 777 additions and 1 deletions

View File

@@ -1,6 +1,9 @@
package services
import (
"fmt"
"strconv"
"github.com/emarifer/go-templ-project-structure/db"
)
@@ -41,6 +44,10 @@ type ServicesMasterMenuUserGroup struct {
MasterMenuUserGroupStore db.MasterMenuUserGroupStore
}
type AutoComplete struct {
Name string `json:"name"`
}
func (su *ServicesMasterMenuUserGroup) GetMasterMenus() ([]MasterMenu, error) {
// dummyBreadcrumb := []Breadcrumb{
@@ -543,6 +550,197 @@ func (su *ServicesMasterMenuUserGroup) GetMasterMenus() ([]MasterMenu, error) {
return dummyMenu, nil
}
func contains(s, substr string) bool {
return s != "" && substr != "" && len(s) >= len(substr) && s[:len(substr)] == substr
}
// example autocomplete
func (su *ServicesMasterMenuUserGroup) GetAutoComplete(param string, pageOfParam string, limitOfStr string) (string, string, string, []AutoComplete, error) {
// fmt.Println("ini param : ", param)
dummyAutoComplete := []AutoComplete{
{
Name: "Google",
},
{
Name: "Google 1",
},
{
Name: "Google 2",
},
{
Name: "Google 3",
},
{
Name: "Google 4",
},
{
Name: "Google 5",
},
{
Name: "Google 6",
},
{
Name: "Gojek",
},
{
Name: "Mozilla Firefox",
},
{
Name: "Apple",
},
{
Name: "Microsoft",
},
{
Name: "Facebook",
},
{
Name: "Amazon",
},
{
Name: "Twitter",
},
{
Name: "Instagram",
},
{
Name: "Snapchat",
},
{
Name: "LinkedIn",
},
{
Name: "Netflix",
},
{
Name: "YouTube",
},
{
Name: "Reddit",
},
{
Name: "WhatsApp",
},
{
Name: "Pinterest",
},
{
Name: "TikTok",
},
{
Name: "Spotify",
},
{
Name: "Zoom",
},
}
var textFound string
var results []AutoComplete
// var currentRes []AutoComplete
// var oldResults []AutoComplete
// var limit int = 3
var count int = 0
var foundCount int = 0
var totalData int = 0
var startIndex int = 0
var endIndex int = 3
if len(dummyAutoComplete) > 0 {
totalData = len(dummyAutoComplete)
}
// pageOf, err := strconv.Atoi(pageOfParam)
// if err != nil {
// return "", "", "", nil, err
// }
limit, err := strconv.Atoi(limitOfStr)
if err != nil {
return "", "", "", nil, err
}
// find data search
if pageOfParam == "1" {
count = 0
// for _, item := range dummyAutoComplete {
// if contains(item.Name, param) {
// results = append(results, item)
// count++
// if count == limit {
// break
// }
// }
// }
for _, item := range dummyAutoComplete {
if contains(item.Name, param) {
// Check if the item is already in results
found := false
for _, res := range results {
if res.Name == item.Name {
found = true
break
}
}
// If not found, append it
if !found {
results = append(results, item)
count++
if count == limit {
break
}
}
}
}
} else {
// ketika klik load more
// count = 0
for _, item := range dummyAutoComplete {
if contains(item.Name, param) {
results = append(results, item)
count++
if count == limit {
break
}
}
}
startIndex = 0
endIndex = limit
if startIndex >= len(dummyAutoComplete) {
return "", "max", "", nil, nil
}
if endIndex > len(dummyAutoComplete) {
endIndex = len(dummyAutoComplete)
}
for _, item := range dummyAutoComplete[startIndex:endIndex] {
found := false
for _, res := range results {
if res.Name == item.Name {
found = true
break
}
}
// If not found, append it
if !found {
results = append(results, item)
}
}
}
foundCount = len(results)
totalCount := len(dummyAutoComplete)
textFound = fmt.Sprintf("Ditemukan %d dari %d", foundCount, totalCount)
return strconv.Itoa(totalData), strconv.Itoa(foundCount), textFound, results, nil
}
// func (su *ServicesMasterMenuUserGroup) GetAllMasterMenus() ([]MasterMenu, error) {
// query := `SELECT id, MasterMenuname, email, created_at FROM MasterMenus ORDER BY created_at DESC`