This is a migrated thread and some comments may be shown as answers.

[Solved] Lite grid. Is it possible?

1 Answer 128 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
cheburek
Top achievements
Rank 1
cheburek asked on 05 Oct 2011, 01:35 PM
Hello

I like telerik controls for their wrappers for VS and powerful controls.
Grids I use on my controls use data bounding on server and most of themdo not use any javascript functionality - simple tables without sorting, filtering etc. Most of them even do not have paging.

But for such simple tables if you look to html source always generated javascript initialization which includes a lot of information, some localization names (even for invisible columns) so on and of cource include several javascript files.

Is it possible to set something like "lite mode" - generate grid without all those javascript bunch?

1 Answer, 1 is accepted

Sort by
0
cheburek
Top achievements
Rank 1
answered on 09 Oct 2011, 01:43 PM
Yes, it's possible :)
I put the solution below for those who are interested in it.
You must override WriteHtml and WriteInitializationScript methods in Grid class and then add ability to call it.
After you use current solution you have to use  Html.CustomTelerik().Grid() to access your new grid implementation

using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using System.Web.UI;
using Telerik.Web.Mvc;
using Telerik.Web.Mvc.Infrastructure;
using Telerik.Web.Mvc.UI;
using Telerik.Web.Mvc.UI.Fluent;
using Telerik.Web.Mvc.UI.Html;

namespace
CustomTelerik
{
  
  /// <summary>
  /// If grid is readonly then do not register any javacript
  /// </summary>
  public class EfficientGrid<T> : Grid<T> where T : class
  {
    public EfficientGrid( ViewContext viewContext, 
      IClientSideObjectWriterFactory clientSideObjectWriterFactory, 
      IUrlGenerator urlGenerator, 
      ILocalizationService localizationService, 
      IGridHtmlBuilderFactory htmlBuilderFactory ) : 
      base( viewContext, clientSideObjectWriterFactory, 
      urlGenerator, localizationService, htmlBuilderFactory )
    {
      //save default script file names 
      DefaultScriptFileNames = ScriptFileNames.ToArray();
    }
  
    private readonly IEnumerable<string> DefaultScriptFileNames;
    private bool IsJsFree()
    {
      return ScriptFileNames.Count <= 2 && Sorting.Enabled==false;
    }
  
  
    protected override void WriteHtml( HtmlTextWriter writer )
    {
      //do not register default files if grid is readonly
      RegisterScriptFiles();
      var isfree = IsJsFree();
      ScriptFileNames.Clear();
      if ( !isfree ) {
        foreach ( var df in DefaultScriptFileNames ) 
          ScriptFileNames.Add( df ); 
      }
  
      base.WriteHtml( writer );
    }
  
      
    public override void WriteInitializationScript(TextWriter writer)
    {
      if ( !IsJsFree())
        base.WriteInitializationScript( writer );
    }
  
  }//class  
    
  /// <summary>
  /// inherit ViewComponentFactory class for EfficientGrid
  /// </summary>
  public class CustomViewComponentFactory : ViewComponentFactory
  {
    public CustomViewComponentFactory(ViewComponentFactory vcf)
      : base( vcf.HtmlHelper, vcf.ClientSideObjectWriterFactory, vcf.StyleSheetRegistrar(), vcf.ScriptRegistrar() ){}
  
    public CustomViewComponentFactory(HtmlHelper htmlHelper, 
      IClientSideObjectWriterFactory clientSideObjectWriterFactory, 
      StyleSheetRegistrarBuilder styleSheetRegistrar, 
      ScriptRegistrarBuilder scriptRegistrar ) : 
      base( htmlHelper, clientSideObjectWriterFactory, styleSheetRegistrar, scriptRegistrar ){}
  
  
    public override GridBuilder<T> Grid<T>()
    {
      return GridBuilder<T>.Create( Register( () => new EfficientGrid<T>( HtmlHelper.ViewContext,
                       ClientSideObjectWriterFactory,
                       DI.Current.Resolve<IUrlGenerator>(),
                       DI.Current.Resolve<ILocalizationServiceFactory>().Create( "GridLocalization", CultureInfo.CurrentUICulture ),
                       DI.Current.Resolve<IGridHtmlBuilderFactory>()
                   )
               )
           );
    }
  }
  
  /// <summary>
  /// Html extension for views
  /// </summary>  
  public static class CustomTelerikExtensions
  {
    public static ViewComponentFactory CustomTelerik(this HtmlHelper helper)
    {
      return new CustomViewComponentFactory( helper.Telerik() );
    }
  }
    
}//namespace

Hope this help somebody
Tags
Grid
Asked by
cheburek
Top achievements
Rank 1
Answers by
cheburek
Top achievements
Rank 1
Share this question
or