How to add dynamically v-model and it's initial values to existed html elements by vuejs - vue.js

For SEO purposes I need to render html elements by php. For example I have these elements.
<?php foreach ($elements as $key => $element): ?>
<select name="first" v-model="model[<?= $key; ?>]">
<option value="">Select</option>
<option value="1">Some Text</option>
<option value="2">Some Text</option>
</select>
<select name="second" v-model="model[<?= $key; ?>]>
<option value="">Select</option>
<option value="4">Some Text</option>
<option value="5">Some Text</option>
</select>
...
...
...
<select name="eleven" v-model="model[<?= $key; ?>]>
<option value="">Select</option>
<option value="101">Some Text</option>
<option value="102">Some Text</option>
</select>
<?php endforeach; ?>
And probably I can manipulate these elements like this on vue side.
const count_models = <?= count($elements) ?>; // in the html
const app = new Vue({
el: '#app',
data: {
model:[]
},
mounted(){
console.log(this.model);
for (let $i = 0; $i < count_models; $i++) {
this.model[$i] = "";
}
}
})
I cannot declare the initial values for model[?]. I need an xhr or assign counted items to a javascript variable to get how many select element I have on DOM and to declare initial values as null for each model[]. Even I redeclare the initial values of the models, it doesn't bind. I just put an example on jsFiddle. In Angular1 there was ng-init attribute to declare initial value for the model.
How can I solve this problem ?
https://jsfiddle.net/ks7jmgwv/1/

you just encountered one of the most common gotchas of Vuejs: reactivity (and therefor the lack off)!
The issue here is when the model property is created it's just an empty array/object and any property that you add to that element it's not going to be reactive: that means any kind of change made programmatically won't trigger the Vue's internal whatches, the only reason that the v-model still works is that the changes made by the user and not by the code does actually trigger native HTML events.
You have two possible solutions:
Ignore the reactivy part (but you won't be able to programmatically update the selected value, or at least it won't be visible) and just make sure that the 'Select' option will be selected by default by assigning it the correct value (in that way you can just skip all the default for-loop initialization).
<option :value="undefined" selected="selected" disabled>Select</option>
Follow the offical way suggested by the Vuejs' documentation to add a new property to an object and still having the advantage of reactivity
this.$set(this.model, $i, "");
You can check this plunker in which I'm showing you both the ways of obtaining your goal:
https://jsfiddle.net/8y7eu39z/18/
Also, did you know that for placeholder options in a select you can add the attribute disabled?
Reactivy Reference: https://v2.vuejs.org/v2/guide/reactivity.html
Also: if you want a "null" as a default value but did not manage to find a way to make it being recognized by the "select" options just use :value="null" instead of value="null" and then you should be able to use
this.$set(this.model, $i, null);

Related

need alpine.js :value to be used just in calculation not to override default html value

so im working with laravel 8 and using alpine.js to create value for readonly field that will on submit be processed with the form in store method.I did this successfully until now, cuz i'm now using select for input. Relevant code looks like this:
<div class="card-body"
x-data="{
bom: parseInt( ''),
quantity: parseInt( ''),
total: parseInt( '')
}">
<select class="form-control select2 {{ $errors->has('bom') ? 'is-invalid' : '' }}" name="bom_id" id="bom_id" x-model.number="bom" required>
#foreach($bom_value as $key => $value)
<option value="{{ $value->id }} {{ old('bom_id') == $value->id ? 'selected' : '' }}" :value="{{ $value->total }}">{{ $value->name }}</option>
#endforeach
</select>
Problem is, that when i set :value it shows same value in value="{{ $value->id }}", but i need to pass id there, and only use $value->total for calculation of that read only input based on selected choice. I have checked and $value->id shows right data, so as all variables. Guess there is something wrong with alpine or my understanding of it.
I don't clearly understand what you mean with:
and only use $value->total for calculation of that read only input based on selected choice
But you definitely shouldn't use any attribute (here value) multiple times. Since you are binding a value to it with :value=.. you are selling AlpineJs to put attach the value you pass between the quotes to the value attribute.
So you could create a custom data-attribute, let's say data-value and pass your value to it. You can then extract this value and attach it to your readonly input field.
Does this help you?

How to properly use select in vuetify?

I currently using vuetify, and so far the code below is working <v-select>.
<v-select
:items="order_by_opt"
v-model="order_column"
label="Order By"
outlined
></v-select>
//script
data() {
order_by_opt: ["Last Added", "Name", "Year Joined", "Age", "Date of Birth", ..etc.],
order_column: ''
}
I just want to ask how can we translate the html code below to v-select
<select>
<option value="last_added">Last Added</option>
<option value="name">Name</option>
//more options here
</select>
In which label and value is different like for example below. If I select Last Added, then I should get the value last_added?
You can do that by providing an array of objects, like this example in the docs. The props define which option property represents the selected value, and which is the display value.

Blazor data binding two values to selectlist

I can easily bind one value to my model, but what if I want to bind two or more?
Sample code, tried to bind to the complex object but it fails.
Component:
<select id="myClassId" #onchange="SelectionChanged" class="form-control">
<option value=""></option>
#foreach (var myObject in myClassList)
{
<option value="#myObject">#myObject.AccountName</option>
}
</select>
Code
void SelectionChanged(ChangeEventArgs e)
{
Console.WriteLine(((MyClass)e.Value).AccountId);
Console.WriteLine(((MyClass)e.Value).AccountName);
}
UPDATE:
Ended up doing this. Probably not the most elegant solution, but I'm not sure if there are other more "supported" ways.
<select id="myClassId" #onchange="SelectionChanged" class="form-control">
<option value=""></option>
#foreach (var myObject in myClassList)
{
<option value="#myObject.AccountId;#myObject.AccountName">#myObject.AccountName</option>
}
</select>
void SelectionChanged(ChangeEventArgs e)
{
var accountId = e.Value.ToString().Split(";")[0];
var accountName = e.Value.ToString().Split(";")[1];
Console.WriteLine(accountId);
Console.WriteLine(accountName);
}
The value of a select is a string. That is a fundamental limitation of the HTML, and has nothing to do with Blazor. You cannot use an object. Your best bet here is to assign an id or some other uniquely identifying value, and then in your onchange handler, look up the actual object you want to assign using that value.

Does one select tag can have two v-model?

I have this collection called servers which is like this: {id, name, code}.
I have this select tag, I iterate over my servers collection and the data() with the model values.
<select v-model="serveur">
<option v-for="server in servers" :key="server.id" :value="server.name">
{{server.name}}
</option>
</select>
.
.
.
data () {
return {
serveur:'',
code:''
}
}
The value "server.name" selected is binded to the model "serveur".
All of this works just fine, but I want the value "server.code to be binded to the model "code"
I can't figure out how to do this. I'm new to vuejs, like I've been working with vue for the first time only two days ago.
This is also my first Stack Overflow "asks" so I hope I am being clear enough.
Thanks in advance.
You really have one independent data item, which I will call selectedServer. It gets the value of the server entry, which contains both name and code.
<select v-model="selectedServer">
<option v-for="server in servers" :key="server.id" :value="server">
{{server.name}}
</option>
</select>
You can simply refer to selectedServer.name and selectedServer.code in the URL you need to assemble from them, or you can create computeds to return those values under whatever names you prefer, like
data: {
selectedServer: {}
},
computed: {
serveur() { return this.selectedServer.name; },
code() { return this.selectedServer.code; }
}
To achieve this, you can bind the select with an array containing 'serveur' and 'code'. But you can't bind the select with multiple variables directly.
You can do comething like that:
<select #input="serveur = $event.name; code = $event.code">
<option v-for="server in servers" :key="server.id" :value="server">
{{ server.name }}
</option>
</select>
If you need fiddle - feel free to ask. It is possible that this example not working :)
I believe you can find an answer here:
https://stackoverflow.com/a/50982485/10954996
that is something I would do - create change handler and change everything I need within handler method
I hope this helps

flexigrid refresh using javascript

Refresh the flexigrid data on dropdown (select) index changed or rebind the flexigrid on index change in javascript or clear the flexigrid data on select change
If you're just looking to bind the grid's refresh with another element, you can use
$('#flex1').flexReload()
I have done the following. On selection change of the Severity, the flexigrid reloads with the data for the selected severity
<select id='ddlSeverity' onchange="setSeverity($('#ddlSeverity option:selected').attr('value'));">
<option value="1">Severity 1</option>
<option value="2">Severity 2</option>
<option value="3">Severity 3</option>
<option value="4">Severity 4</option>
</select>
$(document).ready(function () {
function setSeverity(severity) {
//Construct the URL to be called.
var urlAction = '#Url.Action("RequestQueueBySeverity", "Home", new { severity = "__BOGUS__" })';
//Replace the bogus parameter with the parameter to be passed
urlAction = urlAction.replace("__BOGUS__", severity);
$('#flex1')
.flexOptions({
url: urlAction,
newp: 1
}).flexReload();
}
}
Please mark as answer if it works for you..
For efficient reloading, you can try this:
$("#flexigrid")[0].grid.populate()