link1507 link1508 link1509 link1510 link1511 link1512 link1513 link1514 link1515 link1516 link1517 link1518 link1519 link1520 link1521 link1522 link1523 link1524 link1525 link1526 link1527 link1528 link1529 link1530 link1531 link1532 link1533 link1534 link1535 link1536 link1537 link1538 link1539 link1540 link1541 link1542 link1543 link1544 link1545 link1546 link1547 link1548 link1549 link1550 link1551 link1552 link1553 link1554 link1555 link1556 link1557 link1558 link1559 link1560 link1561 link1562 link1563 link1564 link1565 link1566 link1567 link1568 link1569 link1570 link1571 link1572 link1573 link1574 link1575 link1576 link1577 link1578 link1579 link1580 link1581 link1582 link1583 link1584 link1585 link1586 link1587 link1588 link1589 link1590 link1591 link1592 link1593 link1594 link1595 link1596 link1597 link1598 link1599 link1600 link1601 link1602 link1603 link1604 link1605 link1606 link1607 link1608 link1609 link1610 link1611 link1612 link1613 link1614 link1615 link1616 link1617 link1618 link1619 link1620 link1621 link1622 link1623 link1624 link1625 link1626 link1627 link1628 link1629 link1630 link1631 link1632 link1633 link1634 link1635 link1636 link1637 link1638 link1639 link1640 link1641 link1642 link1643

[Vue.js] How to get values of checked multiple checkbox and also checked checkboxes dynamically from database. facing trouble to operate simultaneously

vue.jsjs: if i get values of checked box by v-model but not set checkbox checked dynaiclly from database. On other hand if i can set chechbox checked dynamically but not able to get values of checked checkbox.

With this i get value of checked checkbox but not set checked dynamically

<li v-for=”swv in appointment.switch_shift “ >
<input type=”checkbox” :value=”swv.id” v-model=”checkshifts”
@change=”shiftSwitch()” />
<label > { swv.name}</label>
</li>

-—————————————
data : {1: {id: 1, name: “Morning”, active: true}, 2: {id: 2, name: “Afternoon”, active: false},}

if set checkbox checked dynamically with the following code but not able get values of checked check box.

<li v-for=”swv in appointment.switch_shift “ >
<input type=”checkbox” v-model=”swv.active”
@change=”shiftSwitch()” />
<label > { swv.name}</label>
</li>

I expected that both operation should worked together. thanks

Solution :

Firstly, updating database dynamically in such a way is not a very good idea, because it’s an asynchronous operation and you can have a loads of concurrency issues with unpredictable results if someone checks/unchecks the checkbox several times before the previous update process is not finished. So you’d better have a special button to update the data an block the UI during update.

But if you insist on acting like this, the best way, I think, is to not set a model for a checked, but to bind it one way and on click toggle it directly in the model object. Something like this

<li v-for=”swv in appointment.switch_shift “ >
<input type=”checkbox” :checked=”swv.active”
@click.prevent = “shiftSwitch(swv)” />
<label > { swv.name}</label>
</li>

and in component


methods:{
shiftSwitch:function(swv){
swv.active = !swv.active;

}
},

Solution 2:

yes i resolved this trouble Firstly set checkbox checked dynaiclly from database see the code below.

HTML

<li v-for=”swv in appointment.switch_shift “ >
<input class=”js-small” type=”checkbox” v-model=”swv.active”
@change=”shiftSwitch()” />
<label > { swv.name}</label>
</li>

**Data**
appointment.switch_shift: {
1: {id: 1, name: “Morning”, active: false}
2: {id: 2, name: “Afternoon”, active: true}
3: {id: 3, name: “Evening”, active: false}
}

see one thing when i checked any of checkbox of Morning , Afternoon, Evening then corresponds data row active become true like below
{id: 1, name: “Morning”, active: true}

and when uncheck then {id: 1, name: “Morning”, active: false}

use data of this.appointment.switch_shift to post for updation in database.

Method

shiftSwitch:function(id){
axios.post(this.switchshiftroute , {‘opd_id’:this.opdid , ‘shifts’: this.appointment.switch_shift})
.then( (response)=>{
this.getAppointments();
console.log(response.data);
}).catch((error)=>{
console.log(error);
})

}

[Vue.js] Make JSON visible from page without using imports in vuejs

I’m trying to make a simple webpage in Vuejs.

there is a Main.vue.js page, which has an import of a json file, and this json file contains certain parameters. One of them is an object array, each of its elements has a string indicating the page to be shown next in a sequence. The page also fetches data from that object.
So, the Main.vue.js can access to every field and object of the json file file and shows the pages (page1, page2, page3…) in a sequence, depending on the information stored at that object array.

These pages (page1, page2, page3…) need to be generic, with no reference to a specific json file, and need to show information from that specific object of the array of the json.

I know how to pass data to pages via URL (so, for example, “page1” knows which element of the array has to fetch info from because Main.vue.js specifies it in the URL) but I don’t know how can I make MyJson accesible to “page1”, “page2” without making an import sentence of MyJson in each page.

I do not have backend or something similar, just a frontend which executes entirely in the browser.

I was wondering whether there is any way of accessing MyJson from page1, page2, page3… without having a backend.

Many thanks in advance

Regards

Miguel

there is tried it by passing info via URL, but it didn’t work out as expected.

PS: This is my json

MyJson.json
{
“id”=”whatever”
“text”=”whatever”
“myArray”=[
{
“whichPageHasToRenderMe”=”page1”
“myData”= …

},
{
“whichPageHasToRenderMe”=”page2”
“myData”= …

},
{
“whichPageHasToRenderMe”=”page1”
“myData”= …

},
{
“whichPageHasToRenderMe”=”page1”
“myData”= …

}
]
}

Solution :

If you serve the json file as static file. You can use fetch api or any other fetch library to access the json file.

fetch(‘path/to/your.json’)
.then(response => response.json())
.then(data => {
// do something
})

See more about Fetch API.

In case you want to pass some json data as url query. Don’t forget to use encodeURIComponent or btoa to escape some special characters.

window.open(‘/some/path?data=’ + encodeURIComponent(JSON.stringify(data)))

Solution 2:

there is solved it by using Vuex and setting the JSON in the state of Vuex, so it can be accessible from other pages.

[Vue.js] XMLHttpRequest blocked by CORS Policy on axios.post

The axios.post (code below) must send data to url api/add-todo, but I get these errors:

axios.post(‘http://localhost/vueoctober/todo/api/add-todo', todo).then(function (response) {
console.log(response);
}).catch(function(error) {
console.log(error);
});

The route api/add-todo is handled with October method Route::get() (https://octobercms.com/docs/services/router). Why is it not found?

If I change axios.post to axios.get it will be working! But I need post data, not get.

What I tried:

1) I tried to add these headers to .htaccess:

Header add Access-Control-Allow-Origin “*“
Header add Access-Control-Allow-Headers “origin, x-requested-with, content-type”
Header add Access-Control-Allow-Methods “PUT, GET, POST, DELETE, OPTION”

It’s working only for axios.get. The axios.post is still blocking.

2) I added Header set Access-Control-Allow-Origin “*“ to httpd.conf.

vue.js app is serving at port 8080, therefore axios.post url can’t be relative.

Solution :

Look at the error message carefully, it says the response to the preflight request didn’t have an HTTP ok status.

Clearly, the server-side code doesn’t have a route handler for the OPTIONS request, so you need to add one.

As an aside, after the browser gets a successful OPTIONS response, it will make the POST request but you said:

The route api/add-todo is handled with October method Route::get()

You’ll need to use Route::post() to handle that.

Solution 2:

Hours of googling and I got answer…

1) Install plugin Cross-Origin Resource Sharing (CORS).

2) In htaccess of vue.js app add:

Header set Access-Control-Allow-Origin ‘*‘
Header set Access-Control-Allow-Headers “origin, x-requested-with, content-type”
Header set Access-Control-Allow-Methods “PUT, GET, POST, DELETE, OPTION”

NOTICE! Write SET not ADD!

That’s it.

[Vue.js] Returning variables from a Vue.js directive

vue.js.js has a built-in directive called v-for which is used to literate over list.

HTML code

<ul id=”example-1”>
<li v-for=”item in items”>
{ item.message }
</li>
</ul>

Script code

var example1 = new Vue({
el: ‘#example-1’,
data: {
items: [
{ message: ‘Foo’ },
{ message: ‘Bar’ }
]
}
})

Here, by using the v-for directive, the elements in items array are returned as variables named item. Here, the variables returned by this v-for directive, can be used in the html DOM. How do I create such a custom directive which returns a variable to the html DOM?

Note: I did search for v-for directives source code in the source code of vuejs, but could not find. Please help me to get this as when very new to vuejs. Thank you.

Edit:

What I currently have?

There are 3 similar input groups for inputting ‘First Name’, ‘Last Name’ and ‘Address’ respectively. Each input field has label, state, disabled, value, and max properties which are stored in a inputOptions array.

<!– HMTL –>
<b-input-group :prepend=”inputOptions.firstName.label”>
<b-form-input
:state=”inputOptions.firstName.state”
v-model=”inputOptions.firstName.value”
:disabled=”inputOptions.firstName.disabled”
:maxlength=”inputOptions.firstName.max”
\></b-form-input>
</b-input-group>

<b-input-group :prepend=”inputOptions.lastName.label”>
<b-form-input
:state=”inputOptions.lastName.state”
v-model=”inputOptions.lastName.value”
:disabled=”inputOptions.lastName.disabled”
:maxlength=”inputOptions.lastName.max”
\></b-form-input>
</b-input-group>

<b-input-group :prepend=”inputOptions.address.label”>
<b-form-input
:state=”inputOptions.address.state”
v-model=”inputOptions.address.value”
:disabled=”inputOptions.address.disabled”
:maxlength=”inputOptions.address.max”
\></b-form-input>
</b-input-group>

//Script

inputOptions: {
firstName: {
label: ‘First Name’,
state: true,
value: ‘Foo’,
disabled: true,
max: 45
},
lastName: {
label: ‘Last Name’,
state: true,
value: ‘Bar’,
disabled: true,
max: 45
},
address: {
label: ‘Address’,
state: false,
value: ‘Foo, Bar.’,
disabled: true,
max: 255
},
}

What I needed to achieve

For each input-group field, it is needed to provide the property names one by one. Assume that there is created a vue.js directive called mydirective and code is simplified as follows.

<!– HMTL –>
<b-input-group v-mydirective=”inputOptions.firstName as myProperty” :prepend=”myProperty.label”>
<b-form-input
:state=”myProperty.state”
v-model=”myProperty.value”
:disabled=”myProperty.disabled”
:maxlength=”myProperty.max”
\></b-form-input>
</b-input-group>

<b-input-group v-mydirective=”inputOptions.lastName as myProperty” :prepend=”myProperty.label”>
<b-form-input
:state=”myProperty.state”
v-model=”myProperty.value”
:disabled=”myProperty.disabled”
:maxlength=”myProperty.max”
\></b-form-input>
</b-input-group>

<b-input-group v-mydirective=”inputOptions.address as myProperty” :prepend=”myProperty.label”>
<b-form-input
:state=”myProperty.state”
v-model=”myProperty.value”
:disabled=”myProperty.disabled”
:maxlength=”myProperty.max”
\></b-form-input>
</b-input-group>

//Script

inputOptions: {
firstName: {
label: ‘First Name’,
state: true,
value: ‘Foo’,
disabled: true,
max: 45
},
lastName: {
label: ‘Last Name’,
state: true,
value: ‘Bar’,
disabled: true,
max: 45
},
address: {
label: ‘Address’,
state: false,
value: ‘Foo, Bar.’,
disabled: true,
max: 255
},
}

Solution :

HTML

In the template, iterate over the multiple inputOptions using v-for like:

<div id=”app”>
<b-input-group v-for=”option in inputOptions” :key=”option.label” :option=”option” />
</div>

SCRIPT

Create custom components for the group, input, and label, like:

Vue.component(‘b-label’, {
props: [‘label’],
template: ‘<div>{ label }</div>’
})

Vue.component(‘b-form-input’, {
props: [‘state’, ‘value’, ‘disabled’, ‘maxlength’],
template: ‘<input type=”text” :value=”value” />’
})

Vue.component(‘b-input-group’, {
props: [‘option’],
template:
`<div>
<b-label :label=”option.label” />
<b-form-input
:state=”option.state”
v-model=”option.value”
:disabled=”option.disabled”
:maxlength=”option.max” />
</div>`
})

FIDDLE

Here is a demo on JSFiddle

This is a basic example of how to use components. Whatever transformations you need to do to the strings can be done in the associated components, via computed properties or methods. You can see a demo by clicking the link where I use a computed property to transform the labels into lowercase. That should be enough to get you going.

[Vue.js] Vue Filter returns an array, how to get the first element

Lets say there is a custom filter cf.js that needs to return multiple values in an array like this:

export default (value) => {
var a = 5
var b = 3

return [a, b]
}

In my vue.js file, how can I display only the first element? I thought something like below would work but they do not

{ myvar | cf[0] } or { (myvar | cf)[0] }

Solution :

You’re limited by the filter syntax in the vue.js template, so what you tried won’t work.

A filter doesn’t make sense in the example you’ve given since it doesn’t make use of the argument.

But anyway, to answer the question, two options come to mind:

1. Define another filter to get the first element

{ myvar | cf | first }

export default function first(value) {
return value[0]
}

2. Call the filter function manually

This will allow you to do what you want with the filter result since you’re just calling it as if it were any other function.

{ $options.filters.cf(myvar)[0] }

[Vue.js] How to get the new data, when listening for changes at specific Firestore doc?

I need to get the new data, when something in a Firestore Doc changes (example Doc: “1234” in collection: “games”). Do I need to listen for updates with this code:

var doc = db.collection(‘games’).doc(‘1234’);

var observer = doc.onSnapshot(docSnapshot => {
console.log(`Received doc snapshot: ${docSnapshot}`);
// …
}, err => {
console.log(`Encountered error: ${err}`);
});

and then just run a function to get the new data where the

// …

is?

I tried to get the data with docSnapshot.data, but there just seems to be some info about firebase stuff, like the doc, that updated…

Is there a better way to do this, than what I showed above?

Solution :

You are correct; to listen to a document’s changes you should use the onSnapshot method on a given collection. To use the example:

db.collection(‘games’).doc(‘1234’).onSnapshot(doc => {
console.log(`Received data:`, doc.data());
}, err => {
console.log(`Encountered error:`, err);
});

[Vue.js] Laravel 5.8 and Vue.js components

I’m new to using Vue.js and when trying to learn how to implement a rating system for recipes in my Laravel application using vue-star-rating. The first thing to learn is how to build components and then include them in my blade views. Already here when running into trouble.
Trying to include the ExampleComponent.vue.js that comes with Laravel out of the box, but I don’t understand how when to include it in my blade file.
The top of my blade file looks like this

Blade view

<div class=”col-sm-6”>
<div class=”container recipeDesc”>
<h2 class=”text-center name”>{$recipe->title}</h2>
<div class=”stars”>
<i class=”fas fa-star”></i>
<i class=”fas fa-star”></i>
<i class=”fas fa-star”></i>
<i class=”fas fa-star”></i>
<i class=”fas fa-star”></i>
</div>

<div id=”app”>
<example-component></example-component>
</div>

Default JS file

/**
* First we will load all of this project’s JavaScript dependencies which
* includes vue.js and other libraries. It is a great starting point when
* building robust, powerful web applications using vue.js and Laravel.
*/

require(‘./bootstrap’);

window.vue.js = require(‘vue’);

/**
* The following block of code may be used to automatically register your
* vue.js components. It will recursively scan this directory for the Vue
* components and automatically register them with their “basename”.
*
* Eg. ./components/ExampleComponent.vue.js -> <example-component></example-component>
*/

// const files = require.context(‘./‘, true, /\.vue$/i)
// files.keys().map(key => Vue.component(key.split(‘/‘).pop().split(‘.’)[0], files(key).default))

Vue.component(‘example-component’, require(‘./components/ExampleComponent.vue’).default);

/**
* Next, we will create a fresh vue.js application instance and attach it to
* the page. Then, you may begin adding components to this application
* or customize the JavaScript scaffolding to fit the unique needs.
*/

const app = new Vue({
el: ‘#app’,
components: {
‘example-component’: require(‘./components/ExampleComponent.vue’),
}
});

How would I include the ExampleComponent.vue.js in this div? Having a hard time understanding this, even when reading the Vue.js documentation.
Thank you!

Solution :

If you still have the default js files of the Laravel installation, in the resources/js/app.js file you should have the following:

Vue.component(‘example-component’, require(‘./components/ExampleComponent.vue’).default);

const app = new Vue({
el: ‘#app’
});

The first line tells you that the name you should use for the example component is example-component, so to include it on the blade view, simply write this:

<example-component/>

The second block in the app.js file tells you that vue.js will work on an element with id=”app”, so you should make sure that you have an div with that id on the layout if you want to use vue.js components.

[Vue.js] How can I disabled button when the button is already clicked depends on its row?

to disabled button depends on the row that I clicked. The problem I received this error once I clicked a button.

“Property or method “$event” is not defined on the instance but
referenced during render. Make sure that this property is reactive,
either in the data option or for class-based components, by
initializing the property, Error in render: “TypeError: Cannot read
property ‘target’ of undefined”.

Can somebody help me how can I disable a button depends on the row I clicked on my table?

<tbody v-for=”(medicine, index) in gulod_medicines”>
<tr v-if=”index >= startIndex && index < endIndex”>
<td>{medicine.medicine_id}</td>
<td>{medicine.medicine_name}</td>
<td>{medicine.quantity}</td>
<td>{medicine.price}</td>
<td>{medicine.description}</td>
<td>{medicine.unit}</td>
<td><button @click=”gulodTransactionButton(medicine.medicine_id,medicine.medicine_name,medicine.price,medicine.description,medicine.unit)” v-on:disabled=”disableButton(index, $event)” data-toggle=”modal” data-target=”#myModal3” class=”btn bg-success text-light” class=”small-box-footer”><i class=”fas fa-plus”></i></button>
</td>
</tr>
</tbody>

//methods
disableButton : function(index, event) {
event.target.disabled = true;
}

Solution :

What I would do is add a key/value to medicine -> medicine.disabled who would be a boolean set to false by default. And when you click on the button you set this medicine.disabled to true.

In this case I would do :

<tbody v-for=”(medicine, index) in gulod_medicines”>
<tr v-if=”index >= startIndex && index < endIndex”>
<td>{medicine.medicine_id}</td>
<td>{medicine.medicine_name}</td>
<td>{medicine.quantity}</td>
<td>{medicine.price}</td>
<td>{medicine.description}</td>
<td>{medicine.unit}</td>
<td><button @click=”gulodTransactionButton(medicine.medicine_id,medicine.medicine_name,medicine.price,medicine.description,medicine.unit, index)” :disabled=”medicine.disabled” data-toggle=”modal” data-target=”#myModal3” class=”btn bg-success text-light” class=”small-box-footer”><i class=”fas fa-plus”></i></button>
</td>
</tr>
</tbody>

//methods
gulodTransactionButton : function (medicine_id, medicine_name, price, description, unit, index) {
this.gulod_medicines[index].disabled = true;
}

[Vue.js] Background image isn't showing up with css

This question already has an answer here:

Percentage Height HTML 5/CSS

6 answers

when trying to have a responsive full screen image background in my vue.js project and I can’t get the image to show up, and am not sure what is wrong, the majority of the examples there is seen use the css in my code example

<template>
<div id=”myDiv”>

</div>
</template>

<script>
</script>

<style scoped>

#myDiv {
/*background: url(‘../../Images/Toronto.jpg’);*/
background: url(‘https://images.pexels.com/photos/374870/pexels-photo-374870.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260');
height: 100%;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}

Solution :

HTML and body tag needs to have min-height: 100%.

If you cant do that, other option would be to apply min-height: 100vh and min-width: 100vw.

[Vue.js] How to remove inside space from v-btn in Vuetify.js?Hi there! Solving the problem

Hi there! [SOLVED]

I’m trying to recreate a page using Vue.js and Vuetify, but I don’t know how can I get the following result with my buttons:

This is my goal:
https://i.imgur.com/CGkrz6R.png

This is what there is now:
https://i.imgur.com/hKxkpOq.png

As you can see, I just need to remove the RED part inside the buttons, to make them stay smaller and together, as the following:
https://i.imgur.com/Q6StwEE.png

How can I remove this ‘inside padding’ from a v-btn?

This is my code:

<v-card>
<v-flex xs12>
<v-img src=”https://cdn.vuetifyjs.com/images/parallax/material2.jpg"/>
</v-flex>
<v-card-text>
{ card.content }
</v-card-text>
<v-card-actions>
<v-btn small outline color=”secondary” class=”ma-0 text-capitalize”>View</v-btn>
<v-btn small outline color=”secondary” class=”ma-0 text-capitalize”>Edit</v-btn>
</v-card-actions>
</v-card>

Thanks yall!!

Solution :

Vue.directive(‘no-padding’, {
bind: (el, bindings) {
el.style.padding = 0
}
})

<v-btn small no-padding outline color=”secondary” class=”ma-0 text-capitalize”>View</v-btn>

Solution 2:

Solving the problem

How to do it, thanks to Traxo.
Creating the following class it’s enough:

<style scoped>
.together{
min-width: 0
}
</style>

And then using it:

<v-btn small outline color=”secondary” class=”together ma-0 text-capitalize”>Edit</v-btn>