Vue JS 2 - dynamic array - vuejs2

I understand with Vue JS 2, you have to declare reactive properties upfront.
However, just wondering how would you do a rest call if the child objects appear depending on a few other conditions. For example:
"abc": {
"tests": [
{
"a1": "xxxx",
"result": null,
"selected": false,
"comment": null
} ...
]
}
Now in this example, what happens if the tests are empty ((null)) in some circumstances?
eg: if you bind it using the v-model xx.xx.abc.tests[1].selected - This wont work as tests[1] is null.
So I tried using Vue.set in mounted function to assign a default value but that did not work. It will be hard to define all these in a static data as then I have to know all the tests before the rest call.
Looking at the Vue warning when accessing nested object the issue is if I do the check for inside
the checkbox (Checkbox is rendered from Spring MVC tag).
<form:checkbox id="xx" path="..Spring MVC Path.." v-if="(xx.xx.abc.tests!=null && xx.xxx.abc.tests[3].selected)" **v-model**="xx.xxx.abc.tests[3].selected"/>
The checkbox does not appear at all. I want it to appear regardless of empty value as it gives users to add the option. Also, the other option of statically declaring it does not work as I don't know want to hard code and define array.
for example that means in case another element is added it is hardcoded into script:
[
{
"testName": String,
"result": String,
"selected": false,
"comment": String
},
{
"testName": String,
"result": String,
"selected": false,
"comment": String
}
]
Basically I need to bind using v-model even if it is null in this case. Tried Vue.set but for some reason did not appear to work.
I guess I am missing something here,any help would be appreciated.

Assume that you have a list of checkbox to render whether the child element is null on not. but you do know the array length, right?
If you do, consider use v-for to render checkboxes (then you’re able to track array item by index) and listening on its change event then assign specific value to specific object when events fire

thanks for the replies. As this was due to rest call not returning all required attributes that was defined in Vue model(As mentioned in question).So I checked that in final section of rest call and if they are empty, created them.
axios
.get(***URL***)
.then(
response => {
this.info = response.data;
this.status.loaded = true;
}
)
.finally(
() => {
*** check and fill missing attributes with default values ***
**Example: this.$set(this.someObject, 'b', 2)
}
)
Reference:
https://v2.vuejs.org/v2/guide/reactivity.html

Related

Vue JS: Add number of available options in select drop down menu

I'm trying to display the number of options next to each item in a select drop-down menu. Like in the example.
My data is pulled from a JSON using AXIOS. Here is one line from an object in my array
"Data": {
"K": "Part Time, Full-time",
},
and the computed property which I'm attempting to create. It brings back a number but not the ones I would like. Can anyone help me out with this, please?
computed: {
feeCount() {
const feeResults = this.results.filter(
(result) => result.Data && result.Data.K
);
return feeResults.length;
},
}
Your problem is that "Part Time, Full-time" is a string, not an array, so you're getting the length of the string instead.
You can use result.Data.K.split(',') to generate an array then count the length from that.

Vue3 quick access printing Target in a "Proxy" object

Vue3 prints everthing as an a "Proxy" object.
I know the value is inside [[Target]], but I don't want to keep expading Proxy->[[Target]] every single time.
Just wanted to let clear, this does't change the output at all. it is just a 'cleaner' way to see stuff.
An example:
const b = 'value'
console.log(value)
Output -> { Proxy: { [[Handler]]: ..., [[Target]]: 'value', [[IsRevoked]]: false }
okay, this works.
console.log(JSON.parse(JSON.stringify(b))).
for more info.
Accessing a Proxy object in Vue3

Vue: Setting Data by matching route query

I'm attempting to set data fields provided by an array based on the Vue Router query. For example, when someone lands on my website using example.com/?location=texas, I want to set the location data by an array.
An example the array:
locations {
{
slug: "texas",
tagline: "Welcome to Texas",
}, {
slug: "california",
tagline: "Welcome to California",
}
}
I know this should be done using a computed property, however I am unable to get anything functioning. I've tried simple tests like if (this.slug.location === "texas"), and I cannot get the location data to populate. I would also like to provide default data in case there are no route matches.
Any help is extremely appreciated!
Edit:
I can accomplish this in a very manual way. Right now, I'm setting the query in data by the following:
slug: this.$route.query.location
I can display specific text by doing something like:
h3(v-if="slug === 'texas'") This will show for texas
h3(v-else-if="slug === 'california'") This will show for California
h3(v-else) This is default
The issue with this approach is there are various elements I need to customize depending on the slug. Is there any way I can create an array, and move whichever array matches a key in an array to the data??
You should be able to access a query param using the following (link to Vue Router documentation):
this.$route.query.location
So based on what you listed I would do something like...
export default {
computed: {
displayBasedOnLocationQueryParam() {
switch(this.$route.query.location) {
case 'texas':
return 'Welcome to Texas'
default:
return 'hello there, generic person'
}
}
}
}
Note that I'm not using your array explicitly there. The switch statement can be the sole source of that logic, if need be.

How to use a cell-renderer without specifying a field in ag-grid

I want to use the last column as the expand row functionality. For this I'm using master/detail. The last column of master will just be an arrow down icon which on click will show the detail row. I'm unable to add a last column without providing a field name. The column is rendered empty. How can I use something like this:
{
headerName: '',
field: "", // as not associated to any column data
cellRenderer: "agGroupCellRenderer"
}
Here's the working example that you may be looking for, I did not had to rely on field for it. https://plnkr.co/edit/RxjZ9rUt7HUhlw5ejUd6?p=preview
Example is in ReactJS ag-grid code but the same thing applies to any framework as ag-grid relies on the columnDefinition array that you pass it in.
Here's the code change:
{
colId: "action", // optional
headerName: "Action", // set it to single space if you dont want any text
cellRendererFramework: (params) => <button onClick={() => (console.log(params), params.node.setSelected(true))}>+</button>, // this can be your any custom function
suppressSorting: true, // sorting for this field dont make sense, but optional
},
Hope that helps.

Vue return v-model name by method

I'm creating a rates per hour form. I have all the categories in my database rates table. I'd like to be able to grow the categories and it will automatically grow the fields on my form.. loop through it all.
The user needs to select a begin to end rate. I start at this rate and end at this rate.
So right now I have these values in json being loaded onto my page:
rateCategories: [ { "name": "Local", "input1": "Local$0", "input2":
"Local$1" }, { "name": "International", "input1": "International$0",
"input2": "International$1" }, { "name": "Weekdays", "input1":
"Weekdays$0", "input2": "Weekdays$1" }, { "name": "Weekends",
"input1": "Weekends$0", "input2": "Weekends$1" } ]
userRates: { "Local$0": 10, "Local$1": 25, "International$0": 300,
"International$1": 400, "Weekdays$0": 100, "Weekdays$1": 200,
"Weekends$0": 200, "Weekends$1": 300 }
What I'm doing is looping through rateCategories array to create the form. I need to dynamically create the v-model name for all the input fields to match the keys in userRates...
This is where it stops working! It will create the v-model name for me if I write:
:v-model.number="'userRates.'+rateCategories.name+'$0'"
but it won't compute it with the value from the user array. I do get a value into an input field if I write out the v-model name without creating it dynamically like this:
v-model.number="userRates.Weekdays$0"
but then the form needs to be manually updated every time a new category is added.
Is it really impossible to have a dynamic v-model name?? Why??
Also if that's so, how do I go about this so the page can loop through the categories when added to the database and not need a manual update to the page when categories are added or taken away??
Note: I'm also using Vuex store, so I'd rather it not break anything with my getter and setter functions too
Vue.js has full javascript expression support in data bindings, so an object's property can be accessed like you would in plain javascript.
Have you tried using userRates[rateCategories.name$0] ?
Using Javascript Expressions in Vue.js