set selected tags in edititem
This commit is contained in:
@@ -432,15 +432,45 @@ func (rc *shopItemController) AddItemsHandler(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "batchupload.html", data)
|
||||
}
|
||||
|
||||
func (rc *shopItemController) EditItemView(c *gin.Context) {
|
||||
shopItem, err := repositories.ShopItems.GetById(c.Param("id"))
|
||||
tags, err := repositories.Tags.GetAll()
|
||||
func GetCheckedTags(item models.ShopItem) ([]models.CheckedTag, error) {
|
||||
allTags, err := repositories.Tags.GetAll()
|
||||
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "edititem.html", gin.H{"error": err})
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Println(shopItem)
|
||||
var tags []models.CheckedTag
|
||||
for _, tag := range allTags {
|
||||
tmpTag := models.CheckedTag{
|
||||
Tag: tag,
|
||||
Checked: "",
|
||||
}
|
||||
|
||||
for _, itemTag := range item.Tags {
|
||||
if itemTag.Name == tmpTag.Name {
|
||||
tmpTag.Checked = "checked"
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
tags = append(tags, tmpTag)
|
||||
}
|
||||
|
||||
return tags, nil
|
||||
}
|
||||
|
||||
func (rc *shopItemController) EditItemView(c *gin.Context) {
|
||||
shopItem, err := repositories.ShopItems.GetById(c.Param("id"))
|
||||
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": err})
|
||||
}
|
||||
|
||||
tags, err := GetCheckedTags(shopItem)
|
||||
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": err})
|
||||
}
|
||||
|
||||
data := CreateSessionData(c, gin.H{
|
||||
"error": "",
|
||||
@@ -489,7 +519,7 @@ func (rc *shopItemController) EditItemHandler(c *gin.Context) {
|
||||
|
||||
newShopItem.PrintMode = shopItem.PrintMode
|
||||
|
||||
tags, err := repositories.Tags.GetAll()
|
||||
tags, err := GetCheckedTags(newShopItem)
|
||||
if err != nil {
|
||||
c.HTML(http.StatusBadRequest, "edititem.html", gin.H{"error": err})
|
||||
return
|
||||
@@ -500,6 +530,7 @@ func (rc *shopItemController) EditItemHandler(c *gin.Context) {
|
||||
data := CreateSessionData(c, gin.H{
|
||||
"error": err,
|
||||
"success": "",
|
||||
"shopItem": newShopItem,
|
||||
"tags": tags,
|
||||
})
|
||||
|
||||
@@ -510,6 +541,7 @@ func (rc *shopItemController) EditItemHandler(c *gin.Context) {
|
||||
data := CreateSessionData(c, gin.H{
|
||||
"error": "",
|
||||
"success": fmt.Sprintf("Item '%s' Updated", newShopItem.Name),
|
||||
"shopItem": newShopItem,
|
||||
"tags": tags,
|
||||
})
|
||||
|
||||
|
||||
@@ -14,6 +14,11 @@ type Tag struct {
|
||||
ShopItems []ShopItem `gorm:"many2many:item_tags;"`
|
||||
}
|
||||
|
||||
type CheckedTag struct {
|
||||
Tag
|
||||
Checked string
|
||||
}
|
||||
|
||||
func NewTag(ctx *gin.Context) (Tag, error) {
|
||||
colors := []string{
|
||||
"red",
|
||||
|
||||
@@ -37,7 +37,7 @@
|
||||
<div class="space-y-2">
|
||||
{{ range .data.tags }}
|
||||
<label class="flex text-sm/6 items-center">
|
||||
<input type="checkbox" class="form-checkbox h-4 w-4 text-gray-900" value="{{ .ID }}" name="tags[]">
|
||||
<input type="checkbox" {{ .Checked }} class="form-checkbox h-4 w-4 text-gray-900" value="{{ .ID }}" name="tags[]">
|
||||
<span class="ml-2 text-sm/6 text-gray-900">{{ .Name }}</span>
|
||||
</label>
|
||||
{{ end }}
|
||||
|
||||
Reference in New Issue
Block a user