SurveyJs Input element with addon - reactjs

Am using ReactJS with surveyjs, I want to use an input element for a percentage value (%).
I wonder if there is a way of doing this:
Using bootsrap addon or something like that with surveyjs inputs.
Limit input value between 0 and 100.
Thank you.

You could configure a question of type "text" to accept only numbers between 0 and 100. There are two options on the question that need to be set:
Add a validator of type "numeric" with minimum value of 0 and a maximum of 100.
Set the input type to "number" and set its min and max values to 0 and 100 respectively.
The resulting JSON should look like this:
{
"pages": [
{
"name": "page1",
"elements": [
{
"type": "text",
"name": "Enter percentage (0-100)",
"validators": [
{
"type": "numeric",
"minValue": 0,
"maxValue": 100
}
],
"inputType": "number",
"min": "0",
"max": "100"
}
]
}
]
}

Related

MuleSoft: Update the value in Array and replace the content based on condition

I have below Payload.
Here if points are not divisible by 5, then round up to the nearest 5. So, if the points are 250.2, the resulting number would be 250 and that is divisible by 5
so 250 would be the value returned.
If the resulting value was 251.2, then the resulting whole number would be 251 and that is not divisible by 5 and would be rounded up to 155
{
"referenceID": "1001",
"Year": "2023",
"Type": "BK",
"contracts": [
{
"contractId": "1",
"contractType": "Booking",
"Points": "250.2",
"Reservations": {
"reservations": [
],
"reservationPoints": ""
}
},
{
"contractId": "1",
"contractType": "Booking",
"Points": "251.2",
"Reservations": {
"reservations": [
],
"reservationPoints": ""
}
}
]
}
Based on above conditions, output payload should be like below
{
"referenceID": "1001",
"Year": "2023",
"Type": "BK",
"contracts": [
{
"contractId": "1",
"contractType": "Booking",
"Points": "250",
"Reservations": {
"reservations": [
],
"reservationPoints": ""
}
},
{
"contractId": "1",
"contractType": "Booking",
"Points": "255",
"Reservations": {
"reservations": [
],
"reservationPoints": ""
}
}
]
}
With regards to this rounding up Points to nearest number divisible by 5, I am using below logic
if (((payload.contracts[0].Points as Number mod 5))<1)
(round((payload.contracts[0].Points as Number)/5)*5)
else
(ceil((payload.contracts[0].Points as Number)/5)*5)
This gets the updated value based on condition but I am not able to update the Payload.
Make use of update operator just to update Points value in the payload Object. Try like below:
%dw 2.0
output application/json
---
payload update {
case c at .contracts -> c map ($ update{
case p at .Points -> if((p mod 5) <1) round((p/5))*5
else ceil((p/5))*5
})
}
Based on the above suggestion, I updated payload like below
%dw 2.0
output application/json
---
payload.contracts map ((item,index)-> item update {
case Points at .Points -> if (((Points as Number mod 5))<1) (round((Points as Number)/5)*5)
else (ceil((Points as Number)/5)*5)
})

Update field with value from a nested field

I know I can use the pipeline aggregator within update to update the field of one value with the value from another field. However, my issue is when updated a field value based on the value of a nested field. The result of the update always issues the new field with brackets. I don't want brackets/array, I just want it to be a value. See code below
https://mongoplayground.net/p/7ZDP8CYtKK3
db={
"players": [
{
"_id": ObjectId("5fba17c1c4566e57fafdcd7e"),
"username": "moshe",
"health": 0,
"maxHealth": 200,
"Chapters": [
{"Cat A": 25,
"Cat B": 100,
"Cat C": 125}]
}
]
}
Here's the query I apply below
db.players.update(
{username: "moshe"},
[{"$set": {"health": "$Chapters.Cat A"}}]
)
The result yields
[{"Chapters": [
{"Cat A": 25,
"Cat B": 100,
"Cat C": 125}],
"_id": ObjectId("5fba17c1c4566e57fafdcd7e"),
"health": [25],
"maxHealth": 200,
"username": "moshe"
}]
What I want is for the update to health to appear without array brackets as so.... "health":25
Again this is an example based on a much much larger DB I'm working with.
You can use $arrayElemAt or $first(v4.4) operators to select the first element from an array,
db.players.update(
{ username: "moshe" },
[{
"$set": {
"health": {
"$arrayElemAt": ["$Chapters.Cat A", 0]
}
}
}]
)
Playground

JSON: use jq to edit specific values in nested arrays

I'm trying to update values within an array inside an array using the utility jq. I've pasted the sample json below.
More specifically: Within the sheets array, and then within the formulas array, I'd like to change each columnName with a value of "MONTH" to "YEAR". I'd like to do the same for within the sheets array, within the columnStyles array, change each incidence of "MONTH" also to "YEAR"
This jq filter gets me the list of columnNames.
.sheets[1] | .formulas[] | .columnName
How can I edit the entire file in place by just updating the values I want? Do I use map with if?
And what if I wanted to edit a portion of a value? For example, in a forumlaString property, just changing the part of the string that contains MONTH but leaving the rest intact?
{
"version": "6.1.1",
"className": "xyz",
"sheets": [
{
"name": "Pass1",
"sheetId": "95e6c2cd-abbe-46c1-8012-bdf37438b9b7",
"keep": true,
"formulas": [
{
"columnName": "SAMPLE_PROVIDER",
"columnId": "0",
"columnIndex": 0,
"formulaString": "\u003dGROUPBY(#Raw!SAMPLE_PROVIDER)"
},
{
"columnName": "MONTH",
"columnId": "1",
"columnIndex": 1,
"formulaString": "\u003dGROUPBY(#Raw!MONTH)"
}
],
"columnStyles": [
{
"columnId": "0",
"name": "SAMPLE_PROVIDER",
"width": 206,
"thousandSeparator": true
},
{
"columnId": "1",
"name": "MONTH",
"width": 100,
"thousandSeparator": true
}
],
"nextColumnId": 2
},
{
"name": "Transform1",
"sheetId": "49071c1c-fa84-4ae3-92c1-b63175a6b26c",
"keep": true,
"formulas": [
{
"columnName": "SAMPLE_PROVIDER",
"columnId": "0",
"columnIndex": 0,
"formulaString": "\u003d#Pass1!SAMPLE_PROVIDER"
},
{
"columnName": "MONTH",
"columnId": "1",
"columnIndex": 1,
"formulaString": "\u003d#Pass1!MONTH"
}
],
"columnStyles": [
{
"columnId": "0",
"name": "SAMPLE_PROVIDER",
"width": 179,
"thousandSeparator": true
},
{
"columnId": "1",
"name": "MONTH",
"width": 100,
"thousandSeparator": true
}
],
"nextColumnId": 3
}
],
"advancedSchedulingInUse": true,
"errorHandlingMode": "IGNORE"
}
To change the columnName field in the desired containers, you can use
jq '(.sheets[] | .formulas[]? | .columnName | select(.=="MONTH")) |= "YEAR"' tmp.json
(The ? avoids an error if there is no key formula.)
To replace MONTH with YEAR in formula strings, replace each formulaString value with a possible modified string returned by sub.
jq '(.sheets[] | .formulas[]? | .formulaString) |= sub("MONTH"; "YEAR")' tmp.json
(sub requires jq 1.5, compiled with the Oniguruma library.)
To combine these into a single jq filter? I'm not sure yet; I have only a tenuous understanding of why either one alone works.
It looks like you're updating more than just fields in the formulas arrays, but a little bit of everything.
If you want to indiscriminately change all occurrences of the string "MONTH" to "YEAR", you could do this:
(.. | strings) |= sub("MONTH"; "YEAR")
This may be a task for walk/1.
(If your jq does not have walk/1, then you can copy its definition from https://github.com/stedolan/jq/blob/master/src/builtin.jq)
For example, if you want to change "MONTH" to "YEAR" whenever "MONTH" appears as the value of a key in an object, then the following would do the job:
jq 'walk(if type == "object"
then with_entries(.value |= (if . == "MONTH" then "YEAR" else . end))
else . end)' input.json
Equivalently:
jq 'walk(if type == "object"
then with_entries(if .value == "MONTH" then .value = "YEAR" else . end)
else . end)' input.json
These can easily be modified in accordance with similar requirements.

Editing array elements with angular-formly

I am using angular-formly to create input screens and all works great as long as I have a KEY for each VALUE.
But i have a situation where the value is an object with an array in it . It is an industry standard so I can not change the
field structure.
The parent Key is GEOCODE its value is COORDINATES (which is an array with 2 elements)
I want to edit those 2 elements (longitude and Latitude). Is this doable or does formly need a KEY:VALUE to create and input which will allow editing of those values.
/* -- JSON DATA -- */
[
"fullname": "Glen Benson",
/* this is the objext I am trying to edit coordinates elements */
**
"geolocation": {
"coordinates": [
-94.467136,
39.090707
],
"type": "Point"
}
**
]
/* -- FORMLY CONTROLLER SNIPPET --*/
{
"key": "fullname",
"type": "input",
"templateOptions": {
"type": "text",
"placeholder": "fullname"
}
},
**{
"key": "geocode.coordinates[0]",
"type": "input",
"templateOptions": {
"type": "text",
"placeholder": "geocode"
}
}**
You can get the value without having to name it some crazy thing. It should work how you have it, but I mean, my God, that is ugly.
just set the defaultValue to the value of geolocation....
key: 'CoordinateX',
defaultValue: 'model.geocode.geolocation.coordinates[0]'
OR add a controller on it to get it
key: 'CoordinateX',
controller: function($scope) {
$scope.model.CoordinateX = $scope.model.geocode.geolocation.coordinates[0];
}
then you can simply use CoordinateX or whatever else you want to name it.

Identify documents in mongodb when matching two key:value pairs within a single array

I am trying to identify documents where both key-value pairs within an array match using the aggregate pipeline. Specifically, if I want to find documents where one array contains user_attribute.Name = Quests_In_Progress and user_attribute.Value =3. Below is an example of such a document that I'm trying to match.
If I use
db.myCollection.aggregate({
$match: {
"user_attribute.Name": "Quests_In_Progress",
"user_attribute.Value": "3"
}
})
It will match every document that contains Quests_In_Progress for user_attribute.Name in one element of the array and contains "3" for user_attribute.Value, regardless of whether they exist in the same element of the array or not.
i.e.
db.myCollection.aggregate({
$match: {
"user_attribute.Name": "Quests_In_Progress",
"user_attribute.Value": "0"
}
})
will match the same document simply because one element of the array has a key:Value pair of Value:0 and another element of the array contains a key:value pair of Quests_In_Progress.
What I want to do is identify documents where both of those conditions are met within one element of the array.
I tried to do this with $elemMatch, but I couldn't get it to work. Plus the aggregate documentation doesn't indicate that $elemMatch works, so maybe that's why I couldn't get it to work.
Lastly, I need to use the aggregate pipeline, because there are a bunch of other things I have to do after finding these documents- specifically unwinding them.
{
"_id": ObjectId("5555bb32de938ce667f78ce00"),
"user_attribute": [{
"Value": "Facebook",
"Name": "Social_Connection"
}, {
"Name": "Total_Fireteam_Missions_Initiated",
"Value": "0"
}, {
"Name": "Quests_Completed",
"Value": "3"
}, {
"Name": "Item_Slots_Owned",
"Value": "36"
}, {
"Name": "Quests_In_Progress",
"Value": "3"
}, {
"Name": "Player_Progression",
"Value": "0"
}, {
"Value": "1",
"Name": "Characters_Owned"
}, {
"Name": "Quests_Started",
"Value": "6"
}, {
"Name": "Total_Friends",
"Value": "0"
}, {
"Name": "Device_Type",
"Value": "Phone"
}]
}
Try using $elemMatch
db.myCollection.aggregate([{$match: {"user_attribute": {$elemMatch: {"Name":"Quests_In_Progress", "Value":"0"}}}}, { $out, "temp"}])
That query will find anyone who has element of their array "Quests_In_Progress" with a value of 0 and put it into the collection temp

Resources