Friday, December 5, 2014

alphabetical pager in mvc



Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NewDesiging.Models
{
    public class AlphabeticalPagingViewModel
    {
        public List<string> ProductNames { get; set; }
        public List<string> Alphabet
        {
            get
            {
                var alphabet = Enumerable.Range(65, 26).Select(i => ((char)i).ToString()).ToList();
                alphabet.Insert(0, "All");
                alphabet.Insert(1, "0-9");
                return alphabet;
            }
        }
        public List<string> FirstLetters { get; set; }
        public string SelectedLetter { get; set; }
        public bool NamesStartWithNumbers
        {
            get
            {
                var numbers = Enumerable.Range(0, 10).Select(i => i.ToString());
                return FirstLetters.Intersect(numbers).Any();
            }
        }
    }
    //public class AlphabeticalPagingViewModel
    //{
    //    public List<string> ProductNames { get; set; }
    //    public List<string> FirstLetters { get; set; }
    //    public string SelectedLetter { get; set; }
    //}
}

Html helpers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace NewDesiging.Models
{
    public static class HtmlHelpers
    {
        public static HtmlString AlphabeticalPager(this HtmlHelper html, string selectedLetter, IEnumerable<string> firstLetters, Func<string, string> pageLink)
        {
            var sb = new StringBuilder();
            var numbers = Enumerable.Range(0, 10).Select(i => i.ToString());
            var alphabet = Enumerable.Range(65, 26).Select(i => ((char)i).ToString()).ToList();
            alphabet.Insert(0, "All");
            alphabet.Insert(1, "0-9");

            var ul = new TagBuilder("ul");
            ul.AddCssClass("pagination");
            ul.AddCssClass("alpha");

            foreach (var letter in alphabet)
            {
                var li = new TagBuilder("li");
                if ( letter == "All")
                {
                    if (selectedLetter == letter || selectedLetter== null && letter == "All")
                    {
                        li.AddCssClass("active");
                        var span = new TagBuilder("span");
                        span.SetInnerText(letter);
                        li.InnerHtml = span.ToString();
                    }
                    else
                    {
                        var a = new TagBuilder("a");
                        a.MergeAttribute("href", pageLink(letter));
                        a.InnerHtml = letter;
                        li.InnerHtml = a.ToString();
                        a.AddCssClass("active");
                        //li.AddCssClass("active");
                    }
                }
                 else if(selectedLetter==letter)
                {
                    li.AddCssClass("active");
                    var span = new TagBuilder("span");
                    span.SetInnerText(letter);
                    li.InnerHtml = span.ToString();
                    //var span = new TagBuilder("span");
                    //span.SetInnerText(letter);
                    //li.InnerHtml = span.ToString();
                }
                else
                {
                    var a = new TagBuilder("a");
                    //li.AddCssClass("inactive");
                    a.AddCssClass("active");
                    a.MergeAttribute("href", pageLink(letter));
                    a.InnerHtml = letter;
                  
                    li.InnerHtml = a.ToString();
                    //var span = new TagBuilder("span");
                    //span.SetInnerText(letter);
                    //li.InnerHtml = span.ToString();
                }
                sb.Append(li.ToString());
            }
            ul.InnerHtml = sb.ToString();
            return new HtmlString(ul.ToString());
        }
    }
}
Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NewDesiging.Models;

namespace NewDesiging.Controllers
{
    public class SearchController : Controller
    {
        // GET: Search
        //public ActionResult Index()
        //{
        //    return View();
        //}
        public ActionResult Index(string selectedLetter)
        {
            var model = new AlphabeticalPagingViewModel { SelectedLetter = selectedLetter };
            using (var context = new NareshDb())
            {
                model.FirstLetters = context.Jobs
                    .GroupBy(p => p.Title.Substring(0, 1))
                    .Select(x => x.Key.ToUpper()).Take(100)
                    .ToList();

                if (string.IsNullOrEmpty(selectedLetter) || selectedLetter == "All")
                {
                    model.ProductNames = context.Jobs
                        .Select(p => p.Title).Take(100)
                        .ToList();
                }
                else
                {
                    if (selectedLetter == "0-9")
                    {
                        var numbers = Enumerable.Range(0, 10).Select(i => i.ToString());
                        model.ProductNames = context.Jobs
                            .Where(p => numbers.Contains(p.Title.Substring(0, 1)))
                            .Select(p => p.Title).Take(100)
                            .ToList();
                    }
                    else
                    {
                        model.ProductNames = context.Jobs
                            .Where(p => p.Title.StartsWith(selectedLetter))
                            .Select(p => p.Title).Take(100)
                            .ToList();
                    }
                }
            }
            return View(model);
        }
    }
}
View
@using NewDesiging.Models
@model NewDesiging.Models.AlphabeticalPagingViewModel

@*<ul class="pagination alpha">
    @foreach (var link in Model.Alphabet)
    {
        if (Model.FirstLetters.Contains(link) || (Model.NamesStartWithNumbers && link == "0-9") || link == "All")
        {
            if (link == "All")
            {
                <li class="active"><span>@link</span></li>
            }
            else
            {
                <li>@Html.ActionLink(link, "Index", new { selectedLetter = link })</li>
            }
        }
        else
        {
            <li class="inactive"><span>@link</span></li>
        }
    }
</ul>
<ul>
  

    @foreach (var item in Model.ProductNames)
    {
      
            <li>@item</li>
           
        }
    }


    @foreach (var item in Model.ProductNames)
        {
            <li>@item</li>
        }
    </ul>*@
@Html.AlphabeticalPager(Model.SelectedLetter, Model.FirstLetters, x =>Url.Action("Index", new { selectedLetter = x }))
<ul>
  
   
    @foreach (var item in Model.ProductNames)
    {
       
                                 <li>@item</li>
                                   
                                    
    }
  
</ul>