public JsonResult GetListAccountShort()
        {
            try
            {
                int    length         = int.Parse(HttpContext.Request["length"]);
                int    start          = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(int.Parse(Request["start"]) / length))) + 1;
                string searchValue    = HttpContext.Request["search[value]"];
                string sortColumnName = HttpContext.Request["columns[" + Request["order[0][column]"] + "][name]"];
                // search theo role
                string searchRoleValue = HttpContext.Request["columns[5][search][value]"];
                string sortDirection   = Request["order[0][dir]"];

                AccountPaging apg = new AccountPaging();
                apg.data = new List <AccountShort>();
                start    = (start - 1) * length;
                List <Account> listAccount = db.Accounts.ToList <Account>();
                apg.recordsTotal = listAccount.Count;
                //filter
                // search theo Role
                if (!string.IsNullOrEmpty(searchRoleValue))
                {
                    listAccount = listAccount.Where(x
                                                    => x.RoleAccount.Role.ToLower().Equals(searchRoleValue.ToLower())
                                                    ).ToList <Account>();
                }
                // search total
                if (!string.IsNullOrEmpty(searchValue))
                {
                    listAccount = listAccount.Where(x => x.Username.ToLower().Contains(searchValue.ToLower()) ||
                                                    x.Email.ToLower().Contains(searchValue.ToLower()) ||
                                                    x.Fullname.ToLower().Contains(searchValue.ToLower()) ||
                                                    x.Phone.ToLower().Contains(searchValue.ToLower()) ||
                                                    x.RoleAccount.Role.ToLower().Contains(searchValue.ToLower())
                                                    ).ToList <Account>();
                }
                //sorting
                if (sortColumnName.Equals("Role"))
                {
                    sortColumnName = "RoleID";
                }
                listAccount = listAccount.OrderBy(sortColumnName + " " + sortDirection).ToList <Account>();
                //}
                apg.recordsFiltered = listAccount.Count;
                //paging
                listAccount = listAccount.Skip(start).Take(length).ToList <Account>();
                Dictionary <string, string> Role_Color = StatusContext.GetColorForRole();
                int count = StatusContext.Color.Count;
                for (int i = 0; i < listAccount.Count; i++)
                {
                    AccountShort acs = new AccountShort
                    {
                        ID             = listAccount[i].ID,
                        Username       = listAccount[i].Username,
                        Fullname       = listAccount[i].Fullname,
                        Phone          = listAccount[i].Phone,
                        Email          = listAccount[i].Email,
                        Role           = listAccount[i].RoleAccount.Role,
                        RoleColorClass = Role_Color[listAccount[i].RoleAccount.Role],
                        Avatar         = listAccount[i].Avatar,
                        Actions        = ""
                    };
                    apg.data.Add(acs);
                }
                apg.draw = int.Parse(Request["draw"]);
                return(Json(apg));
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        public JsonResult getAllUser()
        {
            try
            {
                int    length         = int.Parse(Request.Query["length"]);
                int    start          = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(int.Parse(Request.Query["start"]) / length))) + 1;
                string searchValue    = Request.Query["search[value]"];
                string sortColumnName = Request.Query["columns[" + Request.Query["order[0][column]"] + "][name]"];
                string sortDirection  = Request.Query["order[0][dir]"];


                AccountPaging apg = new AccountPaging();
                apg.data = new List <AccountShow>();
                start    = (start - 1) * length;
                List <Account> listAccount = _context.Accounts.ToList <Account>();
                apg.recordsTotal = listAccount.Count;
                //filter
                if (!string.IsNullOrEmpty(searchValue))
                {
                    listAccount = listAccount.Where(x => x.UserName.ToLower().Contains(searchValue.ToLower()) ||
                                                    x.Email.ToLower().Contains(searchValue.ToLower()) ||
                                                    x.FullName.ToLower().Contains(searchValue.ToLower()) ||
                                                    x.PhoneNumber.ToLower().Contains(searchValue.ToLower())
                                                    ).ToList <Account>();
                }
                //sorting

                if (sortDirection == "asc")
                {
                    listAccount = listAccount.OrderBy(x => x.GetType().GetProperty(sortColumnName).GetValue(x)).ToList <Account>();
                }
                else
                {
                    listAccount = listAccount.OrderByDescending(x => x.GetType().GetProperty(sortColumnName).GetValue(x)).ToList <Account>();
                }
                apg.recordsFiltered = listAccount.Count;
                //paging
                listAccount = listAccount.Skip(start).Take(length).ToList <Account>();
                apg.data    = new List <AccountShow>();
                for (int i = 0; i < listAccount.Count(); i++)
                {
                    AccountShow acs = new AccountShow
                    {
                        UserName    = listAccount[i].UserName,
                        FullName    = listAccount[i].FullName,
                        PhoneNumber = listAccount[i].PhoneNumber,
                        Email       = listAccount[i].Email,
                        DOB         = listAccount[i].DOB,
                        Active      = listAccount[i].Active,
                        Role        = string.Join(",", _context.Account_Roles.Where(x => x.UserName == listAccount[i].UserName).Select(x => x.RoleName).ToList()),
                        ClassCheck  = listAccount[i].Active?"fa-user-check" : "fa-user-lock"
                    };
                    apg.data.Add(acs);
                }
                apg.draw = int.Parse(Request.Query["draw"]);
                return(Json(apg));
            }
            catch (Exception ex)
            {
                return(null);
            }
        }