binding dynamic img in vuejs does not work for me - vue.js

I have a dynamic img being pulled from an api using vue.js. For some strange reason, the image won't bind. I have tried both :src. and :attr but neither works. The url does display in the vue console inside of the data but can't get the image to display on the page. any help will go a long way.
<html>
<head>
<style></style>
</head>
<body>
<div class="container">
<div id="editor">
<img v-bind:src="PictureURL" />
</div>
</div>
<script type="text/javascript" src="https://unpkg.com/vue#2.0.3/dist/vue.js"></script>
<script>
new Vue({
el: "#editor",
data: {
PictureUrl: "",
},
created: function() {
this.getCurrentUser();
},
methods: {
getCurrentUser: function() {
var root = 'https://example.com';
var headers = {
accept: "application/json;odata=verbose"
}
var vm = this;
var __REQUESTDIGEST = '';
$.ajax({
url: root + "_api/Properties",
type: 'Get',
headers: headers,
success: function(data) {
vm.PictureUrl = data.d.PictureUrl;
}
})
},
}
})
</script>
</body>
</html>

Change <img v-bind:src="PictureURL" /> to <img v-bind:src="PictureUrl" />, so that you match the data item name. Vue should be giving you an error in the console about this.

https://jsfiddle.net/kch7sfda/
Example here.
You can try to:
1. add v-if to img element
2. rename PictureUrl to pictureUrl (first lowercase letter)

Related

How to show comma separated value in a list using Vue.js

Below is my code in vue.js where I trying to display the out put in list type like. if user give out put in the form of comma separated.
vue.js,angular,react.js
Then I want the out put in this format
vue.js
angular
react.js
This I have implemented in JavaScript but trying to achieve in vue.js please help me to complete this
This is the updated code please tell me where I am going wrong
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/vue#2/dist/vue.js"></script>
</head>
<body>
<div id="demo">
<span v-for="(string, index) in lists">
<span>{{string}}</span><span v-if="index+1 < lists.length">, </span>
</span>
</div>
<script>
var demo = new Vue({
el: '#demo',
data: function() {
return {
string: ''
};
},
computed: {
lists: function(){
return this.string.split(",");
}
}
})
</script>
</body>
</html>
You could use a computed property for this in combination with split:
var demo = new Vue({
el: '#demo',
data: function() {
return {
string: ''
};
},
computed: {
lists: function(){
return this.string.split(",");
}
}
})

how can I pass the variables to my vuejs component

I am working on a vuejs repeatable component that will allow a user to due several things---enter a question and select the answer type from the drop down. Issue is based on the type, I need to display a select number of boxes if its multiple choices so it can update an array. I cannot figure out how or where to add this. I also need to make these variable (f1 and f2 dynamic) so that it can be reused at other times. So if its a single line choose f1 if it is multiple choice select f2. Someone please provide some direction
Vue.component('my-input', {
template: '<input v-attr="name: name" v-model="value" type="text">' + '<select>' + '<option value="type1">Multiple Choice</option>' + '<option value="type2">single line</option>' + '<option value="type3">multi-line</option>' + '</select><br>'+'<br>'+'</br>',
data() {
return {
value: '',
brand: 'multiple-choice',
options: ['option a, option b'] };
},
props: ['name'] });
new Vue({
el: '#app',
data: {
message:'',
inputs: [{ type: 'my-input' }]
},
mounted: function () {
this.getAllPages();
},
methods: {
addInput() {
this.inputs.push({ type: 'my-input' });
},
getAllPages: function () {
var vm = this;
$.ajax({
url: vm.config.domainRoot + "/_api/web/lists/getbytitle('" + vm.config.listName + "')/items",
type: 'Get',
headers: {
"Accept": "application/json; odata=verbose"
},
success: function (data) {
vm.pages = data.d.results;
console.log(vm.pages);
}
})
},
createCustomL:function(){
// Get filed collection
var fldCollection = oList.get_fields();
var f1 = clientContext.castTo(
fldCollection.addFieldAsXml('<Field Type="Text" DisplayName="NewField" Name="NewField" Required="True"/>', true, SP.AddFieldOptions.addToDefaultContentType),
SP.FieldText);
f1.set_title("q1");
f1.set_description(mydescription);
f1.update();
//Get filed collection
var fldCollection = oList.get_fields();
var f2 = clientContext.castTo(
oList.get_fields().addFieldAsXml('<Field Type="Choice" DisplayName="state" Name="fldchoice" />', true, SP.AddFieldOptions.addToDefaultContentType),
SP.FieldChoice);
var choices = Array("None", "California", "Colorado", "Connecticut", "Georgia", "Indiana");
f2.set_choices(choices);
f2.update();
}
}
});
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Vue.js repeater</title>
</head>
<body>
<!-- partial:index.partial.html -->
<div id="app">
<p>Enter your ques</p>
<component v-repeat="inputs" is="{{ type }}" name="inputs[]">
</component>
<button v-on="click: addInput">Add Question</button>
</div>
<br>
<button v-on:click="createCustom">Generate</button>
<!-- partial -->
<script src='https://cdnjs.cloudflare.com/ajax/libs/vue/0.12.8/vue.js'></script>
<script src="./script.js"></script>
</body>
</html>
I don't know if i understand correctly but if you want to pass different data to your component dynamically you can use props.
Take a look here https://v2.vuejs.org/v2/guide/components-props.html

Display JSON data in dialog with Vue.js

Using Vue.js, how do you use axios GET to display the response in a dialog?
I've tried the following:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<template>
<label>Protocol</label> Display protocol details from json
<div class="text-xs-center">
<br>
</div>
</template>
<script>
export default {
dialog3: false
created() {
let self = this
axios.get('http://localhost:4000/ap/device')
.then(function(response) {
self.fDModelName(response.data.result)
console.log(JSON.stringify(response.data.result))
})
},
methods: {
fDModelName: function(message) {
console.log("inside")
}
}
}
</script>
</body>
</html>
Create a data object where you have content as a key. On component created, you can assign it to the component's content value.
<template>
<div>
{{ content }}
</div>
</template>
<script>
export default {
data () {
return {
content: []
}
},
created () {
axios.get('http://localhost:4000/ap/device')
.then(function (response) {
this.content = response.data.result
})
}
}
</script>

multiple returns in a computed property

I am trying to set up multiple returns in a computed property. Not sure why this is not working. I also tried repurposing the arrow function. What I am trying to do here, is when the application loads trigger the 'nearby' method that does a bit of filtering and also trigger the sortedItems method. I tried moving things around but it breaks. Is it possible to call both of these in in the userFilterkey:['nearby', 'someFunc']. I thought it would be cool, but doesn't work. I basically just need to trigger nearby and sortedItems on load.
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/tachyons/css/tachyons.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div id="app">
<div class="container">
<button v-on:click="userFilterKey='nearby'" :class="{active: userFilterKey == 'nearby'}">Show Completed Tasks</button><br><br>
<div class="panel panel-default" v-for="item in sortedItems">
<div class="panel-heading">Tracking ID#{{item.TrackingID}} <span class="pull-right"><small>last modified</small> {{item.Modified | date}} | <small>Created</small> {{item.Created | date}}</span>
</div>
<span class="pull-right" style="padding-bottom:10px;">status <strong>{{item.status}}</strong></span>
</div>
</div>
</div>
<script type="text/javascript" src="https://unpkg.com/vue#2.0.3/dist/vue.js"></script>
<script>
new Vue({
el: "#app",
data: {
items: [],
userFilterKey:'nearby',
Title: ""
},
created: function() {
this.getData();
},
computed: {
sortedItems: function() {
return this[this.userFilterKey]
return this.items.sort((b, a) => new Date(a.Modified) - new Date(b.Modified));
},
itemFilter: function(){
return this[this.userFilterKey]
},
all: function(){
return this.items
},
nearby: function(){
alert("this")
return this.items.filter((items) => items.status=="New" | items.status=="in-progress")
}
},
filters: {
date: function(str) {
if (!str) {
return '(n/a)';
}
str = new Date(str);
return ((str.getMonth() < 9) ? '0' : '') + (str.getMonth() + 1) + '/' +
((str.getDate() < 10) ? '0' : '') + str.getDate() + '/' + str.getFullYear();
}
},
methods: {
getData: function() {
var root = 'https://example.com';
var headers = {
accept: "application/json;odata=verbose"
}
var vm = this;
$.ajax({
url: root + "_api/web/lists/getbytitle('Issues')/items?&$orderby=Created desc",
type: 'Get',
headers: headers,
success: function(data) {
vm.items = data.d.results;
console.log(vm.items)
}
})
}
}
})
</script>
</body>
A function will only reach one return so not sure what you're doing with sortedItems.
You would never be responsible for running nearby yourself. nearby runs whenever items changes. What you can do instead is have a single results computed property and do something like:
computed: {
results () {
return this.items
.filter(...)
.sort(...)
},
},
and use results in your template.
Also:
items.status=="New" | items.status=="in-progress"
should be:
items.status=="New" || items.status=="in-progress"
or even better:
items.status === "New" || items.status === "in-progress"`.

VueJS Filter Not Working

Why does filter do not work?
Error says: [Vue warn]: Failed to resolve filter: uppercase.
Not sure why but filter is not working.
Also, is this the best way to code this functionality? Are there any ways to do this, or the suggested ways? I'm not sure about using the $refs, maybe I can do this simpler, but with effective use of reusable components.
I'm trying to emulate an Ajax Response by using random message and status instead.
Link: JSBIN
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="app">
<login-alert ref="refalert"></login-alert>
<p>
<button #click="showme">Random Alert</button>
</p>
</div>
<script src=" https://unpkg.com/vue"></script>
<template id="template-alert">
<div v-if="showAlert">
<div :class="'alert alert-'+alertType+' alert-dismissible'" transition="fade">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true" v-on:click="close">×</button>
<h4>{{title|uppercase}}!</h4>
{{message}}
</div>
</div>
</template>
<script>
Vue.component('login-alert', {
template: '#template-alert',
data: function() {
return {
alertType : null,
title : null,
message : null,
showAlert : false
}
},
methods: {
close: function() {
this.alertType= null;
this.title= null;
this.message= null;
this.showAlert= false;
},
open: function(type, message) {
this.alertType= type;
this.title = type;
this.message= message;
this.showAlert= true;
}
}
});
var app = new Vue({
el: '#app',
methods: {
showme: function() {
var randomStatus = ['error', 'warning', 'success'];
var randomMessage = ['Lorem Ipsum', 'Adolor Sit Amet', 'Abed Meepo'];
var status = randomStatus[Math.floor(Math.random() * randomStatus.length)];
var message = randomMessage[Math.floor(Math.random() * randomMessage.length)];
this.$refs.refalert.open(status,message);
}
}
});
</script>
</body>
</html>
The uppercase filter has been removed in Vue.js 2.
https://v2.vuejs.org/v2/guide/migration.html#Built-In-Text-Filters-removed
You can simply use:
{{ text.toUpperCase() }}
As far as the structure of your code goes, something like this is probably better off in a method:
close: function() {
this.alertType= null;
this.title= null;
this.message= null;
this.showAlert= false;
}
Since you are duplicating it twice but just with different values.