Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
14
rated 0 times [  16] [ 2]  / answers: 1 / hits: 19155  / 8 Years ago, fri, december 9, 2016, 12:00:00

I use VueJs, i need to extract javascript variable to generate hidden fields.



But i need to set the name by the index of the variable.



I want to use zig-zag naming schema.



like,



 <input type=text name=segment[{index}][field_name] :value={value}>


Javascript Variable:



    var test_template = {
0: {
nb: 2
},
1: {
nb: 1
},
2: {
nb: 4
}
};


Foreach with Variable to Generate Hidden Fields :



    <div v-for=(a,index) in test_template class=row>            
<input type=hidden :name=segment[index][nb] :value=a.nb>
</div>


Here, :name is a dynamic instance for access vuejs values.
index is vuejs variable but segment is not a vuejs variable, its actually a string.



But i need this schema to generate array of inputs.



Is this possible ?



Or Any other solutions are there ?



Thanks in Advance !


More From » vue.js

 Answers
53

To create input elements with dynamic names by index, you can use the + in a JS expression to concatenate:


<div v-for="(a,index) in test_template" class="row">            
<input type="hidden" :name="'segment[' + index + '][nb]'" :value="a.nb">
</div>

Generates:


<input type="hidden" name="section[0][nb]" value="2">
<input type="hidden" name="section[1][nb]" value="1">
<input type="hidden" name="section[2][nb]" value="4">

See: https://v2.vuejs.org/v2/guide/syntax.html#Using-JavaScript-Expressions


[#59775] Tuesday, December 6, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
freddiejarretk

Total Points: 612
Total Questions: 103
Total Answers: 88

Location: Armenia
Member since Sat, Dec 31, 2022
1 Year ago
;