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

Trouble getting grid in javascript

2 Answers 167 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.
Pierre-Andre van Leeuwen
Top achievements
Rank 1
Pierre-Andre van Leeuwen asked on 17 Feb 2010, 02:38 PM
Hi

I have a problem getting the following to work: 
var grid = $('#CurrentActivitiesGrid').data('tGrid');  
or    
var grid = $(this).data('tGrid');  <-- in event handler  
 
 
 

 

In both cases, the grid is undefined. $('#CurrentActivitiesGrid') is shows up as an object in the debugger and this as a  DispHTMLDivElement.

The grid works fine when using Ajax binding, but I want to call dataBind(). Grid is defined as follows:

 

<%=Html.Telerik().Grid<PlanActivity>()  
        .Name("CurrentActivitiesGrid")  
        .Columns(columns =>  
          {  
            columns.Add(a => a.ActivityId).Format(this.CheckBox("Select")  
              .Id("_selectActivity_{0}").ToString()).Encoded(false).Title("Select");  
            columns.Add(a => a.ActivityName).Title("Name");  
            columns.Add(a => a.ActivityId)  
              .Format(this.Button("Remove").Id("_removeActivity_{0}").OnClick("RemoveActivity({0})").ToString())  
              .Title("Remove");  
          })  
        .ClientEvents(events=>events.OnError("onGridError"))  
        .ClientEvents(events => events.OnLoad("onGridLoad"))  
    %> 

Any ideas?

Pierre-Andre


 

2 Answers, 1 is accepted

Sort by
0
Pierre-Andre van Leeuwen
Top achievements
Rank 1
answered on 17 Feb 2010, 03:21 PM
I put a breakpoint in the telerik.grid.js file where the tGrid property is is, and I see now that I have a timing issue here. I believe this is a general issue and not limited to the grid and caused by my limited understanding of how and when javascript should be included.

I currently include the standard scripts (MicrosoftAjax.js. jquery-1.4.js) and my common.js included in the head of the site.master. I have the Telerik ScriptRegistrar at the end of the site.master. The grid is used on a user control (ascx) that I load as a partial view.

In the user control, I include a javascript file with scripts used in the user control. I don't really know where this script block should go: I have it at the end of the ascx atm. This script file contains the following:

 


$(document).ready(

function() {

 

    var grid = $('#CurrentActivitiesGrid').data('tGrid'); 
}
This code executes before the code in the telerik.grid.js file that sets up the grid. I can see that the telerik ScripRegistrar contains a facility to add document ready handlers, but does this mean I have to move the ScripRegistrat to the user control? We use the Telerik mvc components on a variety of pages and user controls, and it would be a pain to include it everywhere. I'm not sure what happens when you have the registrar in more that one user control that is rendered on the same page, but I assume it will complain about having been rendered more that once.

Can someone tell me how javascript includes should be done?

Thanks
Pierre-Andre
0
Atanas Korchev
Telerik team
answered on 17 Feb 2010, 04:19 PM
Hi Pierre-Andre van Leeuwen,

The grid is initialized during document.ready which is output by the ScriptRegistrar. Ideally you can register your JavaScript file with the ScriptRegistrar. If you want to use document.ready make sure it is output after the ScriptRegistrar otherwise the grid will not be initialized. Alternatively you can handle the OnLoad client-side event of the grid and call your code there.

Regards,
Atanas Korchev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Pierre-Andre van Leeuwen
Top achievements
Rank 1
Answers by
Pierre-Andre van Leeuwen
Top achievements
Rank 1
Atanas Korchev
Telerik team
Share this question
or