|
@@ -0,0 +1,312 @@
|
|
|
+using Android.Content;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Collections.ObjectModel;
|
|
|
+using System.Linq;
|
|
|
+using System.Reflection;
|
|
|
+using Xamarin.Forms;
|
|
|
+using Xamarin.Forms.Xaml;
|
|
|
+
|
|
|
+namespace comal.timesheets
|
|
|
+{
|
|
|
+ [XamlCompilation(XamlCompilationOptions.Compile)]
|
|
|
+ public partial class MobileDataGrid : ContentView
|
|
|
+ {
|
|
|
+ ObservableCollection<DataGridFilter> Filters = new ObservableCollection<DataGridFilter>();
|
|
|
+ public List<DataGridViewModelItem> Items { get; set; }
|
|
|
+
|
|
|
+ PropertyInfo[] info = typeof(DataGridViewModelItem).GetProperties();
|
|
|
+
|
|
|
+ Type Type;
|
|
|
+ public MobileDataGrid()
|
|
|
+ {
|
|
|
+ InitializeComponent();
|
|
|
+ Filters.CollectionChanged += Filters_CollectionChanged;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Setup(List<DataGridViewModelItem> items, Type type)
|
|
|
+ {
|
|
|
+ Type = type;
|
|
|
+ SetupHeadersAndDataTemplate(items.First());
|
|
|
+ Items = items;
|
|
|
+ Refresh(Items);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Refresh(List<DataGridViewModelItem> items)
|
|
|
+ {
|
|
|
+ listView.ItemsSource = items;
|
|
|
+ countLbl.Text = items.Count + " Records";
|
|
|
+ }
|
|
|
+
|
|
|
+ public void SetupHeadersAndDataTemplate(DataGridViewModelItem item)
|
|
|
+ {
|
|
|
+ GenerateHeaders(item);
|
|
|
+ //GenerateDataTemplate(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void GenerateDataTemplate(DataGridViewModelItem item)
|
|
|
+ {
|
|
|
+ var grid = new Grid
|
|
|
+ {
|
|
|
+ HorizontalOptions = LayoutOptions.FillAndExpand,
|
|
|
+ VerticalOptions = LayoutOptions.Center,
|
|
|
+ Margin = new Thickness(0),
|
|
|
+ Padding = new Thickness(2),
|
|
|
+ };
|
|
|
+ grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
|
|
|
+ int count = 0;
|
|
|
+ foreach (var tuple in item.Data)
|
|
|
+ {
|
|
|
+ grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
|
|
|
+ grid.Children.Add(CreateNewListViewLabel(count));
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ if (item.Image != null)
|
|
|
+ {
|
|
|
+ grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
|
|
|
+ grid.Children.Add(CreatNewListViewImage(count, item.Image));
|
|
|
+ }
|
|
|
+ listView.ItemTemplate = new DataTemplate(() =>
|
|
|
+ {
|
|
|
+ return new ViewCell { View = grid };
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private View CreatNewListViewImage(int count, Image img)
|
|
|
+ {
|
|
|
+ var newImage = new Image
|
|
|
+ {
|
|
|
+ Source = img.Source,
|
|
|
+ HeightRequest = 30,
|
|
|
+ WidthRequest = 30,
|
|
|
+ HorizontalOptions = LayoutOptions.Center,
|
|
|
+ VerticalOptions = LayoutOptions.Center
|
|
|
+ };
|
|
|
+ newImage.SetBinding(Image.SourceProperty, "Source");
|
|
|
+ return SetGridValues(newImage, 0, count);
|
|
|
+ }
|
|
|
+
|
|
|
+ private View CreateNewListViewLabel(int count)
|
|
|
+ {
|
|
|
+ var lbl = new Label();
|
|
|
+ lbl.SetBinding(Label.TextProperty, new Binding("Col" + count));
|
|
|
+ return SetGridValues(lbl, 0, count);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void GenerateHeaders(DataGridViewModelItem item)
|
|
|
+ {
|
|
|
+ int count = 0;
|
|
|
+ foreach (var tuple in item.Data)
|
|
|
+ {
|
|
|
+ CreateNewHeader(tuple.Item1, count);
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ //if (item.Image != null)
|
|
|
+ //CreateNewHeader("Image", count);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void CreateNewHeader(string name, int count)
|
|
|
+ {
|
|
|
+ headerGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
|
|
|
+ DataGridHeaderRow header = new DataGridHeaderRow { ColumnName = name, ColumnNumber = count };
|
|
|
+ header.Setup();
|
|
|
+ header.OnDataGridHeaderFilterTapped += Header_OnDataGridHeaderFilterTapped;
|
|
|
+ header.OnDataGridHeaderTapped += Header_OnDataGridHeaderTapped;
|
|
|
+ headerGrid.Children.Add(SetGridValues(header, 0, count));
|
|
|
+ CreateSearchEntry(name, count);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void CreateSearchEntry(string name, int count)
|
|
|
+ {
|
|
|
+ var searchEnt = new DataGridSearchEntry { ColumnName = name, ColumnNumber = count };
|
|
|
+ searchEnt.OnDataGridSearchEntryChanged += SearchEnt_OnDataGridSearchEntryChanged;
|
|
|
+ searchEnt.IsEnabled = name == "Image" ? false : true;
|
|
|
+ headerGrid.Children.Add(SetGridValues(searchEnt, 1, count));
|
|
|
+ }
|
|
|
+
|
|
|
+ private View SetGridValues(View view, int row, int column)
|
|
|
+ {
|
|
|
+ view.SetValue(Grid.RowProperty, row);
|
|
|
+ view.SetValue(Grid.ColumnProperty, column);
|
|
|
+ return view;
|
|
|
+ }
|
|
|
+
|
|
|
+ #region Events
|
|
|
+ private void SearchEnt_OnDataGridSearchEntryChanged(int columnnumber, string value)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrWhiteSpace(value))
|
|
|
+ Filters.Remove(Filters.FirstOrDefault(x => x.ColNumber == "Col" + columnnumber));
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (Filters.FirstOrDefault(x => x.ColNumber == "Col" + columnnumber) != null)
|
|
|
+ Filters.Remove(Filters.FirstOrDefault(x => x.ColNumber == "Col" + columnnumber));
|
|
|
+ Filters.Add(new DataGridFilter { ColNumber = "Col" + columnnumber, Value = value, FilterNumber = FindNumber(columnnumber) });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private FilterNumber FindNumber(int columnnumber)
|
|
|
+ {
|
|
|
+ switch (columnnumber)
|
|
|
+ {
|
|
|
+ case 0:
|
|
|
+ return FilterNumber.Zero;
|
|
|
+ case 1:
|
|
|
+ return FilterNumber.One;
|
|
|
+ case 2:
|
|
|
+ return FilterNumber.Two;
|
|
|
+ case 3:
|
|
|
+ return FilterNumber.Three;
|
|
|
+ default:
|
|
|
+ return FilterNumber.Zero;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void DoSearch()
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ bool bSearching = false;
|
|
|
+
|
|
|
+ private void Filters_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
|
|
|
+ {
|
|
|
+ if (bSearching)
|
|
|
+ return;
|
|
|
+
|
|
|
+ bSearching = true;
|
|
|
+
|
|
|
+ List<DataGridViewModelItem> finalList = new List<DataGridViewModelItem>();
|
|
|
+
|
|
|
+ if (Filters.FirstOrDefault(x => x.FilterNumber == FilterNumber.Zero) != null)
|
|
|
+ {
|
|
|
+ var filter = Filters.FirstOrDefault(x => x.FilterNumber == FilterNumber.Zero);
|
|
|
+ var list = Items.Where(x => x.Col0.Contains(filter.Value));
|
|
|
+ foreach (DataGridViewModelItem item in list)
|
|
|
+ finalList.Add(item);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ foreach (DataGridViewModelItem item in Items)
|
|
|
+ finalList.Add(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Filters.FirstOrDefault(x => x.FilterNumber == FilterNumber.One) != null)
|
|
|
+ {
|
|
|
+ List<DataGridViewModelItem> intermediatelist = new List<DataGridViewModelItem>();
|
|
|
+ var filter = Filters.FirstOrDefault(x => x.FilterNumber == FilterNumber.One);
|
|
|
+ var list = finalList.Where(x => x.Col1.Contains(filter.Value));
|
|
|
+ foreach (var item in list)
|
|
|
+ intermediatelist.Add(item);
|
|
|
+ if (intermediatelist.Count > 0)
|
|
|
+ finalList.Clear();
|
|
|
+ foreach (DataGridViewModelItem item in intermediatelist)
|
|
|
+ finalList.Add(item);
|
|
|
+ }
|
|
|
+ else if (finalList.Count == 0)
|
|
|
+ {
|
|
|
+ foreach (DataGridViewModelItem item in Items)
|
|
|
+ finalList.Add(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Filters.FirstOrDefault(x => x.FilterNumber == FilterNumber.Two) != null)
|
|
|
+ {
|
|
|
+ List<DataGridViewModelItem> intermediatelist = new List<DataGridViewModelItem>();
|
|
|
+ var filter = Filters.FirstOrDefault(x => x.FilterNumber == FilterNumber.Two);
|
|
|
+ var list = finalList.Where(x => x.Col2.Contains(filter.Value));
|
|
|
+ foreach (var item in list)
|
|
|
+ intermediatelist.Add(item);
|
|
|
+ if (intermediatelist.Count > 0)
|
|
|
+ finalList.Clear();
|
|
|
+ foreach (DataGridViewModelItem item in intermediatelist)
|
|
|
+ finalList.Add(item);
|
|
|
+ }
|
|
|
+ else if (finalList.Count == 0)
|
|
|
+ {
|
|
|
+ foreach (DataGridViewModelItem item in Items)
|
|
|
+ finalList.Add(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (Filters.FirstOrDefault(x => x.FilterNumber == FilterNumber.Three) != null)
|
|
|
+ {
|
|
|
+ List<DataGridViewModelItem> intermediatelist = new List<DataGridViewModelItem>();
|
|
|
+ var filter = Filters.FirstOrDefault(x => x.FilterNumber == FilterNumber.Three);
|
|
|
+ var list = finalList.Where(x => x.Col3.Contains(filter.Value));
|
|
|
+ foreach (var item in list)
|
|
|
+ intermediatelist.Add(item);
|
|
|
+ if (intermediatelist.Count > 0)
|
|
|
+ finalList.Clear();
|
|
|
+ foreach (DataGridViewModelItem item in intermediatelist)
|
|
|
+ finalList.Add(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ Refresh(finalList);
|
|
|
+
|
|
|
+ bSearching = false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void Header_OnDataGridHeaderTapped(int columnnumber)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Header_OnDataGridHeaderFilterTapped(int columnnumber, string columnname)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public enum FilterNumber
|
|
|
+ {
|
|
|
+ Zero,
|
|
|
+ One,
|
|
|
+ Two,
|
|
|
+ Three
|
|
|
+ }
|
|
|
+
|
|
|
+ public class DataGridFilter
|
|
|
+ {
|
|
|
+ public string ColNumber { get; set; }
|
|
|
+ public string Value { get; set; }
|
|
|
+ public FilterNumber FilterNumber { get; set; }
|
|
|
+ }
|
|
|
+ public class DataGridViewModelItem
|
|
|
+ {
|
|
|
+ public Guid ID { get; set; }
|
|
|
+
|
|
|
+ public List<Tuple<string, string>> Data { get; set; }
|
|
|
+ public string Col0 { get; set; }
|
|
|
+ public string Col1 { get; set; }
|
|
|
+ public string Col2 { get; set; }
|
|
|
+ public string Col3 { get; set; }
|
|
|
+ public Image Image { get; set; }
|
|
|
+ public ImageSource Source
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ return Image.Source;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public GridLength ColWidth0 { get; set; }
|
|
|
+ public GridLength ColWidth1 { get; set; }
|
|
|
+ public GridLength ColWidth2 { get; set; }
|
|
|
+ public GridLength ColWidth3 { get; set; }
|
|
|
+
|
|
|
+ public DataGridViewModelItem(Guid id, List<Tuple<string, string>> data, Image image = null)
|
|
|
+ {
|
|
|
+ ID = id;
|
|
|
+ Data = data;
|
|
|
+ Image = image;
|
|
|
+ Col0 = data.Count > 0 ? data[0].Item2 : "";
|
|
|
+ Col1 = data.Count > 1 ? data[1].Item2 : "";
|
|
|
+ Col2 = data.Count > 2 ? data[2].Item2 : "";
|
|
|
+ Col3 = data.Count > 3 ? data[3].Item2 : "";
|
|
|
+ ColWidth0 = data.Count > 0 ? new GridLength(1, GridUnitType.Star) : new GridLength(0, GridUnitType.Absolute);
|
|
|
+ ColWidth1 = data.Count > 1 ? new GridLength(1, GridUnitType.Star) : new GridLength(0, GridUnitType.Absolute);
|
|
|
+ ColWidth2 = data.Count > 2 ? new GridLength(1, GridUnitType.Star) : new GridLength(0, GridUnitType.Absolute);
|
|
|
+ ColWidth3 = data.Count > 3 ? new GridLength(1, GridUnitType.Star) : new GridLength(0, GridUnitType.Absolute);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|