Grid datasource not binding the data after new table entries are inserted.

Description:

I have a form where user enters some information in the text fields and when they click on add button I am trying to add a new row in the grid below.
The new row that's added to the grid is not appearing in the grid.

Note: 
1) The gridData has entries but the grid seems to be not binding the data.
2) We don't want to send the data to the API and then bind the grid instead we want multiple entries to be inserted and displayed in the grid at the client side before posting.
3) I am able to bind data on pageload onInit event but not on the button click event.


app.component.html 

<div class="row">
<div class="col-xs-2 col-sm-2 col-lg-2 col-md-2 search-item">
<ejs-datepicker id="appDate" name="appDate" [(ngModel)]="model.Date" #appDate="ngModel" width='100%' placeholder='Date'
floatLabelType='Auto'>ejs-datepicker>
div>
<div class="col-xs-2 col-sm-2 col-lg-2 col-md-2 search-item">
<ejs-timepicker #startTime id="startTime" name="startTime" [(ngModel)]="model.StartTime" #startTime="ngModel" width='100%'
placeholder='Start Time' floatLabelType='Auto' (change)='onEnableEndTime($event)'>ejs-timepicker>
div>
<div class="col-xs-2 col-sm-2 col-lg-2 col-md-2 search-item">
<ejs-timepicker #endTime id="endTime" name="endTime" [(ngModel)]="model.EndTime" #endTime="ngModel" width='100%'
placeholder='End Time' floatLabelType='Auto'>ejs-timepicker>
div>
<div class="col-xs-3 col-sm-3 col-lg-3 col-md-3 search-item">
<ejs-dropdownlist id="language" name="language" [(ngModel)]="model.Language" #language="ngModel" width="100%" [dataSource]='languages'
[fields]='languageFields' [allowFiltering]='true' floatLabelType='Auto' placeholder='Language'>ejs-dropdownlist>
div>
<div class="col-xs-3 col-sm-3 col-lg-3 col-md-3 search-item">
<label class="e-float-text">Gender
<span class="text-danger">*span>
label>
<div class="row radioPadding">
<ejs-radiobutton label="Male" value="Male" name="male" [(ngModel)]="model.Gender" #tranGender="ngModel">ejs-radiobutton>   
<ejs-radiobutton label="Female" value="Female" name="female" [(ngModel)]="model.Gender" #tranGender="ngModel">ejs-radiobutton>   
<ejs-radiobutton label="Either" value="Either" name="either" [(ngModel)]="model.Gender" #tranGender="ngModel">ejs-radiobutton>   
div>
div>
div>

<hr style="border-bottom: 1px solid black;margin-top: 5px !important; margin-bottom: 10px !important;">
<button type="button" class='e-btn e-success pull-right' (click)='AddAppointment()'>
<i class="fa fa-plus">i> Addbutton>
<br/>
<ejs-grid [dataSource]='gridData' [allowPaging]="true" [allowSorting]="true" [pageSettings]='pageSettings'>
<e-columns>
<e-column field='Date' headerText='Date' format='Mdy' width=60>e-column>
<e-column field='StartTime' headerText='Start Time' width=70>e-column>
<e-column field='EndTime' headerText='End Time' width=70>e-column>
<e-column field='Language' headerText='Language' width=50>e-column>
<e-column field='Gender' headerText='Gender' width=80>e-column>
e-columns>
ejs-grid>

app.component.ts

public model: IRAppointment;
public gridData: IRAppointment[];

public ngOnInit(): void {
this.gridData = this.gridData;
}

public AddAppointment(): void {

if (!isNOU(this.model)) {
this.gridData.push(this.model);
this.model= new IRAppointment;
}
}

Array looks like this after pushing the data from model:

  1. Array(1)
    1. 0:gridData
      1. ContactPerson:"Person1"
      2. Language:"Hindi"
      3. TimeZone:"Mountain Time Zone"
      4. Gender:"Female"


12 Replies

VA Venkatesh Ayothi Raman Syncfusion Team June 29, 2018 12:03 PM UTC

Hi Gaviyaiah, 
 
 
Thanks for using Syncfusion products. 
 
As from your query, we suspect that do you want add the multiple records in grid view(Client side) as well as the corresponding request will not sent to server side for adding the records.  If so, we have achieved your requirement ‘Adding the data into Grid view without send the single request to server side’ using actionBegin event and dataManagerSuccess function in Grid. Please refer to the following code example,  
  
[actionBegin event] 
actionBegin = (args:any) => {  
        if (args.requestType == 'save' && args.action == 'add') {  
            args.cancel = true; //cancel normal add operation  
            //here you can send the post request to server side using AJAX  
             
            this.grid.editModule.closeEdit(); //cancel the edit  
            bulkAdd.push(args.data);//store the added records  
              
            this.grid.getCurrentViewRecords().unshift(args.data); //push the data to grid view 
  
            gridObj.renderModule.dataManagerSuccess({ result: gridObj.getCurrentViewRecords(), count: gridObj.getCurrentViewRecords().length });  
        }  
    }  
  
  

After adding the rows, we can send the bulk save request to server side while click the button like as follows,  
  
  
[buttonclick]  
function bulkAdd(args) {  
  
        let ajax:any = new Ajax("/Home/BulkSave""POST"true); // call API  
        ajax.send(JSON.stringify(this.bulkUpdate)).then(  
            (data) => {  
                //do stuff  
                bulkAdd = []; //here empty the bulk add array  
            });  
    }  
  
    [Controller]  
  
    public ActionResult BulkSave([FromBody] List<OrdersDetails> value) {  
        //do stuff  
        return Json(value);  
    }  
  
      

Please let us know if you have any further assistance on this or If we misunderstood your requirement, then could you please provide more information about this requirement? 
 

 
Regards,  
Venkatesh Ayothiraman.  
 



GB gaviyaiah boraiah replied to Venkatesh Ayothi Raman June 29, 2018 12:15 PM UTC

Hi Gaviyaiah, 
 
 
Thanks for using Syncfusion products. 
 
As from your query, we suspect that do you want add the multiple records in grid view(Client side) as well as the corresponding request will not sent to server side for adding the records.  If so, we have achieved your requirement ‘Adding the data into Grid view without send the single request to server side’ using actionBegin event and dataManagerSuccess function in Grid. Please refer to the following code example,  
  
[actionBegin event] 
actionBegin = (args:any) => {  
        if (args.requestType == 'save' && args.action == 'add') {  
            args.cancel = true; //cancel normal add operation  
            //here you can send the post request to server side using AJAX  
             
            this.grid.editModule.closeEdit(); //cancel the edit  
            bulkAdd.push(args.data);//store the added records  
              
            this.grid.getCurrentViewRecords().unshift(args.data); //push the data to grid view 
  
            gridObj.renderModule.dataManagerSuccess({ result: gridObj.getCurrentViewRecords(), count: gridObj.getCurrentViewRecords().length });  
        }  
    }  
  
  

After adding the rows, we can send the bulk save request to server side while click the button like as follows,  
  
  
[buttonclick]  
function bulkAdd(args) {  
  
        let ajax:any = new Ajax("/Home/BulkSave""POST"true); // call API  
        ajax.send(JSON.stringify(this.bulkUpdate)).then(  
            (data) => {  
                //do stuff  
                bulkAdd = []; //here empty the bulk add array  
            });  
    }  
  
    [Controller]  
  
    public ActionResult BulkSave([FromBody] List<OrdersDetails> value) {  
        //do stuff  
        return Json(value);  
    }  
  
      

Please let us know if you have any further assistance on this or If we misunderstood your requirement, then could you please provide more information about this requirement? 
 

 
Regards,  
Venkatesh Ayothiraman.  
 


Hi,

I still don't know how to use the below mentioned event.

[actionBegin event] 
actionBegin = (args:any) => {  
        if (args.requestType == 'save' && args.action == 'add') {  
            args.cancel = true; //cancel normal add operation  
            //here you can send the post request to server side using AJAX  
             
            this.grid.editModule.closeEdit(); //cancel the edit  
            bulkAdd.push(args.data);//store the added records  
              
            this.grid.getCurrentViewRecords().unshift(args.data); //push the data to grid view 
  
            gridObj.renderModule.dataManagerSuccess({ result: gridObj.getCurrentViewRecords(), count: gridObj.getCurrentViewRecords().length });  
        }  
    }  
 

Would it be possible to send the related HTML grid template / code for better understanding.


VA Venkatesh Ayothi Raman Syncfusion Team July 2, 2018 04:20 AM UTC

Hi Gaviyaiah, 


Thanks for the update. 

We can define the Grid events in angular way. Please refer to the following code example, 
[Grid html] 
<ejs-grid #grid [dataSource]='data' (actionBegin)='actionBegin($event)'> 
  <e-columns> 
      . .  . 
  </e-columns> 
</ejs-grid> 
 
 

Please refer to the following Help documentation for more information, 

Please let us know if you have any further assistance on this. 

Regards, 
Venkatesh Ayothiraman. 



GB gaviyaiah boraiah July 2, 2018 10:42 AM UTC

Hi,

Please refer the details and comments added in the Word Document within the attached Zip file.

Attachment: Synfusion_GridBind_Issue_db359ca3.zip


VA Venkatesh Ayothi Raman Syncfusion Team July 3, 2018 01:01 PM UTC

Hi Gaviyaiah, 
 
Thanks for the update. 
 
We went through your word documentation and screenshot that you have shared with us and found that you would like to add a record when user fill the details and click the add button. If so, we suggest you use addRecord API in Grid to perform the add operation. This is the recommended way to add a record dynamically. Please refer to the following Help documentation for more information, 
Code example: 
 
 
addButtonClik(e) {   
            //here you can get the values which is filled by user 
                                                               i.      ContactPerson:"Person1" 
                                                             ii.      Date:Sat Jun 30 2018 00:00:00 GMT+0530 (India Standard Time) {} 
                                                           iii.      EndTime:Thu Jun 28 2018 01:30:00 GMT+0530 (India Standard Time) {} 
                                                           iv.      Language:"Hindi" 
                                                             v.      StartTime:Thu Jun 28 2018 00:00:00 GMT+0530 (India Standard Time) {} 
                                                           vi.      TimeZone:"Mountain Time Zone" 
                                                         vii.      Gender:"Female" 
    let record:Object = { ContactPerson:"Person1" 
, Date: Sat Jun 30 2018 00:00:00 GMT+0530 (India Standard Time), EndTime: Thu Jun 28 2018 01:30:00 GMT+0530 (India Standard Time), StartTime: Thu Jun 28 2018 00:00:00 GMT+0530 (India Standard Time), Gender:"Female" };  
        this.Grid.addRecord(0, record) 
              
        
         }   
    }   
 
 

Please let us know if you have any further assistance on this. 

Regards, 
Venkatesh Ayothiraman. 



GB gaviyaiah boraiah replied to Venkatesh Ayothi Raman July 25, 2018 11:31 AM UTC

Hi Gaviyaiah, 
 
Thanks for the update. 
 
We went through your word documentation and screenshot that you have shared with us and found that you would like to add a record when user fill the details and click the add button. If so, we suggest you use addRecord API in Grid to perform the add operation. This is the recommended way to add a record dynamically. Please refer to the following Help documentation for more information, 
Code example: 
 
 
addButtonClik(e) {   
            //here you can get the values which is filled by user 
                                                               i.      ContactPerson:"Person1" 
                                                             ii.      Date:Sat Jun 30 2018 00:00:00 GMT+0530 (India Standard Time) {} 
                                                           iii.      EndTime:Thu Jun 28 2018 01:30:00 GMT+0530 (India Standard Time) {} 
                                                           iv.      Language:"Hindi" 
                                                             v.      StartTime:Thu Jun 28 2018 00:00:00 GMT+0530 (India Standard Time) {} 
                                                           vi.      TimeZone:"Mountain Time Zone" 
                                                         vii.      Gender:"Female" 
    let record:Object = { ContactPerson:"Person1" 
, Date: Sat Jun 30 2018 00:00:00 GMT+0530 (India Standard Time), EndTime: Thu Jun 28 2018 01:30:00 GMT+0530 (India Standard Time), StartTime: Thu Jun 28 2018 00:00:00 GMT+0530 (India Standard Time), Gender:"Female" };  
        this.Grid.addRecord(0, record) 
              
        
         }   
    }   
 
 

Please let us know if you have any further assistance on this. 

Regards, 
Venkatesh Ayothiraman. 


Hi,

In the separate (component) HTML file i have a grid defined like this;
<div class="row bg-gray-light-op">
<div class="col-lg-12">
<ejs-grid #grid id="AGrid" [dataSource]='gridData' [allowPaging]="true" [allowSorting]="true" [pageSettings]='pageSettings' [editSettings]='editSettings'>
<e-columns>
<e-column field='Date' headerText='Date' format='Mdy' width=60></e-column>
<e-column field='StartTime' headerText='Start Time' width=70></e-column>
<e-column field='EndTime' headerText='End Time' width=70></e-column>
<e-column field='Language' headerText='Language' width=50></e-column>
<e-column field='TranslatorGender' headerText='Gender' width=80></e-column>
</e-columns>
</ejs-grid>
</div>
</div>

And then 
export class CreateRequestComponent implements OnInit {

@ViewChild('grid')
public grid: GridComponent
this.grid.editSettings = { allowEditing: true, allowAdding: true, allowDeleting: true };
     THIS METHOD DIDN"T WORK.
public AddAppointment(): void {
//Create instance for the grid
var gridObj = $("#AGrid").data('ejs-grid'); //this line is not working it always says gridObj is undefined.
//Add the record
                         gridObj.dataSource.unshift(this.gridData)
gridObj.refresh();
}

This method also didn't work
public AddAppointment(): void {
//Create instance for the grid
var gridObj = $("#AGrid").data('ejs-grid'); //this line is not working it always says gridObj is undefined.
//Add the record
                        gridObj.addRecord(this.gridData,0);
}

This method also didn't work
public AddAppointment(): void {
this.grid.getCurrentViewRecords().unshift(this.gridData)
this.grid.refresh();
}


please help me how to  create an instance of the grid object in the component.ts file,  inside the AddAppointment method.




VA Venkatesh Ayothi Raman Syncfusion Team July 26, 2018 11:01 AM UTC

Hi Gaviyaiah,  
 
 
Thanks for the update. 
 
 
We went through your code example that you have shared with us and found that you are define the grid instance using Essential JavaScript 1 standard. So, we suggest you get the Essential JavaScript2 Syncfusion components instances like as follows, 
 
let gridInst:GridComponent = document.getElementById("Grid").ej2_instances[0]; 
 
  
But in an Angular way, we can get the Grid instance using ViewChild decorator like as follows. This is recommended as a way of getting an instance in the Angular project. 
 
@ViewChild('grid') public grid: GridComponent 
 //Here this.grid is Grid component 
 
 
We have further validated your code and found that you add the record using unshift JavaScript API instead Grid addRecord method. This is not recommended way, we suggest you to use addRecord API of Grid to add a record externally. Please refer to the following Help documentation for more information, 
 
Note: If we use addRecord API to add a record, then we need not refresh the Grid Manually. It will handle by default. 
 
Please let us know if you have any further assistance on this. 
 
Regards,
Venkatesh Ayothiraman.
 



GB gaviyaiah boraiah July 26, 2018 11:22 AM UTC

Hi,

The programming language being used is Angular only.
I just gave you different code saying none of them are working.
Again, I tried your suggestions and it is still not working.
if I use this suggestion: 
let gridInst:GridComponent = document.getElementById("Grid").ej2_instances[0]; 

I get this error.
[ts] Property 'ej2_instances' does not exist on type 'HTMLElement'.



IF I use this suggestion
this.grid.addRecord(this.gridData,0);

I get this error:
ng:///AppModule/CreateRequestComponent.ngfactory.js:2560 ERROR TypeError: Cannot read property 'addRecord' of undefined at GridComponent.push../node_modules/@syncfusion/ej2-grids/dist/es6/ej2-grids.es5.js.Grid.addRecord (vendor.js:111021) at CreateRequestComponent.push../src/app/InterpretationRequest/create-request/create-request.component.ts.CreateRequestComponent.AddAppointment (main.js:863) at Object.eval [as handleEvent] (ng:///AppModule/CreateRequestComponent.ngfactory.js:2566) at handleEvent (vendor.js:43133) at callWithDebugContext (vendor.js:44230) at Object.debugHandleEvent [as handleEvent] (vendor.js:43933) at dispatchEvent (vendor.js:40587) at vendor.js:41031 at HTMLButtonElement.<anonymous> (vendor.js:60113) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2743)


I have attached both HTML and .TS Files in ZIP for your reference here. kindly review and help us fix this issue.

Attachment: createrequest.component_80dee783.zip


VA Venkatesh Ayothi Raman Syncfusion Team July 28, 2018 03:52 AM UTC

Hi Gaviyaiah, 
 
Thanks for the feedback. 
 
Query 
Description 
Query #1:” Again, I tried your suggestions and it is still not working. I get this error.” 
This is typescript lint(type-cast) issue. So, we suggest you to typecast the HTMLElement to resolve this issue. Please refer to the following code example, 
buttonclick(args:any){ 
            //get the grid instance 
        let gridInst:any = (<any>document.getElementById('Grid')).ej2_instances[0]; 
    } 
 
 
Please refer to the following online forums for more information, 
 
Query #2:” ERROR TypeError: Cannot read property 'addRecord' of undefined” 
This issue occurs in following scenarios, 
i)                    We have tried to take the instances of Grid before the control is render and add the record. 
ii)                   ViewChild decorator id is mismatching or missed to define. Please refer to the following code example, 
<ejs-grid #grid [dataSource]='data' [height]='315' > 
           . .   .                     
                </ejs-grid> 
 
//this is the correct way to create the Grid instance using ViewChild decorator 
@ViewChild('grid') 
    public Grid: GridComponent; 
 
 
this.Grid.addRecord() 
Note: Please ensure your ViewChild definition and ID is correct 
iii)                 Missed to import the Edit module. Please refer to the following UG documentation for more information, 
 
We have also prepared a sample for your convenience which can be download from following link, 
 
Please let us know if you have any further assistance on this. 
 
 
Regards, 
Venkatesh Ayothiraman. 



AC Aparna Cilusani June 23, 2023 11:58 AM UTC

Hi, 


I am using ejs-grid with infinite scrolling inside ejs-tab, in console whenever I scroll and reach end of the grid I am fetching data from api and also data is getting updated. But grid scroller is hitting top of the grid again.


I have tried this.grid.refresh(), but it is not working.




PS Pavithra Subramaniyam Syncfusion Team June 26, 2023 11:30 AM UTC

Hi Aparna,


We have created a new forum “183101” for the “Infinite Scrolling” query under your account. So please follow that forum for more updates regarding infinite scrolling.


Regards,

Pavithra S



SG Suganya Gopinath Syncfusion Team July 1, 2023 04:10 PM UTC


Loader.
Up arrow icon