diff --git a/README-ru.md b/README-ru.md index 407bd75..2434c68 100644 --- a/README-ru.md +++ b/README-ru.md @@ -3001,6 +3001,21 @@ float32, float64, int, int8…int64, uint, uint8…uint64. func GetDropDownItems(view View, subviewID string) []string +Можно запретить выбор отдельных пунктов. Для этого используется свойство "disabled-items" (константа DisabledItems). +Данному свойству присваивается массив индексов запрещенных пунктов. Индекс может задаваться или числом или в виде текста +или как константа. Поэтому свойству "disabled-items" могут присваиваться следующие типы данных: + +* []int +* int +* []string +* string может содержать несколько индексов разделенных запятыми +* []interface{} содержащий в качестве элементов только: string, int, int8…int64, uint, uint8…uint64. + +Все эти типы данных преопразуются в []interface{} и присваиваются свойству "disabled-items". +Прочитать значение свойства "disabled-items" можно с помощью функции + + func GetDropDownDisabledItems(view View, subviewID string) []int + Выбранное значение определяется int свойством "current" (константа Current). Значение по умолчанию 0. Прочитать значение данного свойства можно с помощью функции diff --git a/README.md b/README.md index c54be8a..74d1d49 100644 --- a/README.md +++ b/README.md @@ -2972,6 +2972,20 @@ You can read the value of the "items" property using the function func GetDropDownItems(view View, subviewID string) []string +You can disable the selection of individual items. For this, the "disabled-items" property (constant DisabledItems) is used. +This property is assigned an array of disabled item indices. The index can be specified either as a number, as text, or as a constant. Therefore, the following data types can be assigned to the "disabled-items" property: + +* []int +* int +* []string +* string - can contain multiple indexes separated by commas +* []interface{} - containing as elements only: string, int, int8…int64, uint, uint8…uint64. + +All of these data types are converted to []interface{} and assigned to the "disabled-items" property. +You can read the value of the "disabled-items" property using the function + + func GetDropDownDisabledItems(view View, subviewID string) []int + The selected value is determined by the int property "current" (Current constant). The default is 0. You can read the value of this property using the function diff --git a/dropDownList.go b/dropDownList.go index 8b170cf..d1505c9 100644 --- a/dropDownList.go +++ b/dropDownList.go @@ -17,6 +17,7 @@ type DropDownList interface { type dropDownListData struct { viewData items []string + disabledItems []interface{} dropDownListener []func(DropDownList, int) } @@ -36,6 +37,7 @@ func (list *dropDownListData) Init(session Session) { list.viewData.Init(session) list.tag = "DropDownList" list.items = []string{} + list.disabledItems = []interface{}{} list.dropDownListener = []func(DropDownList, int){} } @@ -58,6 +60,15 @@ func (list *dropDownListData) remove(tag string) { list.propertyChangedEvent(tag) } + case DisabledItems: + if len(list.disabledItems) > 0 { + list.disabledItems = []interface{}{} + if list.created { + updateInnerHTML(list.htmlID(), list.session) + } + list.propertyChangedEvent(tag) + } + case DropDownEvent: if len(list.dropDownListener) > 0 { list.dropDownListener = []func(DropDownList, int){} @@ -85,10 +96,18 @@ func (list *dropDownListData) Set(tag string, value interface{}) bool { } func (list *dropDownListData) set(tag string, value interface{}) bool { + if value == nil { + list.remove(tag) + return true + } + switch tag { case Items: return list.setItems(value) + case DisabledItems: + return list.setDisabledItems(value) + case DropDownEvent: return list.setDropDownListener(value) @@ -119,7 +138,7 @@ func (list *dropDownListData) setItems(value interface{}) bool { list.items = value case []DataValue: - list.items = []string{} + list.items = make([]string, 0, len(value)) for _, val := range value { if !val.IsObject() { list.items = append(list.items, val.Value()) @@ -133,7 +152,7 @@ func (list *dropDownListData) setItems(value interface{}) bool { } case []interface{}: - items := []string{} + items := make([]string, 0, len(value)) for _, v := range value { switch val := v.(type) { case string: @@ -183,6 +202,91 @@ func (list *dropDownListData) setItems(value interface{}) bool { return true } +func (list *dropDownListData) setDisabledItems(value interface{}) bool { + switch value := value.(type) { + case []int: + list.disabledItems = make([]interface{}, len(value)) + for i, n := range value { + list.disabledItems[i] = n + } + + case []interface{}: + disabledItems := make([]interface{}, len(value)) + for i, val := range value { + if val == nil { + notCompatibleType(DisabledItems, value) + return false + } + + switch val := val.(type) { + case string: + if isConstantName(val) { + disabledItems[i] = val + } else { + n, err := strconv.Atoi(val) + if err != nil { + notCompatibleType(DisabledItems, value) + return false + } + disabledItems[i] = n + } + default: + if n, ok := isInt(val); ok { + disabledItems[i] = n + } else { + notCompatibleType(DisabledItems, value) + return false + } + } + + } + list.disabledItems = disabledItems + + case string: + values := strings.Split(value, ",") + disabledItems := make([]interface{}, len(values)) + for i, str := range values { + str = strings.Trim(str, " ") + if str == "" { + notCompatibleType(DisabledItems, value) + return false + } + if isConstantName(str) { + disabledItems[i] = str + } else { + n, err := strconv.Atoi(str) + if err != nil { + notCompatibleType(DisabledItems, value) + return false + } + disabledItems[i] = n + } + } + list.disabledItems = disabledItems + + case []DataValue: + disabledItems := make([]string, 0, len(value)) + for _, val := range value { + if !val.IsObject() { + disabledItems = append(disabledItems, val.Value()) + } + } + return list.setDisabledItems(disabledItems) + + default: + notCompatibleType(DisabledItems, value) + return false + } + + if list.created { + updateInnerHTML(list.htmlID(), list.session) + } + + list.propertyChangedEvent(Items) + return true + +} + func (list *dropDownListData) setDropDownListener(value interface{}) bool { switch value := value.(type) { case func(DropDownList, int): @@ -250,6 +354,9 @@ func (list *dropDownListData) get(tag string) interface{} { case Items: return list.items + case DisabledItems: + return list.disabledItems + case Current: result, _ := intProperty(list, Current, list.session, 0) return result @@ -273,8 +380,19 @@ func (list *dropDownListData) htmlSubviews(self View, buffer *strings.Builder) { if list.items != nil { current := GetCurrent(list, "") notTranslate := GetNotTranslate(list, "") + disabledItems := GetDropDownDisabledItems(list, "") for i, item := range list.items { - if i == current { + disabled := false + for _, index := range disabledItems { + if i == index { + disabled = true + break + } + } + + if disabled { + buffer.WriteString("