Hi,
I am new to Ignite UI and am trying to follow the online documentation on binding data via OData datasource.
1. I do see data returned back (alert does show the first record) as also I tried populating a normal HTML dropdown, but the Grid does NOT populate.
Any help would be welcome.
The code (In my past few days I have tried a few things- so maybe there is some duplication/redundancies).
Please Note - the "alert(data.d[0].Name);" returns the first record (and hence my belief that data is being returned successfully)
//*******Grid code that does not populate
var svc_url1 = "AdventureWorksDataService.svc/Products?$select=ProductID,Name"; $.ajax({ type: 'GET', dataType: 'json', contentType: 'application/json; charset=utf-8', url: svc_url1, beforeSend: function (xhr) { xhr.setRequestHeader("Accept", "application/json;odata=verbose"); xhr.setRequestHeader("MaxDataServiceVersion", "3.0"); }, success: function (data) { alert(data.d[0].Name); $('#tableProducts').igGrid({ height: '500px', width: '800px', responseDataKey: 'd.results', dataSource: svc_url1, autoGenerateColumns: false, columns: [ { headerText: 'ID', key: 'ProductID', dataType: 'number' }, { headerText: 'Name', key: 'Name', dataType: 'string' } ], }); }, error: function () { alert("Failed to retrieve !"); } });
//*************end of code
//**** This code correctly populates a normal HTML Select
var svc_url = "AdventureWorksDataService.svc/Products?$select=ProductID,Name"; $.ajax({ type: "GET", dataType: "json", contentType: 'application/json; charset=utf-8', url: svc_url, beforeSend: function (xhr) { xhr.setRequestHeader("Accept", "application/json;odata=verbose"); xhr.setRequestHeader("MaxDataServiceVersion", "3.0"); }, success: function (resp) { $.each(resp.d, function () { $("#lstCategories").append($("<OPTION></OPTION>").val(this['ProductId']).html(this['Name'])); }); }, error: function () { alert("Failed to retrieve Category items!"); } });
//*****end of code
Hello S Merchant,
Could you provide me with a sample that demonstrates the issue in order I can test it on my side? Waiting for your reply.
Regards,
Tsanna
Hi Tsanna,
Appreciate the response. It almost seems that the grid does not like the json returned by the web service. This is the code that retrieves the data. Please let me know if there is any other information that you require.
(AdventureWorksDataService.svc)
using System;using System.Collections.Generic;using System.Data.Services;using System.Data.Services.Common;using System.Linq;using System.ServiceModel.Web;using System.Web;namespace WebFoms_OData{ public class AdventureWorksDataService : DataService<AdventureWorksEntities> { // This method is called only once to initialize service-wide policies. public static void InitializeService(DataServiceConfiguration config) { // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc. // Examples: // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead); // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All); config.UseVerboseErrors = true; config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); config.SetEntitySetAccessRule("Products", EntitySetRights.AllRead); config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3; HttpContext.Current.Response.ContentType = "application/json; charset=utf-8"; // HttpContext.Current.Response.Write(json); } }}
*******************************************************************************************
Regards
Sam
Hi S Merchant,
I assume that the responseDataKey that you passed in to the grid is not correct. Please try only with the following: responseDataKey: 'd'. Also please remove the ajax request for grid initializing. If you encounter the same issue after these changes, let me know.
Appreciate the response. I tried setting the responseDataKey: 'd', but I do not know by what you mean by removing the "ajax request for grid initializing". I am sure this has to do with me being new to all of this. A working example would be great and much appreciated.
I'm sending a sample for your reference. If you need further assistance, please let me know.