Esempio n. 1
0
        /// <summary>
        ///     创建JavaScript字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            var script = new StringBuilder();

            script.Append("{");

            if (_align != Align.Left)
            {
                script.AppendFormat("align:'{0}',", _align.ToString().ToLower());
            }

            if (!_classes.IsEmpty())
            {
                script.AppendFormat("classes:'{0}',", _classes);
            }

            script.AppendFormat("name:'{0}',", _columnName);

            if (_firstSortOrder.HasValue)
            {
                script.AppendFormat("firstsortorder:'{0}',", _firstSortOrder.ToString().ToLower());
            }

            if (_fixedWidth.HasValue)
            {
                script.AppendFormat("fixed:{0},", _fixedWidth.Value);
            }

            if (_formatter != null && _formatter == "custom" && !_customFormatter.IsEmpty())
            {
                script.AppendFormat("formatter:{0},", _customFormatter);
            }

            if (_formatter != null && _formatter != "custom")
            {
                script.AppendFormat("formatter:'{0}',", _formatter.ToLower());
            }

            if (_formatter != null && !_formatOptions.IsEmpty())
            {
                script.Append("formatter:'" + _formatter.ToString().ToLower() + "', formatoptions: " + _formatOptions + ",");
            }

            if (_hidden.HasValue)
            {
                script.AppendFormat("hidden:{0},", _hidden.Value.ToString().ToLower());
            }

            if (_key.HasValue)
            {
                script.AppendFormat("key:{0},", _key.Value.ToString().ToLower());
            }

            if (!_label.IsEmpty())
            {
                script.AppendFormat("label:'{0}',", _label);
            }

            if (_resizeable.HasValue)
            {
                script.AppendFormat("resizable:{0},", _resizeable.Value.ToString().ToLower());
            }

            if (_search.HasValue)
            {
                script.AppendFormat("search:{0},", _search.Value.ToString().ToLower());
            }

            if (_sortable.HasValue)
            {
                script.AppendFormat("sortable:{0},", _sortable.Value.ToString().ToLower());
            }

            if (_title.HasValue)
            {
                script.AppendFormat("title:{0},", _title.Value.ToString().ToLower());
            }

            if (_width.HasValue)
            {
                script.AppendFormat("width:{0},", _width.Value);
            }

            if (_editable.HasValue)
            {
                script.AppendFormat("editable:{0},", _editable.Value.ToString().ToLower());
            }

            var searchOptions = new Dictionary <string, string>();

            if (_searchType.HasValue)
            {
                if (_searchType.Value == Searchtype.Text)
                {
                    script.Append("stype:'text',");
                }

                if (_searchType.Value == Searchtype.Select)
                {
                    script.Append("stype:'select',");
                }

                if (_searchOptions.Any())
                {
                    searchOptions.Add("sopt", $"['{_searchOptions.Aggregate((current, next) => $"{current}',  '{next}")}']");
                }
                else
                {
                    searchOptions.Add("sopt", "['bw']");
                }
            }

            if (_searchType == Searchtype.Select || _searchType == Searchtype.Datepicker)
            {
                if (_searchType == Searchtype.Select)
                {
                    if (_searchTerms != null)
                    {
                        var emtpyOption = (_searchTerms.Any()) ? ":;" : ":";
                        searchOptions.Add("value", $"\"{emtpyOption}{string.Join(";", _searchTerms.Select(s => $"{s.Key}:{s.Value}").ToArray())}\"");
                    }
                    else
                    {
                        searchOptions.Add("value", "':'");
                    }
                }

                if (_searchType == Searchtype.Datepicker)
                {
                    if (_searchDateFormat.IsEmpty())
                    {
                        searchOptions.Add("dataInit", "function(el){$(el).datepicker({changeYear:true, onSelect: function() {var sgrid = $('###gridid##')[0]; sgrid.triggerToolbar();},dateFormat:'dd-mm-yy'});}");
                    }
                    else
                    {
                        searchOptions.Add("dataInit", "function(el){$(el).datepicker({changeYear:true, onSelect: function() {var sgrid = $('###gridid##')[0]; sgrid.triggerToolbar();},dateFormat:'" + _searchDateFormat + "'});}");
                    }
                }
            }

            if (_searchType.HasValue && !_defaultSearchValue.IsEmpty())
            {
                searchOptions.Add("defaultValue", "'" + _defaultSearchValue + "'");
            }

            if ((!_searchType.HasValue && !_defaultSearchValue.IsEmpty()))
            {
                searchOptions.Add("defaultValue", "'" + _defaultSearchValue + "'");
            }

            if (_clearSearch.HasValue)
            {
                searchOptions.Add("clearSearch", _clearSearch.Value.ToString().ToLower());
            }

            if (_searchOptions.Any() && !_searchType.HasValue) // When searchtype is set, searchoptions is already added
            {
                searchOptions.Add("sopt", "['" + _searchOptions.Aggregate((current, next) => current + "', '" + next) + "']");
            }

            if (searchOptions.Any())
            {
                script.Append("searchoptions: { " + string.Join(", ", searchOptions.Select(x => x.Key + ":" + x.Value)) + " },");
            }

            if (_editType.HasValue)
            {
                script.AppendFormat("edittype:'{0}',", _editType.Value.ToString().ToLower());
            }

            if (_editOptions != null)
            {
                script.AppendFormat("editoptions:{0},", _editOptions.ToString());
            }

            if (_editRules != null)
            {
                script.AppendFormat("editrules:{0},", _editRules.ToString());
            }

            if (_editFormOptions != null)
            {
                script.AppendFormat("formoptions:{0},", _editFormOptions.ToString());
            }

            if (_sortType.HasValue)
            {
                script.AppendFormat("sorttype:'{0}',", _sortType.ToString().ToLower());
            }

            script.AppendFormat("index:'{0}'", _index);

            script.Append("}");

            return(script.ToString());
        }