Related
This might be the noobiest Strapi or possibly backend question at all, but I have just started doing backend, so please bear with me.
That being said, I have the following case. I am building an online shop and every product I have has a price (a required field) and new_price (optional). When I filter my API by min-max value, I would like to filter price if new_price is not available and new_price if it is available. Is this possible at all in strapi?
{
id: 2,
attributes: {
name: "My name",
createdAt: "2022-01-15T11:28:46.138Z",
updatedAt: "2022-02-16T10:38:20.412Z",
publishedAt: "2022-01-15T11:29:30.306Z",
description: "Lorem ipsum",
item_code: "688002",
slug: "some-slug-here",
available: true,
price: 59,
new_price: 21.9
}
http://localhost:1337/api/products?filters[price || new_price][$gte]=50
You're answer is perfectly fine. Just posted my full implementation here so that it may help others who stumble upon it.
const qs = require("qs");
const query = qs.stringify(
{
filters: {
$or: [
{
$and: [
{ new_price: { $notNull: true } },
{ new_price: { $gte: minPrice } },
{ new_price: { $lte: maxPrice } },
],
},
{
$and: [
{ new_price: { $null: true } },
{ price: { $gte: minPrice } },
{ price: { $lte: maxPrice } },
],
},
],
},
},
{
encodeValuesOnly: true,
}
);
await request(`/api/books?${query}`);
So I came up with this solution. It might be ugly and not how it's done, but it works, and I couldn't think of anything else. If somebody has a better solution, I will greatly appreciate it!
const query = qs.stringify(
{
populate: '*',
pagination: {
page: page,
pageSize: PER_PAGE
},
filters: {
$or: [
{
$and: [
[
{
new_price: {
$null: true
}
},
{
price: {
$gte: minPrice
}
},
{
price: {
$lte: maxPrice
}
}
]
]
},
{
$and: [
[
{
new_price: {
$notNull: true
}
},
{
new_price: {
$gte: minPrice
}
},
{
new_price: {
$lte: maxPrice
}
}
]
]
}
]
}
},
I been trying to load datas from elasticsearch into combobox but unsuccessfully, however load datas from json file to combobox it work.
the only different that is that load data from the store on json file, it successfully loaded the data, but when for elasticsearch, it reply '400: Bad Request'
[Json File]
[
{
"index":"color",
"_type":"_doc",
"_id":1,
"_score":1.0,
"_source":
{
"name":"Red"
}
},
{
"index":"color",
"_type":"_doc",
"_id":2,
"_score":1.0,
"_source":
{
"name":"Blue"
}
}
]
[ElasticSearch Json Reply]
{
"took":3,
"timed_out":false,
"_shards":
{
"total":1,
"successful":1,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":4,
"relation":"eq"
},
"max_score":1.0,
"hits":[
{
"_index":"color",
"_type":"_doc",
"_id":1,
"_score":1.0,
"_source":{
"name":"Red"
}
},
{
"_index":"color",
"_type":"_doc",
"_id":2,
"_score":1.0,
"_source":{
"name":"Blue"
}
},
{
"_index":"color",
"_type":"_doc",
"_id":3,
"_score":1.0,
"_source":{
"name":"Green"
}
},
{
"_index":"color",
"_type":"_doc",
"_id":4,
"_score":1.0,
"_source":{
"name":"Yellow"
}
}
]
}
}
My Model Code
Ext.define('MyApp.model.ColorModel',{
extend: 'Ext.data.Model',
alias: 'model.colormodel,
requires: [
'Ext.data.field.String',
'Ext.data.field.Integer'
],
fields: [
{
type:'string',
name:'_index'
},
{
type:'string',
name:'_type'
},
{
type:'string',
name:'_id'
},
{
type:'int',
name:'_score'
},
{
name:'_source'
},
]
});
My Store Code [Json File- That Work]
Ext.define('MyApp.store.ColorStore',{
extend: 'Ext.data.Store",
requires: [
'MyApp.model.ColorModel',
'Ext.data.proxy.Ajax',
'Ext.data.reader.Json'
],
constructor: function(cfg) {
var me = this;
cfg = cgf || {};
me.callParent([Ext.apply({
storeId:'ColorStore',
model:'MyApp.model.ColorModel',
proxy: {
type:'ajax',
url: 'http://localhost:8888/data/color.json,
withCredentials:true,
reader: {
type:'json'
}
}
}, cfg)]);
}
});
[My Another Store that retrieve from elasticsearch] - not working
Ext.define('MyApp.store.ESColorStore',{
extend: 'Ext.data.Store",
requires: [
'MyApp.model.ColorModel',
'Ext.data.proxy.Ajax',
'Ext.data.reader.Json'
],
constructor: function(cfg) {
var me = this;
cfg = cgf || {};
me.callParent([Ext.apply({
storeId:'ESColorStore',
model:'MyApp.model.ColorModel',
proxy: me.processMyAjaxProxy1({
type:'ajax',
read: function(operation, callback, scope){
var request = this.bulidRequest(operation, callback,scope);
var query = {
"from": operation.params.from,
"size": operation.params.size,
"query": {
"query_string" : { }
},
"sort": [
{
"name.raw":{
"order": "asc"
}
}
]
};
Ext.apply(request, {
headers: this.headers,
timeout: this.timeout,
scope: this,
callback: this.createRequestCallback(request,operation,callback,scope),
method: 'POST',
params: operation.params,
jsonData: query,
disableCaching:true,
success: function(rec) {
console.log('[ESColorStore], successfully retrieved query: ' + rec.responseText);
}
failure: function(rec) {
console.log('[ESColorStore], failed retrieved query: ' + rec.responseText);
}
});
Ext.Ajax.request(request);
return request;
},
reader: {
type:'json'
}
}
}, cfg)]);
},
proccessMyAjaxProxy1: function(config) {
config.api = {
read: 'http://localhost:9200/color/_search'
};
config.url = 'http://localhost:9200/color/';
return config
},
});
[Views]
Ext.define('MyApp.view.MyPanel', {
extend: 'Ext.Panel',
alias: 'widget.mypanel',
requires: [
'MyApp.view.MyPanelViewModel',
'Ext.field.ComboBox'
],
viewModel: {
type: 'mypanel'
},
title: 'My Panel',
items: [
{
xtype:'comobobox',
name:'select-the-color',
width: 419,
docked: 'top',
label:'Select The Color',
autoLoadOnValue: true,
displayField: '_source.name',
selectOnTab:false,
store:'ESColorStore',
valueField:'_source.name',
queryCaching: false,
queryParam:''
}
]
});
I am trying to format a Google Sheets. This is the code based on the Google API documentation for Ruby, conditional formatting and the Ruby Doc (very poor).
https://developers.google.com/sheets/api/guides/batchupdate
https://developers.google.com/sheets/api/samples/conditional-formatting
https://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/SheetsV4/Sheet#conditional_formats-instance_method
So far I did not find ANY full documentation on how to adapt the code to Ruby.
The terminal returns:
`check_status': badRequest: Invalid requests[0]: No request set. (Google::Apis::ClientError)
Do you have any idea of how to make this code work?
requests.push({
add_conditional_formats: {
rule: {
ranges: [
{
sheet_id: 0, start_column_index: 9, end_column_index: 100,
}
],
gradient_rule: {
minpoint: {
color: {
green: 0.2,
red: 0.8
},
type: "MIN"
},
maspoint: {
color: {
green: 0.9
},
type: "MAX"
},
}
},
index: 0
},
add_conditional_formats: {
rule: {
ranges: [
{
sheet_id: 0, start_column_index: 9, end_column_index: 100,
}
],
gradient_rule: {
minpoint: {
color: {
green: 0.2,
red: 0.8
},
type: "NUMBER",
value: 0
},
maspoint: {
color: {
green: 0.9
},
type: "NUMBER",
value: 256
},
}
},
index: 1
}
})
body = {requests: requests}
result = service.batch_update_spreadsheet(spreadsheet_id, body, {})
How about this modification? This modified script supposes that your access token can be used for updating spreadsheet.
Modification points :
add_conditional_format_rule is one request. So in your case, the array of requests is required to be 2 elements, because you are using 2 add_conditional_format_rule in the requests.
maspoint is maxpoint.
add_conditional_formats is add_conditional_format_rule.
Value of sheet_id is string.
Value of value of gradient_rule is string.
Modified script :
requests = []
requests.push({
add_conditional_format_rule: {
rule: {
ranges: [
{
sheet_id: "0", start_column_index: 9, end_column_index: 100,
}
],
gradient_rule: {
minpoint: {
color: {
green: 0.2,
red: 0.8
},
type: "MIN"
},
maxpoint: {
color: {
green: 0.9
},
type: "MAX"
},
}
},
index: 0
}
},{
add_conditional_format_rule: {
rule: {
ranges: [
{
sheet_id: "0", start_column_index: 9, end_column_index: 100,
}
],
gradient_rule: {
minpoint: {
color: {
green: 0.2,
red: 0.8
},
type: "NUMBER",
value: "0"
},
maxpoint: {
color: {
green: 0.9
},
type: "NUMBER",
value: "256"
},
}
},
index: 1
}
})
body = {requests: requests}
result = service.batch_update_spreadsheet(spreadsheet_id, body, {})
Reference :
spreadsheets.batchUpdate
If this was not what you want, I'm sorry.
I have the following object that contains 2 fixed attributes (OrderId and Purchasedate, and an array of attribues. I try to to put this in ng-repeat with orderBy option. The first 2 attribute (OrderId and PurchaseDate) work OK when sorting is applied by clicking on the header. However I do not get it working on the 3 rd attribute and so on.
The rows shown on the table are correct.
The object looks like
e.g.
$scope.orders;
[
{ OrderId: "P888291", PurchaseDate : "2016-12-02",
Items: { elt : [ { Name: "City", Value: "New York"}, { Name: "Street", Value: "Broadway 5" }, { Name: "Country", Value: "USA" } ] } },
{ OrderId: "P334498", PurchaseDate : "2016-11-02",
Items: { elt : [ { Name: "City", Value: "London" }, { Name: "Street", Value: "WestMinister 3" }, { Name: "Country", Value: "Great Brittain" } ] } },
{ OrderId: "G393383", PurchaseDate : "2016-11-28",
Items: { elt : [ { Name: "City", Value: "Milan" }, { Name: "Street", Value: "Pizza 8" }, { Name: "Country", Value: "Italy" } ] } },
{ OrderId: "P978381", PurchaseDate : "2015-05-25",
Items: { elt : [ { Name: "City", Value: "Seattle" }, { Name: "Street", Value: "Houston 9" }, { Name: "Country", Value: "US" } ] } },
{ OrderId: "P983394", PurchaseDate : "2015-06-05",
Items: { elt : [ { Name: "City", Value: "Amsterdam" }, { Name: "Street", Value: "Damrak 5" }, { Name: "Country", Value: "Netherlands" } ] } },
{ OrderId: "G678994", PurchaseDate : "2015-04-01",
Items: { elt : [ { Name: "City", Value: "The Hague" }, { Name: "Street", Value: "Markt 22" }, { Name: "Country", Value: "Netherlands" } ] } },
{ OrderId: "P128994", PurchaseDate : "2016-12-04",
Items: { elt : [ { Name: "City", Value: "The Hague" }, { Name: "Street", Value: "Plein 7" }, { Name: "Country", Value: "Netherlands" } ] } },
];
Please advise and the code is put in :
http://www.w3schools.com/code/tryit.asp?filename=FAG7BWVK8BYH
You can try with custom filter logic.(https://docs.angularjs.org/api/ng/filter/orderBy )
for example:
JS:
$scope.filterOrderFn = function(orderobj)
{
// Do
if(...)
{
return _something_// this will be your sorted order according to your first condition
}
else if(...)
{
return _something_ // this will be your sorted order according to your second condition if require
}
return false; // otherwise it won't be within the orderlist
};
HTML:
...
<article data-ng-repeat="order in orders | orderBy:filterOrderFn" class="order">
...
If you need a very specific ordering behaviour you could write your own filter (although orderBy should be enough for most uses cases). As you may know you can chain many filters together, so adding your custom filter function doesn't force you to remove the previous filter using the search object (they will work together seamlessly).
I have a question about the Kendo Grid and grouping - I'd like to include some logic when it comes to grouping a grid. I need to group addresses by state, and if state is empty, then group by country. Is this doable? Thanks.
You could create a hidden column that has state is available otherwise country, and then set the datasource to group by that column:
var jsondata = [
{City : "Houston",State : "Texas",Country : "USA"},
{City : "New York",State : "New York",Country : "USA"},
{City : "Austin",State : "Texas",Country : "USA"},
{City : "London",State : "",Country : "UK"},
{City : "Manchester",State :"",Country : "UK"},
{City : "Paris",State : "",Country : "France"}
];
for (var i=0; i < jsondata.length; i++){
var stateCountry = jsondata[i].State ? jsondata[i].State : jsondata[i].Country;
jsondata[i].Group = stateCountry;
}
$(document).ready(function() {
$("#grid").kendoGrid({
dataSource: {
data: jsondata,
schema: {
model: {
fields: {
City: { type: "string" },
State: { type: "string" },
Country: { type: "string" },
}
}
},
group: {
field: "Group",
dir: "asc"
}
},
groupable: false,
scrollable: true,
columns: [
{ field: "City" },
{ field: "State" },
{ field: "Country" },
{ field: "Group", title: "State/Country", hidden: true }
]
});
});
DEMO