Making an array reactive in vue Composition API - vuejs3

This seems simple but I could not find an answer. I have a form and all inputs are put in an object. Now the backend requires the form object to be wrapped in an array. So I have this :
<input
class="input"
placeholder="Serial #"
v-model="form.serialNum"
/>
// etc for form
and in <script setup>
let form = reactive({
eqpmntType: "",
make: "",
modelNum: "",
// etc
So I read docs and understand reactive function is for objects so I tried ref():
let form = ref([{
eqpmntType: "",
make: "",
modelNum: "",
//etc
}]
And unfortunately I lose my reactivity for the form inputs. I know I can wrap the form object in an array after the fact and submit. I was wondering if there is a way to make an array or for that matter an array of objects reactive. Thanks for any clarification.

I think your initiated form is reactive and have no problem to me. Maybe it is how you use it.
For example, if you want to alter the value of modelNum within form, since it is an Array type now, make sure you did this:
form.value[0].modelNum = 123
And in the input element, the v-model should be v-model="form[0].modelNum" also because of type Array.
Althogh I don't see why the backend requires you to pass Array-type params since it seems form only contains 1 Object within the array. Normally, in below case, Array-type params will be adopted:
const form = ref([
{
eqpmntType: "1",
make: "1",
modelNum: "1"
},
{
eqpmntType: "2",
make: "2",
modelNum: "2"
},
{
eqpmntType: "3",
make: "3",
modelNum: "3"
},
])
In this case, your input must be more than one so that each v-model of one input will bind each modelNum respectively. So if this answer doesn't solve your problem, I would like you to display more code and details.

Related

Extracting values with jq only when they exist

I have a large file of records that contain fields that look something like this:
{
"id": "1000001",
"updatedDate": "2018-12-21T01:52:00Z",
"createdDate": "1993-11-30T02:59:25Z",
"varFields": [
{
"fieldTag": "b",
"content": "1000679727"
},
{
"fieldTag": "v",
"content": "v.1"
}
}
I need to extract the .content element along with other things, but only when the fieldTag associated with it is "v". Only some records contain a fieldTag "v".
When I try to parse using
(.varFields[] |select(.fieldTag=="v") | "\(.content)") // ""
it works fine so long as v is present. However, when it is not present, I get
jq: error (at <stdin>:353953): Cannot iterate over null (null)
I tried to get rid of the error with multiple variations, including things to the effect of
(select((.varFields[] |select(.fieldTag=="v") | .content) != null) | .varFields[] |select(.fieldTag=="v") | "\(.content)") // ""
but I'm still getting the same error. What am I missing?
Take a look at the error suppression operator ? that works a bit like the new ?. nullable chaining operator in Javascript.
The ? operator, used as EXP?, is shorthand for try EXP.
Example:
jq '[.[]|(.a)?]'
Input [{}, true, {"a":1}]
Output [null, 1]
They have a slightly simpler demonstrable example of this at https://jqplay.org/jq?q=%5B.%5B%5D%7C(.a)%3F%5D&j=%5B%7B%7D%2C%20true%2C%20%7B%22a%22%3A1%7D%5D and the try-catch operator is similar if all you need is custom error handling (or just error ignoring...).

DynamoDB update_item: Increase values for existing items or use 1 for new item

I try to use updateitem as according to the wiki it works for updating existing & creating new items:
"Edits an existing item's attributes, or adds a new item to the table
if it does not already exist."
I want to store:
the first time someone opened a item
the last time someone opened a item
the number of openings of a item
I have following code:
table.update_item(
Key={'item':"NEW"},
UpdateExpression="SET opens = if_not_exists(opens + :var1, :var0), last_open = :var2, first_open = if_not_exists(first_open, :var3)",
ExpressionAttributeValues={
':var0': "0",
':var1': "1",
':var2': '2020-04-01',
':var3': '1999-01-01'
},
ReturnValues="UPDATED_NEW"
)
runs into error for new & existing items and says
An error occurred (ValidationException) when calling the UpdateItem
operation: Invalid UpdateExpression: Syntax error; token: \"+\", near:
\"opens + :var1\""
Following works for existing items but throws an error for new ones:
table.update_item(
Key={'item':"NEW"},
UpdateExpression="SET opens = opens + :var1, last_open = :var2, first_reachout = if_not_exists(first_open, :var3)",
ExpressionAttributeValues={
':var1': "1",
':var2': '2020-04-01',
':var3': '1999-01-01'
},
ReturnValues="UPDATED_NEW"
)
Error for only new ones:
"An error occurred (ValidationException) when calling the UpdateItem operation: The provided expression refers to an attribute that does not exist in the item"
I guess it means the contacts attribute, but including it into "if_not_exists" also does not work....
You can find the documentation for the UpdateExpression syntax in https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.SET. The relevant part for your question is the following:
value ::=
operand
| operand '+' operand
| operand '-' operand
operand ::=
path | function
Which means, the "+" can only be at the top level of the expression, it cannot be inside the function's parameters.
Luckily, there is an easy workaround for you. Instead of the if_not_exists(opens + :var1, :var0) which you tried, try" :var1 + if_not_exists(opens, :var0).

Altering Json output in Drupal

I am outputing a json document from a view that looks like this:
[
{
"NewsTitle": "asdas",
"ItemDescription": "asdasdasd\r\n",
"NumofViews": "3",
"Likes": "0",
"PostDate": "10 Mar, 2016",
"ImageUrl": "6_n_0.jpg",
"NewsType": "8",
"ShareURL": "",
"VideoURL": "https://www.youtube.com/",
}
]
I want to remove the brackets in the beginning to be outputed in this form :
{
"menu": {
"NewsTitle": "asdas",
"ItemDescription": "asdasdasd\r\n",
"NumofViews": "3",
"Likes": "0",
"PostDate": "10 Mar, 2016",
"ImageUrl": "6_n_0.jpg",
"NewsType": "8",
"ShareURL": "",
"VideoURL": "https://www.youtube.com/",
}
}
This is the configuration i am using:
in Views:
FORMAT
Format:JSON data document | Settings
FIELDS
in Format:JSON data document | Settings
Root object name
*empty*
The name of the root object in the JSON document. e.g nodes or users or forum_posts
Top-level child object
*empty*
The name of each top-level child object in the JSON document. e.g node or user or forum_post
Field output
Normal *chosen*
Raw
For each row in the view, fields can be output as either the field rendered by Views, or by the raw content of the field.
Plaintext output *selected*
For each row in the view, strip all markup from the field output.
Remove newlines
Strip newline characters from the field output.
JSON data format
Simple *selected*
MIT Simile/Exhibit
To be consumed by jqGrid
What object format will be used for JSON output.
JSONP prefix
If used the JSON output will be enclosed with parentheses and prefixed by this label, as in the JSONP format.
Content-Type
Default: application/json *selected*
text/json
application/javascript
The Content-Type header that will be sent with the JSON output.
Views API mode
With Views API mode the JSON will embedded as normal content so normal page processing is used. Leave it unchecked when JSON should be printed directly to the client.
Object arrays
Outputs an object rather than an array when a non-associative array is used. Especially useful when the recipient of the output is expecting an object and the array is empty.
Numeric strings
Encodes numeric strings as numbers.
Numeric strings *selected*
Encodes large integers as their original string value.
Pretty print *selected*
Use whitespace in returned data to format it.
Unescaped slashes *selected*
Don't escape forward slashes /.
Unescaped unicode
Encode multibyte Unicode characters literally (default is to escape as \uXXXX).

How to set selectedValue for "blank" value using html.select in Coldbox?

After search for a few hours...I found the answer to my problem from this question: How to populate a select list using a structure inside select HTML helper in ColdBox?
This example is perfect since I wasn't able to find anything on the ColdBox Doc. I used the "simple-form" for my Options... the result display is correct. However, the selectedValue does not seem to work when the value is a "". I also tried populating the Options from an array. I had the same issue for both method when it comes setting selectedValue is .
When the value is "", the first value in the Options always gets selected. How do I fix this or how do I change the Options to display "Select..." for when a selectedValue is ?
#html.select(
name="Approved",
options="Yes,No,Pending",
column="value",
selectedValue="",
label="",
required="required",
title="Approved"
)#
You need to create an array of objects containing a name and value pair for blank option, along with the rest of options:
<cfset foo = [
{"name"= "Select", "value"= ""}
, {"name"= "Yes", "value"= "Yes"}
, {"name"= "No", "value"= "No"}
, {"name"= "Pending", "value"= "Pending"}
] />
And pass the array to the select helper function using html.options()
method.
#html.select(
name="Approved",
options=html.options(foo),
selectedValue="",
label="",
required="required",
title="Approved"
)#
Note: In this case you don't need to provide column attribute.
This may solve your problem.

Getting error when using and or statement in my expression?

I am trying to do an and/or statement for a field in my report, and I am getting an error. If I just do the one without the "or" is works fine. Is there a different way I need to code this?
=IIf(Fields!NOP.Value = "1","Consulting Fee","") or IIf(Fields!NOP.Value = "3","Honoraria","")
From what I've been able to tell there is no formal "OR" statement in SSRS expressions, what you might try is a switch:
=Switch(Fields!NOP.Value = "1", "Consulting Fee", Fields!NOP.Value = "3","Honoraria")
Nested IIF statements should work:
=IIF(Fields!NOP.Value = "1", "Consulting Fee", IIF(Fields!NOP.Value = "3", "Honoraria", ""))

Resources