Skip to content

Sorting results in Select and SelectRepeater #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"html"
"html/template"
"log"
"sort"
"strings"

"github.com/ponzu-cms/ponzu/management/editor"
Expand Down Expand Up @@ -83,15 +84,34 @@ func SelectRepeater(fieldName string, p interface{}, attrs map[string]string, co

opts = append(opts, cta, reset)

// order the options by value
orderedOptions := make([]string, 0, len(options))
for k, v := range options {
optAttrs := map[string]string{"value": k}
if k == val {
orderedOptions = append(orderedOptions, fmt.Sprintf("%s__ponzu__order%s", v, k))
}
sort.Strings(orderedOptions)

// construct editor elements array from option items
for _, v := range orderedOptions {
// get key value pair from ordered string
vkPair := strings.Split(v, "__ponzu__order")
if len(vkPair) < 2 {
continue
}
key := vkPair[1]
value := vkPair[0]

// check for selected item
optAttrs := map[string]string{"value": key}
if key == val {
optAttrs["selected"] = "true"
}

// construct editor elements item
opt := &editor.Element{
TagName: "option",
Attrs: optAttrs,
Data: v,
Data: value,
ViewBuf: &bytes.Buffer{},
}

Expand Down