Formatting dynamically created items.

Hello.  I have two grids in my page.  The first has rows created by my Razor template, and I may later add new rows using the URL manager INSERT url.  Everything works ok.

                    <e-grid-column field="lastOperation" width="85" headerText="Updated" allowEditing="false" format="yMd"></e-grid-column>

The second grid starts empty, but when I click on a row in the first grid, I load rows for the second grid.  I use the same basic code that the documentation uses to explain how  the "refresh()" method works.  Here is a typical column on the second table:

                    <e-grid-column field="uploadDate" isPrimaryKey="false" visible="true" width="75" headerText="Date" format="yMd"></e-grid-column>

Again, I am using a data format "yMd".

I use this code to update the second (as the success function to an $.Ajax call)

success: function (result, status, xhr) {
      const videoGrid = document.getElementById("VideoGrid").ej2_instances[0];
      videoGrid.dataSource.splice(0, videoGrid.dataSource.length);
      for (var video of result) {
         videoGrid.dataSource.unshift(video);
      }
      videoGrid.refresh();
...

Basically, just adding rows to the gird.dataSource array.  The problem here is that the formatting specified in the original column is not applied when adding rows dynamically.  

Am I doing something wrong or is the application of the formatting not possible in this case?

thank in advance.

dennis.




5 Replies

MS Madhu Sudhanan P Syncfusion Team October 3, 2018 07:02 AM UTC

 
Thanks for contacting Syncfusion support. 
 
We suspect the date field value is in string format in the newly added record, grid requires JavaScript Date object to apply formatting properly. So we suggest you to parse the result using ej.data.DataUtil.parse.parseJson method before assign the data to the grid. 
 
 
<script> 
success: function (result, status, xhr) { 
        const videoGrid = document.getElementById("VideoGrid").ej2_instances[0]; 
        videoGrid.dataSource.splice(0, videoGrid.dataSource.length); 
        result = ej.data.DataUtil.parse.parseJson(result); 
        for (var video of result) { 
            videoGrid.dataSource.unshift(video); 
        } 
        videoGrid.refresh(); 
        ... 
</script> 
 
 
 
Regards, 
Madhu Sudhanan P 




SE sellinger October 4, 2018 05:42 AM UTC

This does not seem to be working (although I can confirm that the fields in question are numeric and date fields).  I am including a screen shot with two grids, the first is populated with the URL manager, the second is populated in code (note that in the second, formatting is not applied).




MS Madhu Sudhanan P Syncfusion Team October 4, 2018 06:44 AM UTC

Hi Sellinger, 

We suspect the value of the Date field is not recognized by the grid control. Normally grid understands two string format such as ISO format(2018-04-05-22:32:00) and MS format(Date(/456885332/)) and they can be parsed to JavaScript date object using ej.data.DataUtil.parse.parseJson. Please ensure whether the Date field value is in one of the specified formats when returned from the server and also if the issue still persist please share the response to us to check the Date field value from the ajax success. 

Regards, 
Madhu Sudhanan P 



SE sellinger October 4, 2018 09:01 AM UTC

I have managed to get my formatting working.  In fact, when my page is built, the data source on the contained no rows.  If I add a dummy row to the grid in my razor template, then all my dynamically created rows will be formatted correctly (even if I completely remove all the rows).  If I bind an empty array to the data source, the formatting is not applied.  I am not sure of what is going on internally (or if this is the desired behavior), but I think I can live with this for now.

Thanks for all your help.

cheers.
d. 


MS Madhu Sudhanan P Syncfusion Team October 5, 2018 05:19 AM UTC

Hi Sellinger,  

Thanks for the update. Please get back to us if you need further assistance. 

Regards,  
Madhu Sudhanan P 


Loader.
Up arrow icon