| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- using InABox.Clients;
- using InABox.Core;
- using Syncfusion.SfDataGrid.XForms;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- namespace comal.timesheets
- {
- public abstract class MobileGrid : SfDataGrid
- {
- List<MobileGridDataModelShell> Data;
- public MobileGrid()
- {
- }
- public void Setup(List<MobileGridDataModelShell> data)
- {
- AutoGenerateColumns = false;
- AssignPropertiesToColumns(data);
- GenerateColumns(data.First());
- ColumnSizer = ColumnSizer.Star;
- AllowResizingColumn = true;
- AllowSorting = true;
- NavigationMode = NavigationMode.Row;
- SelectionMode = Syncfusion.SfDataGrid.XForms.SelectionMode.Single;
- SelectionUnit = SelectionUnit.Row;
- Data = data;
- Device.BeginInvokeOnMainThread(() =>
- {
- ItemsSource = data;
- });
- }
- protected void AssignPropertiesToColumns(List<MobileGridDataModelShell> data)
- {
- foreach (var shell in data)
- {
- if (shell.Data.Count > 0)
- shell.Column1 = shell.Data[0].Item2;
- if (shell.Data.Count > 1)
- shell.Column2 = shell.Data[1].Item2;
- if (shell.Data.Count > 2)
- shell.Column3 = shell.Data[2].Item2;
- if (shell.Data.Count > 3)
- shell.Column4 = shell.Data[3].Item2;
- if (shell.Data.Count > 4)
- shell.Column5 = shell.Data[4].Item2;
- }
- }
- protected void GenerateColumns(MobileGridDataModelShell shell)
- {
- foreach (var col in shell.Data)
- {
- GenerateColumn(new Tuple<string, object>(col.Item1, col.Item2), shell.Data.IndexOf(col));
- }
- if (shell.HasImage)
- {
- GenerateColumn(new Tuple<string, object>("Image", shell.Image), -1);
- }
- }
- protected void GenerateColumn(Tuple<string, object> col, int index)
- {
- if (index > 4)
- return;
- var column = FindColumnType(col.Item2);
- column = AddMapping(column, index);
- column = AddCaption(column, col.Item1);
- Device.BeginInvokeOnMainThread(() =>
- {
- Columns.Add(column);
- });
- }
- protected GridColumn FindColumnType(object value)
- {
- if(value == null || value.GetType() == typeof(ImageSource))
- return new GridImageColumn();
- if (value.GetType() == typeof(string))
- return new GridTextColumn();
- return new GridTextColumn();
- }
- protected GridColumn AddMapping(GridColumn col, int index)
- {
- switch (index)
- {
- case -1:
- col.MappingName = "Image";
- break;
- case 0:
- col.MappingName = "Column1";
- break;
- case 1:
- col.MappingName = "Column2";
- break;
- case 2:
- col.MappingName = "Column3";
- break;
- case 3:
- col.MappingName = "Column4";
- break;
- case 4:
- col.MappingName = "Column5";
- break;
- }
- return col;
- }
- protected GridColumn AddCaption(GridColumn col, string caption)
- {
- col.HeaderText = caption;
- col.HeaderCellTextSize = 18;
- col.HeaderFontAttribute = FontAttributes.Bold;
- return col;
- }
- }
- public class MobileGridDataModelShell
- {
- public Guid ID { get; set; }
- public List<Tuple<string, string>> Data { get; set; }
- public string Column1 { get; set; }
- public string Column2 { get; set; }
- public string Column3 { get; set; }
- public string Column4 { get; set; }
- public string Column5 { get; set; }
- public ImageSource Image { get; set; }
- public bool HasImage { get; set; }
- /// <summary>
- /// max of 5 columns to display on mobile
- /// </summary>
- /// <param name="id"></param>
- /// <param name="data"></param>
- public MobileGridDataModelShell(Guid id, List<Tuple<string, string>> data, Image image = null)
- {
- ID = id;
- Data = data;
- if (image != null)
- {
- Image = image.Source;
- HasImage = true;
- }
- else
- {
- Image = null;
- HasImage = false;
- }
- }
- }
- }
|