[CLOSED] Implement the remote sort gridpanel call

  1. #1

    [CLOSED] Implement the remote sort gridpanel call

    I need to apply the "sort" to the next code, I use it from a gridpanel

    generic .ashx handler
         StoreRequestParameters prms2 = new StoreRequestParameters(context);
                        _iStart = prms2.Start;
                        _iLimit = prms2.Limit;
                        _sOrderBy = prms2.Sort[0].Property; //prms.SimpleSort;
                        _sOrderDirection = prms2.Sort[0].Direction.ToString(); //prms.SimpleSortDirection
                        _sFilter = context.Request["filterheader"];
                        context.Response.Write(JSON.Serialize(new UsuarioRepository().GetPagingFilterHeader(_iStart, _iLimit, _sOrderBy, _sOrderDirection, _sFilter)));
                        break;

    Here is the GetPagingFilterHeader function, I need to have the sort code, as it is in this example:

    https://examples3.ext.net/#/GridPane...Header/Remote/

     public Paging<pxyUsuario> GetPagingFilterHeader(int start, int limit, string sort, string dir, string filter)
            {
                using (IDbConnection Db = DataConn.GetDbFactory().OpenDbConnection())
                {
                    List<pxyUsuario> pxyUsuarios = Db.Select<pxyUsuario>();
    
                    FilterHeaderConditions fhc = new FilterHeaderConditions(filter);
    
                    foreach (FilterHeaderCondition condition in fhc.Conditions)
                    {
                        string dataIndex = condition.DataIndex;
                        FilterType type = condition.Type;
                        string op = condition.Operator;
                        object value = null;
    
                        switch (condition.Type)
                        {
                            case FilterType.Boolean:
                                value = condition.Value<bool>();
                                break;
    
                            case FilterType.Date:
                                switch (condition.Operator)
                                {
                                    case "=":
                                        value = condition.Value<DateTime>();
                                        break;
    
                                    case "compare":
                                        value = FilterHeaderComparator<DateTime>.Parse(condition.JsonValue);
                                        break;
                                }
                                break;
    
                            case FilterType.Number:
                                bool isInt = pxyUsuarios.Count > 0 && pxyUsuarios[0].GetType().GetProperty(dataIndex).PropertyType == typeof(int);
                                switch (condition.Operator)
                                {
                                    case "=":
                                        if (isInt)
                                        {
                                            value = condition.Value<int>();
                                        }
                                        else
                                        {
                                            value = condition.Value<double>();
                                        }
                                        break;
    
                                    case "compare":
                                        if (isInt)
                                        {
                                            value = FilterHeaderComparator<int>.Parse(condition.JsonValue);
                                        }
                                        else
                                        {
                                            value = FilterHeaderComparator<double>.Parse(condition.JsonValue);
                                        }
    
                                        break;
                                }
    
                                break;
                            case FilterType.String:
                                value = condition.Value<string>();
                                break;
                            default:
                                throw new ArgumentOutOfRangeException();
                        }
    
                        pxyUsuarios.RemoveAll(item =>
                        {
                            object oValue = item.GetType().GetProperty(dataIndex).GetValue(item, null);
                            string matchValue = null;
                            string itemValue = null;
    
                            if (type == FilterType.String)
                            {
                                //matchValue = (string)value;
                                //itemValue = oValue as string;
                                matchValue = ((string)value).ToUpper();
                                itemValue = (oValue as string).ToUpper();
                            }
    
                            switch (op)
                            {
                                case "=":
                                    return oValue == null || !oValue.Equals(value);
                                case "compare":
                                    return !((IEquatable<IComparable>)value).Equals((IComparable)oValue);
                                case "+":
                                    return itemValue == null || !itemValue.StartsWith(matchValue);
                                case "-":
                                    return itemValue == null || !itemValue.EndsWith(matchValue);
                                case "!":
                                    return itemValue == null || itemValue.IndexOf(matchValue) >= 0;
                                case "*":
                                    return itemValue == null || itemValue.IndexOf(matchValue) < 0;
                                default:
                                    throw new Exception("Not supported operator");
                            }
                        });
                    }
                    //-- end filtering ------------------------------------------------------------
    
    
                    //-- start sorting ------------------------------------------------------------
    // This is what I need to implement, I copied from the link I mentioned above, obviously the e.Sort doesnt exist in my code                
                    if (e.Sort.Length > 0)
                    {
                        pxyUsuarios.Sort(delegate(object x, object y)  //here also is not recognized the x and y objects
                        {
                            object a;
                            object b;
    
                            int direction = e.Sort[0].Direction == Ext.Net.SortDirection.DESC ? -1 : 1;
    
                            a = x.GetType().GetProperty(e.Sort[0].Property).GetValue(x, null);
                            b = y.GetType().GetProperty(e.Sort[0].Property).GetValue(y, null);
                            return CaseInsensitiveComparer.Default.Compare(a, b) * direction;
                        });
                    }
                    //-- end sorting ------------------------------------------------------------
    
                    //-- start paging ------------------------------------------------------------
                    if ((start + limit) > pxyUsuarios.Count)
                    {
                        limit = pxyUsuarios.Count - start;
                    }
    
                    List<pxyUsuario> rangeUsuario = (start < 0 || limit < 0) ? pxyUsuarios : pxyUsuarios.GetRange(start, limit);
    
    
                    return new Paging<pxyUsuario>(rangeUsuario, pxyUsuarios.Count);
                }

    pxyUsuario class:
      public class pxyUsuario
        {
            public int Id { get; set; }
            public string Cuenta { get; set; }
            public string UsuarioPerfil { get; set; }
            public string Nombre { get; set; }
            public string Email { get; set; }
            public string TelCasa { get; set; }
            public string TelCelular { get; set; }
            public bool DeviceAny { get; set; }
            public string DeviceSerial { get; set; }
            public DateTime Lastaccess { get; set; }
            public bool Estatus { get; set; }    
        }
    Last edited by Daniil; Mar 10, 2015 at 11:34 AM. Reason: [CLOSED]
  2. #2
    Hello,

    As far as I could understand, you already have the 'sort' string on your data gathering method, right?..

    As I can see, you always get the full user list from database so, you have two options for sorting:

    a) set an ORDER BY on your database select statement. This requires that all sortable columns on your grid are returned on your select statement.

    For example, if you have a column with values 0, 1, 2, to represent an enum MyEnum.Store, MyEnum.House, MyEnum.Apartment and you need to sort by the enum string, you can't rely on database select to order this column. You'll need to translate it to your enums while sorting.

    b) Use C# commands to sort. This is not Ext.NET-related. You can sort your list, for example, using Linq and .NET's implementations of sort, or building your own sort algorythm if you need something too specialized. To improve performance, you could sort you list after you run the remove (to remove whatever the filter excluded). But you must sort before you trim the desired range!

    If you are unsure how to sort your list of customers using c#, what I can do to help you is suggest reading Microsoft's MSDN manuals about Sort(), OrderBy(), and Linq.

    These links might be useful for you to achieve this second approach to sorting a list:
    - orderby clause (entity framework - sql)
    - Enumerable.OrderBy<TSource, TKey> Method
    - List<T>.Sort Method
    - How to Sort a List<T> by a property in the object

    Well, I hope this helps!
    Last edited by fabricio.murta; Feb 28, 2015 at 11:15 PM.
    Fabrício Murta
    Developer & Support Expert
  3. #3
    <!DOCTYPE html>
    <html>
    <head runat="server">
    </head>
    <body>
        <ext:ResourceManager ScriptMode="Debug" runat="server" />
        <ext:GridPanel Title="Ext.Net" Border="true" Width="500" Height="300" runat="server">
            <Store>
                <ext:Store PageSize="10" RemoteSort="true" AutoLoad="true" runat="server">
                    <Sorters>
                        <ext:DataSorter Property="ID" Direction="DESC" />
                    </Sorters>
                    <Proxy>
                        <ext:AjaxProxy Url="~/Example/LoadFakeRecords/" StartParam="start" LimitParam="limit" PageParam="page" SortParam="sort">
                            <Reader>
                                <ext:JsonReader RootProperty="data" />
                            </Reader>
                        </ext:AjaxProxy>
                    </Proxy>
                    <Model>
                        <ext:Model IDProperty="ID" runat="server">
                            <Fields>
                                <ext:ModelField Name="ID" Type="Int" />
                                <ext:ModelField Name="Name" Type="String" />
                            </Fields>
                        </ext:Model>
                    </Model>
                </ext:Store>
            </Store>
            <ColumnModel runat="server">
                <Columns>
                    <ext:Column Text="ID" DataIndex="ID" runat="server" />
                    <ext:Column Text="Name" Flex="1" DataIndex="Name" runat="server" />
                </Columns>
            </ColumnModel>
        </ext:GridPanel>
    </body>
    </html>
    
    namespace SandBox.Controllers
    {
        public class ExampleController : System.Web.Mvc.Controller
        {
            public ActionResult Index()
            {
                return View();
            }
    
            public StoreResult LoadFakeRecords(int start, int limit, int page, string sort)
            {
                List<Entity> lst = new List<Entity>();
    
                for (int index = start; index < (page * limit); index++)
                {
                    lst.Add(new Entity
                    {
                        ID = index,
                        Name = string.Format("Name - {0}", index)
                    });
                }
    
                lst = Sort(lst, sort);
    
                return new StoreResult(lst, (page * limit) + limit);
            }
    
            public List<Entity> Sort(List<Entity> lst, string sort)
            {
                DataSorter sortDefinition = DataSorter.From(sort).FirstOrDefault();
    
                var parameter = Expression.Parameter(typeof(Entity));
    
                Func<Entity, Object> function = Expression.Lambda<Func<Entity, IComparable>>(Expression.Convert(Expression.Property(parameter, sortDefinition.Property), typeof(IComparable)), parameter).Compile();
    
                if (sortDefinition.Direction == SortDirection.ASC)
                {
                    return lst.OrderBy(function).ToList();
                }
                else
                {
                    return lst.OrderByDescending(function).ToList();
                }
            }
        }
    
        [Serializable]
        public class Entity
        {
            public int ID { get; set; }
    
            public string Name { get; set; }
        }
    }

Similar Threads

  1. [CLOSED] [#699] Disable remote sort and sort locally
    By RCN in forum 3.x Legacy Premium Help
    Replies: 10
    Last Post: Feb 13, 2015, 11:10 AM
  2. [CLOSED] Remote sort with LinqDataSource not working
    By PhilG in forum 1.x Legacy Premium Help
    Replies: 8
    Last Post: Mar 06, 2012, 3:15 PM
  3. Replies: 4
    Last Post: Jul 25, 2011, 4:57 PM
  4. [CLOSED] remote sort gird use anthor column
    By lonely7345 in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Mar 11, 2011, 1:26 AM
  5. Replies: 0
    Last Post: Mar 01, 2011, 12:09 PM

Tags for this Thread

Posting Permissions