Пример #1
0
        private string GetSelect()
        {
            StringBuilder select = new StringBuilder();

            TagBuilder.Begin(select, "select");
            TagBuilder.AddId(select, this.FieldConfig.ColumnName);
            TagBuilder.AddAttribute(select, "data-scrud", "select");

            if (!this.FieldConfig.IsNullable)
            {
                TagBuilder.AddRequired(select);
            }

            if (this.FieldConfig.IsDisabled)
            {
                TagBuilder.AddDisabled(select);
            }


            TagBuilder.Close(select);

            using (
                DataTable table = GetTable(this.FieldConfig.ParentTableSchema, this.FieldConfig.ParentTable,
                                           this.FieldConfig.ParentTableColumn, this.Config.DisplayViews, this.Config.UseDisplayViewsAsParents))
            {
                //Get the expression value of display field from comma separated list of expressions.
                //The expression can be either the column name or a column expression.
                string columnOrExpression = Helpers.Expression.GetExpressionValue(this.Config.DisplayFields,
                                                                                  this.FieldConfig.ParentTableSchema, this.FieldConfig.ParentTable, this.FieldConfig.ParentTableColumn);

                //Let's check whether the display field expression really exists.
                //If it does not exist, it is probably an expression column.
                if (!table.Columns.Contains(columnOrExpression) && !string.IsNullOrWhiteSpace(columnOrExpression))
                {
                    //Add the expression as a new column in the datatable.
                    table.Columns.Add("DataTextField", typeof(string), columnOrExpression);
                    columnOrExpression = "DataTextField";
                }

                if (table.Rows.Count > 0)
                {
                    if (this.FieldConfig.IsNullable)
                    {
                        TagBuilder.Begin(select, "option");
                        TagBuilder.AddAttribute(select, "value", string.Empty);
                        TagBuilder.Close(select);
                        select.Append(string.Empty);
                        TagBuilder.EndTag(select, "option");
                    }

                    foreach (DataRow row in table.Rows)
                    {
                        string value = row[this.FieldConfig.ParentTableColumn].ToString();

                        TagBuilder.Begin(select, "option");
                        TagBuilder.AddAttribute(select, "value", value);


                        if (value == this.FieldConfig.DefaultValue)
                        {
                            TagBuilder.AddSelected(select);
                        }

                        TagBuilder.Close(select);

                        @select.Append(!string.IsNullOrWhiteSpace(columnOrExpression) ? row[columnOrExpression] : row[1]);

                        TagBuilder.EndTag(select, "option");
                    }
                }
            }

            TagBuilder.EndTag(select, "select");

            return(select.ToString());
        }