Exemple #1
0
        public Html Dropdown <T>(ObservableArray <T> list, Observable <T> selectedItem, string displayField, string valueField)
        {
            if (Context.NodeName.ToLowerCase() != "select")
            {
                Select.Render();
            }
            var select = Context as HTMLSelectElement;

            ForEach(list, (T model, int index) => {
                var text  = model[displayField] as string;
                var value = model[valueField] as string;
                Option.Text(text).Value(value).End.Render();
            });

            select.SelectedIndex = GetSelectedIndex(list.Data.ToList(), selectedItem.Data, valueField);
            list.Subscribe(realList => {
                select.SelectedIndex = GetSelectedIndex(realList.Array.ToList(), selectedItem.Data, valueField);
            });

            selectedItem.BindingNodes.Add(select);

            Event(EventType.Change, () => {
                var selectedObj   = list.Data[select.SelectedIndex];
                selectedItem.Data = selectedObj;
            });

            // Subscribe change from selectedItem, to update selected index
            selectedItem.Subscribe(val => {
                select.SelectedIndex = GetSelectedIndex(list.Data.ToList(), val.NewData, valueField);
            });

            return(this);
        }
Exemple #2
0
        public Html ForEach <T>(ObservableArray <T> observableArray, Action <T, int> renderer)
        {
            if (observableArray == null)
            {
                throw new ArgumentNullException(nameof(observableArray));
            }
            var element = Context;

            var array  = observableArray.Data as Array;
            var length = array.Length;
            var index  = -1;

            while (++index < length)
            {
                Context = element;
                renderer.Call(element, array[index] ?? index, index);
            }
            observableArray.Subscribe((ObservableArrayArgs <T> e) =>
            {
                e.Element  = element;
                e.Renderer = renderer;
                e.Action   = e.Action ?? ObservableAction.Render;
                Update(e);
            });
            return(this);
        }