manipulate-page-url-in-event-func.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package advanced_functions
  2. import (
  3. "github.com/qor5/docs/docsrc/examples/e00_basics"
  4. "github.com/qor5/docs/docsrc/generated"
  5. "github.com/qor5/docs/docsrc/utils"
  6. . "github.com/theplant/docgo"
  7. "github.com/theplant/docgo/ch"
  8. )
  9. var ManipulatePageURLInEventFunc = Doc(
  10. Markdown(`
  11. Encode page state into query strings in url is useful. because user can paste the link to another person,
  12. That can open the page to the exact state of the page being sent, Not the initial state of the page.
  13. For example:
  14. `),
  15. ch.Code(generated.MultiStatePageSample).Language("go"),
  16. utils.Demo("Manipulate Page URL In Event Func", e00_basics.MultiStatePagePath, "e00_basics/manipulate-page-url.go"),
  17. Markdown(`
  18. This page have several state that encoded in the url:
  19. - Page title have a default value, but if provided with a ~title~ query string, it will use that value
  20. - The edit panel can be open, or closed based on having the ~panel~ query string or not
  21. ~web.Location(url.Values{"panel": []string{"1"}}).MergeQuery(true)~ means it will do a push state request to current page, with panel query string panel=1.
  22. ~MergeQuery~ means that it will not touch other query strings like ~title=1~ we mentioned above.
  23. In ~update5~ event func, which is when you click the update button after open the panel, ~web.Location(url.Values{"panel": []string{""}}).MergeQuery(true)~ basically removes the query string panel=1, and won't touch any other query strings.
  24. Don't have to be in event func to use push state query, can use a simple ~web.Bind~ to directly change the query string like:
  25. ~~~go
  26. A().Text("change page title").Href("javascript:;").
  27. Attr("@click", web.POST().Queries(url.Values{"title": []string{"Hello"}}).Go()),
  28. ~~~
  29. This don't have ~.MergeQuery(true)~, So it will replace the whole query string to only ~title=Hello~
  30. `),
  31. ).Title("Manipulate Page URL in Event Func").
  32. Slug("basics/manipulate-page-url-in-event-func")