Vue JS, Drag n drop, and Get Ids - vue.js

I manage differents teams, and a single column of competitors.
With vue-dragula, I can drag n' drop competitors to the team I want, this is working fine.
Now, I need to detect which competitors is added to which team and modify the myTeam variable each drag and drop.
I wrote a fiddle here:
https://jsfiddle.net/xoco70/vzk27smw/9/
I removed the events because they won't work in fiddle, but they are like:
Vue.vueDragula.eventBus.$on(
'drop',
function (args) {
...
}
);
Vue.vueDragula.eventBus.$on(
'dropModel',
function (args) {
...
}
)
How should I get teams Id, and competitor Id that I moved, so I can persist asignations ???

Here is my Fiddle: https://jsfiddle.net/kanlidy/tsofafzq/4/
In your HTML template panel-body part:
<div class="panel-body">
<div class="container-dragula"
v-dragula="copyOne"
:team-id="team.id"
bag="third-bag">
<div v-cloak
v-for="(competitor, index) in copyOne"
:team-id="team.id"
:id="compotitor.id"
:index="index"
:key="competitor.id"
>{{competitor.name}}
</div>
</div>
</div>
In your wrapper-dragula part: :competitor="competitor" to :id="competitor.id"
<div class="wrapper-dragula">
<div class="container-dragula" v-dragula="competitorsArea"
bag="third-bag">
<div v-cloak
v-for="(competitor, index) in competitorsArea"
:id="competitor.id"
:index="index"
:key="competitor.id"
>{{competitor.name}}
</div>
</div>
</div>
In your JS:
created: function () {
Vue.vueDragula.eventBus.$on('drop', function (args) {
alert('teams ID is:'+args[1].parentNode.getAttribute("team-id"));
alert('competitors ID is: ' + args[1].getAttribute("id"));
})
}
Hope helps.

Related

How to display a string value in template section in vue.js?

I developed one page which is responsible for showing order success message and i am getting response from backend which contains orderId ,i am getting that response and i am able to bind in front end but i am getting my output as a json format in my UI page ,but what i need is to display only orderID value only (like a string).please help me to fix this issue...
i want to display orderID value only
OrderPlace.vue
<template>
<div class="order-place">
<div class="image-container">
<img src="../assets/success.png" alt="not found" />
</div>
<div class="title-container">
<p>Order placed Successfully</p>
</div>
<div class="message-section">
<p>Hurray!!!your order is confirmed and placed successfully contact us in below details
for further communication..</p>
</div>
<div class="order-id">
{{orderNumber}}
</div>
<div class="title-section">
<div class="email-us">
<p>Email-us</p>
</div>
<div class="contact-us">
<p>Contact-us</p>
</div>
<div class="address">
<p>Address</p>
</div>
</div>
<div class="email-sec">
<p>admin#bookstore.com</p>
</div>
<div class="contact-sec">
<p>+918163475881</p>
</div>
<div class="address-sec">
42, 14th Main, 15th Cross, Sector 4 ,opp to BDA complex, near Kumarakom restaurant, HSR Layout, Bangalore 560034
</div>
<div class="button">
<router-link to="/dashboard" class="btn">Continue Shopping</router-link>
</div>
</div>
</template>
<script>
import service from '../service/User';
// import { EventBus } from "./event-bus.js";
export default {
name: 'OrderPlace',
data(){
return{
successTitle:'Order placed Successfully',
adminEmailSection:'Email-us',
adminContactSection:'Contact-us',
adminAddressSection:'Address',
adminEmail:'admin#bookstore.com',
adminMobNum:'+918163475881',
orderNumber: ''
}
},
created() {
service.confirmMail().then(response =>
(this.orderNumber=JSON.stringify(response.data))
)
}
}
</script>
You're stringifying an Object of { message: string; orderId: number } which of course will result in that "string" of an Object being displayed when you use
<div class="order-id">
{{orderNumber}}
</div>
Like Boussadjra said just assign the id to the corresponding data field
service.confirmMail().then(response =>
(this.orderNumber=response.data.orderId)
)
Just assign the orderID to the orderNumber:
service.confirmMail().then(response =>
(this.orderNumber=response.data.orderID)
)

Dynamic computed property in html

I'm using php for creating set of computed properties in my app based on ID property of each object in my main array of data stored in property deals. So i have now computed properties like list_10_deals_cnt, list_20_deals_cnt, list_30_deals_cnt etc. Now, how can I create these dynamic created properties in span with class dials__cnt while looping my array of data? {{'list_'+el.id+'_deals_cnt'}} is not working as i wish, its display just a string like list_10_deals_cnt instead to display a computed value.
P.S. sorry about my english.
<div class="dials" id="app">
<div class="dials__column" v-for="(el, index) in deals">
<div class="dials__header">
<div>{{el.title}}</div>
<div>сделок: <span class="dials__cnt">{{`'list_'+el.id+'_deals_cnt'`}}</span>, <span></span> руб</div>
</div>
<draggable drag-class="draggable-ghost__class" class="dials__block" :list="el.items" group="deal" #change="log(el.id, $event)">
<div
class="dials__card"
v-for="(el, index) in el.items"
:key="el.title"
>
<div class="card__header">
<div class="card__title">{{ el.customer_name }}, {{ el.customer_company_name }}</div>
<div class="card__date">{{ el.created_at }}</div>
</div>
<div class="card__body">
<a :href="'/deal/detail/'+el.id" class="card__link">{{ el.title }}</a>
</div>
<div class="card__footer">
<span class="card__price">{{ el.price }} руб </span>
<span v-for="(tag, index) in el.tags" class="card__tag">{{ tag }}</span>
</div>
</div>
</draggable>
</div>
</div>
<script>
new Vue({
el: '#app',
data: {
deals: <?php echo json_encode($deals, JSON_UNESCAPED_UNICODE); ?>
},
computed: {
<?php
foreach ($deals as $k=>$v) {
echo 'list_'.$v->id.'_deals_cnt: function() {return this.deals['.$k.'].items.length},';
}
?>
},
methods: {
log: function(id, evt) {
if (evt.hasOwnProperty('added')) {
let dealID = evt.added.element.id;
console.log('сделка ID '+dealID+' перемещена в статус '+id+'. Отправляем аякс запрос на обновление статуса в бд');
// ajax for update
}
}
}
});
</script>
Hi
Problem 1
you will not be able to get the value of computed property by using this
{{`'list_'+el.id+'_deals_cnt'`}}
for the same reason as console.log('vari' + 'able') doesn't print out value of variable to the console.
( Vue evaluates whatever is in between {{ }} as an expression ).
Solution
I suppose, you can either use the deals property directly in html as shown below without using a computed property
<div class="dials__column" v-for="(el, index) in deals">
<div class="dials__header">
<div>{{el.title}}</div>
<div>сделок: <span class="dials__cnt">{{ el.items.length }}</span>, <span></span> руб</div>
</div>
----------- rest of the code
or you can create a computed property based on deals data property and use that to loop in html using v-for.
Problem 2
I don't think below code is valid php string. although it becomes irrelevant if you use first solution above.
<?php
foreach ($deals as $k=>$v) {
echo 'list_'.$v->id.'_deals_cnt: function() {return this.deals['.$k.'].items.length},';
}
?>
the ' inside ['.$k.'] should be escaped.
Solution
echo 'list_'.$v->id.'_deals_cnt: function() {return this.deals[\'.$k.\'].items.length},';

How to get data from JSON? My v-for loop is not working

I'm trying to get data from a external json but the v-for loops does not work. Previously, with the same code I was able to show the data I don't know whats happens now.
This is the code i'm using:
`
<div class="row">
<div class="col-sm-4">
<h1>Llista Valors</h1>
<div class="card" style="margin-bottom: 2%" v-for="dada in list">
<div class="card-header">
Hora: {{dada.lists.hora}}
Preu: {{dada.lists.preu}}
</div>
</div>
</div>
<div class="col-sm-8">
<h1>JSON</h1>
<pre>
{{ $data }}
</pre>
</div>
</div>
</div>`
`
var url = "http://172.17.100.3/api/tarifa2.php";
new Vue({
el: '#main',
created: function () {
this.getApi();
},
data: {
lists: []
},
methods: {
getApi: function () {
this.$http.get(url).then(function (response) {
this.lists = response.body.valors;
});
}
}
});
</script>`
This is what i get:
Your data should be called list:[], not lists:[] to match your template. Or rename your template to in lists.
For each dada in lists, there is no dada.lists.hora or dada.lists.preu. There is only dada.preu or dada.hora.
Each dada in lists is like saying lists[0] (or whatever index).
So try changing the template to just {{data.hora}} etc as needed (after doing the list/lists adjustment per above).

How to fix my price to show on "Pris:" in java in vue?

<script>
// # is an alias to /src
export default {
name: 'home',
data() {
return {
inc: 0,
inc2: 0,
inc3: 0,
}
},
methods: {
toInc() {
this.inc++
},
toDec() {
this.inc--
},
toInc2() {
this.inc2++
},
toDec2() {
this.inc2--
},
toInc3() {
this.inc3++
},
toDec3() {
this.inc3--
}
}
computed:{ //here i get this error with ";"
total(){
return this.inc+this.inc2+this.inc3;
}
}
}
</script>
<div class="plane">
<div class="columns">
<ul class="price">
<div class="prisinfo">
<p class="item">Ordinarie (<span class="cost">85kr</span>/st)</p> //those prices i need to get when im pressing on +
<div class="priser">
<li class="pris1">-</li>
<p>{{inc}}</p>
<li class="pris1">+</li>
</div>
</div>
<div class="prisinfo">
<p class="item">Barn (<span class="cost">65kr</span>/st)</p> //those prices i need to get when im pressing on +
<div class="priser">
<li class="pris2">-</li>
<p>{{inc2}}</p>
<li class="pris2">+</li>
</div>
</div>
<div class="prisinfo">
<p class="item">Pensionär (<span class="cost">75kr</span>/st) </p> //those prices i need to get when im pressing on +
<div class="priser">
<li class="pris3">-</li>
<p>{{inc3}}</p>
<li class="pris3">+</li>
</div>
</div>
</ul>
</div>
<section id="totalprice">
<p>Pris:<span class="total_price">{{total}}</span></p> // and here my total price after getting all the prices togheter after choosing how many tickets you need.
</section>
I got error in console with "unexpected ;" and my dosnt work anymore after i did those changes. I need also to get the price to add when i press + and substact when i press -, i have the price in html.
You got my coments in the code if you need them.
Im so bad at vue right now, and java is my weak side too.
to show the total price, you may find computed property very useful:
after your data property, add :
computed:{
total(){
return this.inc+this.inc2+this.inc3;
}
},
and you HTML line would be:
<p>Pris:<span class="total_price">{{total}}</span></p>
now, about the + and - buttons:
they look good, i think its just a tiny mistake on the HTML side: you should call the function toInc with ().
like this:
<li class="pris3">+</li>
(#click is like v-on:click https://v2.vuejs.org/v2/guide/syntax.html#v-on-Shorthand)

remove option from second select if already selected [Ordered multiple selection]

I had select option repeater.
What I want is that when I selected johndoe at the first option, it will no longer display on the second select option.
here's my html
<div id="app">
<h1>Vue JS Multiple Fields Repeater</h1>
<div class="col-sm-6">
<div class="panel panel-default relative has-absolute" v-for="(field, index) in users.usersRepeater">
<button #click="addUsersField" type="button">
Add
</button>
<button #click="deleteUsersField(index)" v-if="field != 0" type="button">
Delete
</button>
<div class="panel-body has-absolute">
<div class="form-group">
<label for="users" class="control-label col-sm-3 text-left">Users {{field}}</label>
<select :name="'users'+index"
class="form-control"
id="users">
<option value="" hidden>Select User</option>
<option value="1">John Doe</option>
<option value="2">Mark Doe</option>
<option value="3">Mae Doe</option>
<option value="4">John Smith</option>
<option value="5">Mae Smith</option>
</select>
</div>
</div>
</div>
</div>
</div>
here's my vue.js
new Vue({
el: '#app',
data: {
users: {
usersRepeater: [{ user: '' }]
}
},
methods: {
addUsersField: function() {
this.users.usersRepeater.push({
user: ''
});
},
deleteUsersField: function(index) {
this.users.usersRepeater.splice(index, 1);
},
}
});
here's the fiddle -> https://jsfiddle.net/0e3csn5y/23/
Ok I have this working now. I liked the question because making an ordered selection is a generic case. But, for me anyway, it wasn't straightforward. The breakthrough was realising that, when you number the choices, the whole state of the component could be encapsulated in one array, allUsers. Available users and choices then become computed properties, based on this array. Moral of the story: get your store right, with no interactions between elements of the store.
My answer weighs in at 130 lines. How long and hard would this be without Vue? Mind boggles.
Stack wants me to post some code, so here's the computed property that generates an array of choices made, in order of their priority, from the all users array...
choices(){
return this.store.allUsers.map((aUser,index)=>{
if(aUser.selection != null)
return {idxAllUsers : index, selection: aUser.selection};
else
return null;
})
.filter(aSelection=>aSelection != null)
.sort((a,b)=>{return a.selection - b.selection})
.map(a=>a.idxAllUsers);
},
I found this one very helpful.