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() }),
…
}) |
[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); } }
[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>); }
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);
} |