We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

Grid and OData V4 Support

Thread ID:

Created:

Updated:

Platform:

Replies:

125000 Jul 13,2016 09:06 AM Jun 8,2018 09:09 AM JavaScript 3
loading
Tags: ejGrid
Andrea Perazzolo
Asked On July 13, 2016 09:06 AM

Hi,
I am trying to use the OData V4 support for data binding to a grid that has Search features.
When I pass the search on server the response is the following:

{
  "error":{
    "code":"","message":"The query parameter '$search' is not supported."
  }
}

Am I doing something wrong? The other operations works correctly.
Also there is a support for the PATCH Verb of the Odata Controller when doing an Edit?
I have only some fields of my record on the grid and with the PUT Verb the other data are overwritten with null values.
On the OData Protocol it is suggested to prefer PATCH over PUT for updating a record.

We are using the ASP Net Web API OData V4 implementation.

   Thanks in advance

    Andrea Perazzolo

Alan Sangeeth S [Syncfusion]
Replied On July 14, 2016 07:52 AM

Hi Andrea, 

Query 1: “Search Query is not supported” 

Though ODataV4 has support for handling search queries, WebAPI OData doesn’t have support to handle search query. Please refer the following link. 


You may use OData Service without WebAPI to achieve searching support. 

Query 2: “support for the PATCH Verb of the Odata Controller when doing an Edit” 

You can achieve PATCH verb support by using custom adaptor in Grid DataManager. Please refer the following code example. 
var customAdaptor = new ej.ODataV4Adaptor().extend({ 
            update: function (dm, keyField, value, tableName) { 
                return { 
                    type: "PATCH", 
                    url: dm.dataSource.url.replace(/\/*$/, tableName ? '/' + tableName : '') + '(' + value[keyField] + ')', 
                    data: JSON.stringify(value), 
                    accept: this.options.accept 
                }; 
            } 
        }) 
            $("#Grid").ejGrid({ 
                dataSource: ej.DataManager({ url: "/odata/Orders", adaptor: new customAdaptor() }), 
 
}) 


For your convenience we have created a sample and the same can be downloaded from below link. 


Regards,
Alan Sangeeth S 


Jerome Piquot
Replied On June 6, 2018 01:56 AM

Hi,

You can create a custom query attribute to handle the search command :

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public sealed class EnableQueryWithSearchAttribute : EnableQueryAttribute
{
    public Type ModelType { get; set; }
 
    public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
    {
        HttpRequest request = actionExecutedContext.HttpContext.Request;
        KeyValuePair<string, StringValues> searchParam = request.Query.FirstOrDefault(q => q.Key == "$search");
        if (!string.IsNullOrWhiteSpace(searchParam.Key))
        {
            var queryItems = request.Query.Where(q => q.Key != "$search").ToDictionary(p => p.Key, o => o.Value);
            if (!string.IsNullOrWhiteSpace(searchParam.Value))
                queryItems.Add("search", searchParam.Value);
            request.Query = new QueryCollection(queryItems);
        }
        base.OnActionExecuted(actionExecutedContext);
    }
}
Then in the controller use the custom attribute instead of EnableQuery :

[EnableQueryWithSearch]
public IActionResult Get()
{
	string search = Request.Query.FirstOrDefault(p => p.Key == "$search").Value;
 
	//if (!AuthorizationService.AuthorizeAsync(User, CrudPermissions<TD>.Read).Result)
	//    throw new AppException(T["ODATA Get on {0} is not authorized.", T[typeof(TD).Name]]);
 
	if (search == null)
		return Ok(Service.ODataGet() as IQueryable<TD>);
	else
		return Ok(Service.ODataSearch(search) as IQueryable<TD>);
}
 
Jérôme Piquot
FiveForty
France

Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On June 8, 2018 09:09 AM

Hi Jerome,  
 
We can also use the following way to search the table in ODataV4. Refer to the following code.  
 
        public PageResult<Order> Get(ODataQueryOptions opts) 
        { 
 
            var results = db.Orders.AsQueryable(); 
            var count = results.Count(); 
            if (opts.OrderBy != null) 
                results = opts.OrderBy.ApplyTo(results); 
            results = results.OrderBy(ord => ord.OrderID); 
            var allUrlKeyValues = ControllerContext.Request.GetQueryNameValuePairs(); 
            string key = allUrlKeyValues.LastOrDefault(x => x.Key == "$search").Value; 
            if (key != null) 
            { 
 
                results = results.Where(fil => fil.ShipCity.Contains(key) || fil.CustomerID.Contains(key)); 
            } 
            . .  
               . .  
            return new PageResult<Order>(results.ToList(), null, count); 
        } 
 
In this, we have looked for the $search key and used the default where filter query.  
 
Regards, 
Seeni Sakthi Kumar 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;