Ejemplo n.º 1
0
        protected virtual Select2Options GetSelect2Options()
        {
            var emptyItemText = EmptyItemText();

            return(new Select2Options
            {
                Data = items,
                MinimumResultsForSearch = 10,
                PlaceHolder = !emptyItemText.IsEmptyOrNull() ? emptyItemText : null,
                AllowClear = emptyItemText != null,
                Query = delegate(Select2QueryOptions query)
                {
                    var term = query.Term.IsEmptyOrNull() ? "" : Q.Externals.StripDiacritics(query.Term ?? "").ToUpperCase();
                    var results = this.items.Filter(item =>
                    {
                        return (term == null ||
                                Q.Externals.StripDiacritics(item.Text ?? "").ToUpperCase().StartsWith(term));
                    });

                    results.AddRange(this.items.Filter(item =>
                    {
                        return term != null &&
                        !Q.Externals.StripDiacritics(item.Text ?? "").ToUpperCase().StartsWith(term) &&
                        Q.Externals.StripDiacritics(item.Text ?? "").ToUpperCase().IndexOf(term) >= 0;
                    }));

                    query.Callback(new Select2Result
                    {
                        Results = results.Slice((query.Page - 1) * pageSize, query.Page * pageSize),
                        More = results.Count >= query.Page * pageSize
                    });
                },
                InitSelection = delegate(jQueryObject element, Action <object> callback)
                {
                    var val = element.GetValue();
                    Select2Item item = null;
                    for (var i = 0; i < this.items.Count; i++)
                    {
                        var x = items[i];
                        if (x.Id == val)
                        {
                            item = x;
                            break;
                        }
                    }

                    callback(item);
                }
            });
        }
Ejemplo n.º 2
0
 protected void AddItem(Select2Item item)
 {
     this.items.Add(item);
     this.itemById[item.Id] = item;
 }
Ejemplo n.º 3
0
        protected virtual Select2Options GetSelect2Options()
        {
            var emptyItemText = EmptyItemText();

            return(new Select2Options
            {
                Data = items,
                PlaceHolder = !emptyItemText.IsEmptyOrNull() ? emptyItemText : null,
                AllowClear = emptyItemText != null,
                CreateSearchChoicePosition = "bottom",
                Query = delegate(Select2QueryOptions query)
                {
                    var term = query.Term.IsEmptyOrNull() ? "" : Q.Externals.StripDiacritics(query.Term ?? "").ToUpperCase();
                    var results = this.items.Filter(item =>
                    {
                        return (term == null ||
                                Q.Externals.StripDiacritics(item.Text ?? "").ToUpperCase().StartsWith(term));
                    });

                    results.AddRange(this.items.Filter(item =>
                    {
                        return term != null &&
                        !Q.Externals.StripDiacritics(item.Text ?? "").ToUpperCase().StartsWith(term) &&
                        Q.Externals.StripDiacritics(item.Text ?? "").ToUpperCase().IndexOf(term) >= 0;
                    }));

                    query.Callback(new Select2Result
                    {
                        Results = results.Slice((query.Page - 1) * pageSize, query.Page * pageSize),
                        More = results.Count >= query.Page * pageSize
                    });
                },
                InitSelection = delegate(jQueryObject element, Action <object> callback)
                {
                    var val = element.GetValue();
                    var isAutoComplete = this.IsAutoComplete();

                    if (multiple)
                    {
                        var list = new List <object>();
                        foreach (var z in val.Split(","))
                        {
                            var item = itemById[z];

                            if (item == null && isAutoComplete)
                            {
                                item = new Select2Item {
                                    Id = z, Text = z
                                };
                                AddItem(item);
                            }

                            if (item != null)
                            {
                                list.Add(item);
                            }
                        }

                        callback(list);
                        return;
                    }

                    var it = itemById[val];
                    if (it == null && isAutoComplete)
                    {
                        it = new Select2Item {
                            Id = val, Text = val
                        };
                        AddItem(it);
                    }

                    callback(it);
                }
            });
        }