|
<ej-drop-down-list id="bikeList" width="100%" create="onCreate">
<e-drop-down-list-fields text="text" value="empid" />
</ej-drop-down-list>
<script>
function onCreate() {
//Internal methods required for modifying existing Adaptor's processQuery method
var filterQueries = function (queries, name) {
return queries.filter(function (q) {
return q.fn === name;
}) || [];
};
var filterQueryLists = function (queries, singles) {
var filtered = queries.filter(function (q) {
return singles.indexOf(q.fn) !== -1;
}), res = {};
for (var i = 0; i < filtered.length; i++) {
if (!res[filtered[i].fn])
res[filtered[i].fn] = filtered[i].e;
}
return res;
};
var callAdaptorFunc = function (obj, fnName, param, param1) {
if (obj[fnName]) {
var res = obj[fnName](param, param1);
if (!isNull(res)) param = res;
}
return param;
};
var getValue = function (value, inst) {
if (typeof value === "function")
return value.call(inst || {});
return value;
};
var isNull = function (val) {
return val === undefined || val === null;
};
// Creating custom adaptor
var customAdaptor = new ej.ODataAdaptor().extend({
//you can add custom $filter queries in the url in the processQuery method
// override the default webpapi adaptor's processQuery method here
processQuery: function (dm, query, hierarchyFilters) {
var sorted = filterQueries(query.queries, "onSortBy"),
grouped = filterQueries(query.queries, "onGroup"),
filters = filterQueries(query.queries, "onWhere"),
searchs = filterQueries(query.queries, "onSearch"),
aggregates = filterQueries(query.queries, "onAggregates"),
singles = filterQueryLists(query.queries, ["onSelect", "onPage", "onSkip", "onTake", "onRange"]),
params = query._params,
url = dm.dataSource.url, tmp, skip, take = null,
op = this.options;
var r = {
sorted: [],
grouped: [],
filters: [],
searches: [],
aggregates: []
};
// calc Paging & Range
if (singles["onPage"]) {
tmp = singles["onPage"];
skip = getValue(tmp.pageIndex, query);
take = getValue(tmp.pageSize, query);
skip = (skip - 1) * take;
} else if (singles["onRange"]) {
tmp = singles["onRange"];
skip = tmp.start;
take = tmp.end - tmp.start;
}
// Sorting
for (var i = 0; i < sorted.length; i++) {
tmp = getValue(sorted[i].e.fieldName, query);
r.sorted.push(callAdaptorFunc(this, "onEachSort", { name: tmp, direction: sorted[i].e.direction }, query));
}
// hierarchy
if (hierarchyFilters) {
tmp = this.getFiltersFrom(hierarchyFilters, query);
if (tmp)
r.filters.push(callAdaptorFunc(this, "onEachWhere", tmp.toJSON(), query));
}
// Filters
for (var i = 0; i < filters.length; i++) {
r.filters.push(callAdaptorFunc(this, "onEachWhere", filters[i].e.toJSON(), query));
for (var prop in r.filters[i]) {
if (isNull(r[prop]))
delete r[prop];
}
}
// Searches
for (var i = 0; i < searchs.length; i++) {
tmp = searchs[i].e;
r.searches.push(callAdaptorFunc(this, "onEachSearch", {
fields: tmp.fieldNames,
operator: tmp.operator,
key: tmp.searchKey,
ignoreCase: tmp.ignoreCase
}, query));
}
// Grouping
for (var i = 0; i < grouped.length; i++) {
r.grouped.push(getValue(grouped[i].e.fieldName, query));
}
// aggregates
for (var i = 0; i < aggregates.length; i++) {
tmp = aggregates[i].e;
r.aggregates.push({ type: tmp.type, field: getValue(tmp.field, query) });
}
var req = {};
req[op.from] = query._fromTable;
if (op.expand) req[op.expand] = query._expands;
req[op.select] = singles["onSelect"] ? callAdaptorFunc(this, "onSelect", getValue(singles["onSelect"].fieldNames, query), query) : "";
req[op.count] = query._requiresCount ? callAdaptorFunc(this, "onCount", query._requiresCount, query) : "";
req[op.search] = r.searches.length ? callAdaptorFunc(this, "onSearch", r.searches, query) : "";
req[op.skip] = singles["onSkip"] ? callAdaptorFunc(this, "onSkip", getValue(singles["onSkip"].nos, query), query) : "";
req[op.take] = singles["onTake"] ? callAdaptorFunc(this, "onTake", getValue(singles["onTake"].nos, query), query) : "";
req[op.antiForgery] = (dm.dataSource.antiForgery) ? dm.antiForgeryToken().value : "";
req[op.where] = r.filters.length || r.searches.length ? callAdaptorFunc(this, "onWhere", r.filters, query) : "";
req[op.sortBy] = r.sorted.length ? callAdaptorFunc(this, "onSortBy", r.sorted, query) : "";
req[op.group] = r.grouped.length ? callAdaptorFunc(this, "onGroup", r.grouped, query) : "";
req[op.aggregates] = r.aggregates.length ? callAdaptorFunc(this, "onAggregates", r.aggregates, query) : "";
req["param"] = [];
// Params
callAdaptorFunc(this, "addParams", { dm: dm, query: query, params: params, reqParams: req });
// cleanup
for (var prop in req) {
if (isNull(req[prop]) || req[prop] === "" || req[prop].length === 0 || prop === "params")
delete req[prop];
}
if (!(op.skip in req && op.take in req) && take !== null) {
req[op.skip] = callAdaptorFunc(this, "onSkip", skip, query);
req[op.take] = callAdaptorFunc(this, "onTake", take, query);
}
var p = this.pvt;
this.pvt = {};
if (this.options.requestType === "json") {
return {
data: JSON.stringify(req),
url: url,
ejPvtData: p,
type: "POST",
contentType: "application/json; charset=utf-8"
}
}
// you can override the url and set it here in the "return" or edit the convertToQueryString method
tmp = this.convertToQueryString(req, query, dm);
tmp = (dm.dataSource.url.indexOf("?") !== -1 ? "&" : "/") + tmp;
return {
type: "GET",
url: tmp.length ? url.replace(/\/*$/, tmp) : url,
ejPvtData: p
};
},
// you can modify the query string value in the convertToQueryString method
// override the default webpapi adaptor's convertToQueryString method here
convertToQueryString: function (req, query, dm) {
var res = [], tableName = req.table || "";
delete req.table;
if (dm.dataSource.requiresFormat)
req["$format"] = "json";
for (var prop in req)
res.push(prop + "=" + req[prop]);
res = res.join("&");
if (dm.dataSource.url && dm.dataSource.url.indexOf("?") !== -1 && !tableName)
return res;
return res.length ? tableName + "?" + res : tableName || "";
},
});
var dataManager = ej.DataManager({ url: "../api/Values", crossDomain: true, adaptor: new customAdaptor() });
this.setModel({ dataSource: dataManager });
}
</script> |