Uncaught TypeError: Cannot read property 'lastChild' of undefined

Good day,

I have a schedule in which I display events, but I only want to show the schedule after certain steps have been performed.  When I set the display: "none" in css, it gives me the Uncaught TypeError: Cannot read property 'lastChild' of undefined.  I also tried using multiple tabs, but I receive the same error when the schedule is not in the first tab.  I also wondered if it is possible to dynamically create the schedule in javascript.  Please advise me on the matter.

Regards.

7 Replies

VD Vinitha Devi Murugan Syncfusion Team August 6, 2018 02:42 PM UTC

Hi Rikard,  
 
Thanks for contacting syncfusion support. 

The reported problem with “Uncaught TypeError: Cannot read property ‘lastchild’ of undefined” while rendering the schedule dynamically within tab was due to the live update of time in the scheduler when “showTimeIndicator property is set to true. Scheduler continuously updates the live current system time appropriately on its user interface at a regular interval and therefore, when we initially hide the schedule element - the query selector element gets returned as empty which is the cause of this problem. To overcome this, we recommended two options to follow – either you can off the showTimeIndicator property by setting showTimeIndicator = ’false’ or else if you need showTimeIndicator property, then make use of the tab control’s client-side events ‘created’ and ‘selecting’ to hide and show the schedule dynamically on its initial rendering. We have prepared the sample with tab events, which can be downloaded from the following location. 
 

<code> 
@using Syncfusion.EJ2.Grids 
@using Syncfusion.EJ2.Schedule 
@using Syncfusion.EJ2.Navigations; 
@{ 
    ViewData["Title"] = "Home Page"; 
 
    var filterSettings = new GridFilterSettings { Type = FilterType.Menu }; 
    var tabHeaderOne = new TabHeader { Text = "Tab One" }; 
    var tabHeaderTWo = new TabHeader { Text = "Tab Two" }; 
    var tabHeaderThree = new TabHeader { Text = "Tab Three" }; 
} 
 
 
<div id="SampleGrid1" style="display:none"> 
    <br/> 
    <br /> 
    <br /> 
 
    <ejs-grid id="Grid"> 
        <e-datamanager url="http://js.syncfusion.com/ejServices/Wcf/Northwind.svc/Orders/?$top=7" adaptor="ODataAdaptor" crossdomain="true"></e-datamanager> 
        <e-grid-columns> 
            <e-grid-column field="OrderID" headerText="Order ID" type="number" textAlign="Right" width="120"></e-grid-column> 
            <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="140"></e-grid-column> 
            <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" width="120"></e-grid-column> 
            <e-grid-column field="OrderDate" headerText="Order Date" format='yMd' textAlign="Right" width="140"></e-grid-column> 
        </e-grid-columns> 
    </ejs-grid> 
</div> 
 
<div id="SampleSchedule" > 
    <br /> 
    <br /> 
    <br /> 
    <ejs-schedule id="Schedule1" selectedDate="DateTime.Now" > </ejs-schedule> 
</div> 
 
<div id="elementThree" style="display:none"> 
    <br /> 
    <br /> 
    <br /> 
     
    <ejs-button id="elementThree" content="ButtonThree"></ejs-button> 
 
</div> 
 
 
 
 
<div style="height:600px"> 
    <div class="e-sample-resize-container"> 
 
        <ejs-tab id="ej2Tab" created="tabCreated" selecting="tabSelecting"> 
            <e-tab-tabitems> 
                <e-tab-tabitem header="tabHeaderOne" content="#SampleGrid1"></e-tab-tabitem> 
                <e-tab-tabitem header="tabHeaderTWo" content="#SampleSchedule"></e-tab-tabitem> 
                <e-tab-tabitem header="tabHeaderThree" content="#elementThree"></e-tab-tabitem> 
            </e-tab-tabitems> 
        </ejs-tab> 
    </div> 
</div> 
 
<script> 
    function tabCreated() { 
        document.getElementById("Schedule1").style.display = 'none'; 
    } 
    function tabSelecting() { 
        document.getElementById("Schedule1").style.display = 'block'; 
    } 
 
</script> 

</code> 

Kindly make use of any of the above suggested ways and let us know, if it resolves your problem and also, if you need any further assistance on this. 

Regards,  
M. Vinitha devi. 
 



RS Rikard Schouwstra August 7, 2018 10:23 AM UTC

Thank you very much, it did solve my problem. I would just like to know how to dynamically change the datasource of the schedule if it is possible.


VD Vinitha Devi Murugan Syncfusion Team August 7, 2018 03:32 PM UTC

Hi Mariusz,  
 
Yes it is possible to dynamically change the datasource of the schedule as given in the below code example. 

<code> 

var scheduleObj = document.getElementById('Schedule1').ej2_instances[0];       // Create object to schedule 
scheduleObj.eventSettings.dataSource = [ {Id = 4,                                                  // set the datasource to schedule 
                                                                     Subject = "Time-off in lieu", 
                                                                     StartTime = new DateTime(2018, 2, 13, 9, 0, 0), 
                                                                      EndTime = new DateTime(2018, 2, 13, 11, 0, 0)  
                                                                     } 
                                                                    ];    
 scheduleObj.dataBind();  

</code> 

Please try the above code and let us know if you need any further assistance on this. 

Regards,  
M. Vinitha devi. 
 



RS Rikard Schouwstra August 8, 2018 01:15 PM UTC

Great thanks, is it also possible to change the datasource of a grid dynamically, with jQuery.ajax() to a new ViewBag instance?



VD Vinitha Devi Murugan Syncfusion Team August 9, 2018 09:41 AM UTC

Hi Rikard,  
  
We can dynamically set the Essential JavaScript 2 Grid dataSource with jquery ajax() call. But you need to return the JSON data from the server action. We have prepared a simple sample based on your query in which we have dynamically set the grid.dataSource with jquery ajax success function. Please refer to the below code example and sample link.  
 
[index.cshtml]  
 
<ejs-button id="change" content="Change DataSource" isPrimary="true"></ejs-button>  
<ejs-grid id="Grid" dataSource="ViewBag.dataSource" actionFailure="fail">  
    .  .  .  
</ejs-grid>  
  
<script>  
    document.getElementById('change').addEventListener('click', () => {  
         
         $.ajax({  
             url: '@Url.Action("GetData", "Home")',  
             type: 'GET',  
             success: function (data) {  
                 var grid = document.getElementById("Grid").ej2_instances[0];  
                 grid.dataSource = data;  
             },  
             error: function () { alert("error"); }  
             });         
  
    });  
</script>  
  
  
[controller.cs]  
public ActionResult GetData()  
        {  
            var employee = Employee1Details.GetAllRecords();  
            var data = employee.ToList();  
            return Json(data);  
        }  
  
 
Regards, 
M.Vinitha devi 




LH line hammer May 29, 2019 06:06 AM UTC

This error occurs in Chrome Browser when you read a property or call a method on an undefined object . Uncaught TypeError: Cannot read property of undefined  error is probably easiest to understand from the perspective of undefined, since undefined is not considered an object type at all (but its own undefined type instead), and properties can only belong to objects within JavaScript. There are a few variations of this error depending on the property you are trying to access. Sometimes instead of undefined it will say null.




KK Karthigeyan Krishnamurthi Syncfusion Team May 30, 2019 04:48 AM UTC

Thanks for sharing the information 😊 


Loader.
Up arrow icon