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

[Solved] apply a common javascript function to all grids

4 Answers 133 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mandar Bansod
Top achievements
Rank 1
Mandar Bansod asked on 02 Mar 2012, 02:25 PM
Hi,
I am working on an application which uses telerik mvc grids extensively. In each of the grid I have a column 'IsActive' which contains a boolean value. I want all the rows with IsActive = false should be highlighted with some color.

I could write a javascript which can execute on rowDataBound() event of the grid. But for the purpose I need to dig into the application, locate each & every grid & add that client event to the grid markup.

I don't want to do that as there are more than 200 grids in application & also its a bit time consuming & error prone.

Is there any ways where in I would write a code at single place & it will apply to all of the grids across my application ?

Thanks for you help.

4 Answers, 1 is accepted

Sort by
0
Dadv
Top achievements
Rank 1
answered on 02 Mar 2012, 03:21 PM
Hi,

Do you try an inheritance ?

make all your models inherit from an interface and change the Grid declaration by a CustomGrid like this :

public interface IIsActive
   {
       public bool IsActive { get; set; }
   }
 
   public class CustomGrid<T> : Grid<T> where T : class, IIsActive
   {
       public CustomGrid(ViewContext viewContext, IClientSideObjectWriterFactory clientSideObjectWriterFactory, IUrlGenerator urlGenerator, ILocalizationService localizationService, IGridHtmlBuilderFactory htmlBuilderFactory)  : base(viewContext, clientSideObjectWriterFactory, urlGenerator, localizationService, htmlBuilderFactory)
       {
           base.RowAction = row =>
           {
               if (!row.DataItem.IsActive)
               {
                   row.HtmlAttributes["class"] = "highlight";
               }
           };
       }
   }
0
Dadv
Top achievements
Rank 1
answered on 02 Mar 2012, 03:55 PM
Forget it, it only work if you do it in the telerik mvc source code...
0
Accepted
Dadv
Top achievements
Rank 1
answered on 02 Mar 2012, 04:32 PM
I found an other way :

public static GridBuilder<T> Highlight<T>(this GridBuilder<T> builder) where T : class,IIsActive
       {
 
           var grid = builder.ToComponent();
           if (grid.IsClientBinding)
           {
               grid.ClientEvents.OnRowDataBound.HandlerName = "onRowDataBound";
           }
           else
           {
               grid.RowAction = row =>
               {
                   if (!row.DataItem.IsActive)
                   {
                       row.HtmlAttributes["class"] = "onRowDataBound";
                   }
               };
           }
 
 
           return builder;
       }


function onRowDataBound(e) {
    if (!e.dataItem.IsActive) {
        e.row.className = 'Hightligth';
    }
}


you just have to had the .Highlight () method in yours grids. I try it and it work.
0
Mandar Bansod
Top achievements
Rank 1
answered on 09 Mar 2012, 02:04 PM
Hi Davd

The code you provided worked for me, just needed to make some changes in your code & its working fine at my end. Thanks for the kind help.
Tags
General Discussions
Asked by
Mandar Bansod
Top achievements
Rank 1
Answers by
Dadv
Top achievements
Rank 1
Mandar Bansod
Top achievements
Rank 1
Share this question
or