module_shop_act_get_attribute_values.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package modules
  2. import (
  3. "html"
  4. "golang-fave/engine/wrapper"
  5. "golang-fave/utils"
  6. )
  7. func (this *Modules) RegisterAction_ShopGetAttributeValues() *Action {
  8. return this.newAction(AInfo{
  9. WantDB: true,
  10. Mount: "shop-get-attribute-values",
  11. WantAdmin: true,
  12. }, func(wrap *wrapper.Wrapper) {
  13. pf_id := utils.Trim(wrap.R.FormValue("id"))
  14. if !utils.IsNumeric(pf_id) {
  15. wrap.MsgError(`Inner system error`)
  16. return
  17. }
  18. options := ``
  19. rows, err := wrap.DB.Query(
  20. wrap.R.Context(),
  21. `SELECT
  22. id,
  23. name
  24. FROM
  25. fave_shop_filters_values
  26. WHERE
  27. filter_id = ?
  28. ORDER BY
  29. name ASC
  30. ;`,
  31. utils.StrToInt(pf_id),
  32. )
  33. if err == nil {
  34. defer rows.Close()
  35. values := make([]string, 2)
  36. scan := make([]interface{}, len(values))
  37. for i := range values {
  38. scan[i] = &values[i]
  39. }
  40. for rows.Next() {
  41. err = rows.Scan(scan...)
  42. if *wrap.LogCpError(&err) == nil {
  43. options += `<option value="` + html.EscapeString(string(values[0])) + `">` + html.EscapeString(utils.JavaScriptVarValue(string(values[1]))) + `</option>`
  44. }
  45. }
  46. }
  47. wrap.Write(`if($('#prod_attr_` + pf_id + `').length > 0) {`)
  48. wrap.Write(`$('#prod_attr_` + pf_id + ` select').prop('disabled', false).prop('multiple', true);`)
  49. wrap.Write(`$('#prod_attr_` + pf_id + ` select').html('` + options + `');`)
  50. wrap.Write(`$('#prod_attr_` + pf_id + ` select').selectpicker({});`)
  51. wrap.Write(`$('#prod_attr_` + pf_id + ` button').prop('disabled', false);`)
  52. wrap.Write(`}`)
  53. })
  54. }