using System.Collections.Generic; using System.Web.WebPages; using ContractService.Views.Shared.SearchForm; namespace ContractService.Views.Shared.Table { public class TableViewModel { public TableViewModel() { IsPage = true; IsClickSelect = true; IsSingleSelect = true; } public TableViewModel(string tableId = "table") : this() { TableId = tableId; PageArray = new[] { 25, 50, 100, 200 }; IsSingleSelect = true; } public TableViewModel(string url, string currentPage, SearchFormViewModel searchForm, string tableId = "table", bool hasBox = true) : this(tableId) { TableId = tableId; Url = url; CurrentPage = currentPage; SearchForm = searchForm; HasBox = hasBox; Other = ""; } public TableViewModel(string url, string currentPage = null, string tableId = "table", bool hasBox = true) : this(url, currentPage, null, tableId, hasBox) { TableId = tableId; Url = url; CurrentPage = currentPage; IsSingleSelect = true; PageArray = new[] { 25, 50, 100, 200 }; Other = ""; } public TableViewModel(string url, string currentPage, SearchFormViewModel searchForm, List fieldItems, string tableId = "table") : this(url, currentPage, searchForm, tableId) { TableId = tableId; Url = url; CurrentPage = currentPage; FieldItems = fieldItems; SearchForm = searchForm; } public SearchFormViewModel SearchForm { get; set; } public string TableId { get; set; } public string _fieldId; public string FieldId => _fieldId.IsEmpty() ? "id" : _fieldId; public string Url { get; set; } public bool HasBox { get; set; } public string CurrentPage { get; set; } public bool IsPage { get; set; } public string PageStr => IsPage ? "true" : "false"; public int[] PageArray { get; set; } public string PageArrayStr => string.Join(",", PageArray); public bool IsSingleSelect { get; set; } public string SingleSelectStr => IsSingleSelect ? "true" : "false"; public bool IsClickSelect { get; set; } public string ClickSelectStr => IsClickSelect ? "true" : "false"; public bool IsLocalData { get; set; } public string LocalDataStr => IsLocalData ? "" : "data-method=\"post\" data-side-pagination=\"server\""; public string Other { get; set; } public string RowAttributes { get; set; } public List FieldItems { get; set; } public TableViewModel SetTable(string active, string url = "", bool hasBox = true, string id = "table") { CurrentPage = active; TableId = id; Url = url; HasBox = HasBox; return this; } public TableViewModel SetId(string id) { TableId = id; return this; } public TableViewModel SetFieldId(string fieldId) { _fieldId = fieldId; return this; } public TableViewModel SetNotBox() { HasBox = false; return this; } public TableViewModel SetSearchForm(SearchFormViewModel search) { SearchForm = search; return this; } public TableViewModel SetNotPage() { IsPage = false; return this; } public TableViewModel SetPageArray(params int[] page) { IsPage = true; PageArray = page; return this; } public TableViewModel SetMultipleSelect() { IsSingleSelect = false; return this; } public TableViewModel SetRowAttributes(string rowAttributes) { RowAttributes = rowAttributes ?? ""; return this; } public TableViewModel SetOther(string other) { Other = other ?? ""; return this; } public TableViewModel SetFields(List filedItems) { FieldItems = filedItems; return this; } public TableViewModel AddFields(List filedItems) { FieldItems = FieldItems ?? new List(); FieldItems.AddRange(filedItems); return this; } public TableViewModel AddField(FieldItem fieldItem) { FieldItems = FieldItems ?? new List(); FieldItems.Add(fieldItem); return this; } public TableViewModel SetLocalData() { IsLocalData = true; return this; } public TableViewModel SetClickSelect(bool isClickSelect = true) { IsClickSelect = isClickSelect; return this; } public TableViewModel SetSingleSelect(bool isSingleSelect = true) { IsSingleSelect = isSingleSelect; return this; } } public class FieldItem { public FieldItem(string filed, string displayName, string formatter = "", string align = "center", bool isTip = false, bool isSort = false) { Filed = filed; DisplayName = displayName; Formatter = formatter; Align = align; IsTip = isTip; IsSort = isSort; } public FieldItem SetWidth(int width, string unit = "px") { Width = width; WidthUnit = unit; return this; } public FieldItem SetTip() { IsTip = true; return this; } public FieldItem SetSort() { IsSort = true; return this; } public FieldItem SetRight() { Align = "right"; return this; } public FieldItem SetLeft() { Align = "left"; return this; } public string Filed { get; set; } public string DisplayName { get; set; } public string Formatter { get; set; } public string FormatterStr => string.IsNullOrEmpty(Formatter) ? "" : "data-formatter=\"" + Formatter + "\""; public string Align { get; set; } public int? Width { get; set; } public string WidthUnit { get; set; } public string WidthStr => Width != null ? $" data-width=\"{Width}{WidthUnit}\"" : ""; public bool IsSort { get; set; } public string IsSortStr => IsSort ? " data-sortable=\"true\"" : ""; private bool IsTip { get; set; } public string IsTipStr => IsTip ? "data-class=\"iwb-tips\"" : ""; } }