vuetify.js how to add type="submit" to Loaders Button - laravel

i am new in Vuetify.js im trying to make submit Form using Loaders Button
i tryid but it not worked.
my tryid code
<v-btn
rounded
class="ma-2"
type="submit"
:loading="loading2"
:disabled="loading2"
color="primary"
#click="loader = 'loading2'"
>
Login
<template v-slot:loader>
<span>Loading...</span>
</template>
</v-btn>
my main code
<template>
<v-row justify="center">
<v-col cols="12" sm="6">
<form #submit.prevent="submit">
<v-card ref="form">
<v-card-text>
<h3 class="text-center">Login</h3>
<v-divider class="mt-3"></v-divider>
<v-col cols="12">
<v-img
:src="require('#/assets/1.png')"
class="my-3"
contain
height="80"
/>
</v-col>
<v-col cols="12" sm="12">
<v-text-field
v-model.trim="form.mobile_number"
type="number"
label="Mobile No"
solo
autocomplete="off"
></v-text-field>
<small class="form-text red--text" v-if="errors.mobile_number">{{
errors.mobile_number[0]
}}</small>
</v-col>
<v-col cols="12">
<v-text-field
v-model.trim="form.password"
type="password"
label="Password"
solo
autocomplete="off"
append-icon="mdi-eye"
></v-text-field>
<small class="form-text red--text" v-if="errors.password">{{
errors.password[0]
}}</small>
</v-col>
</v-card-text>
<v-divider class="mt-12"></v-divider>
<v-card-actions>
<div class="text-center">
<v-btn
rounded
color="primary"
dark
to="/AppMain/UserRegisterPage"
nuxt
>Register</v-btn
>
</div>
<v-spacer></v-spacer>
<div class="text-center">
<v-btn
rounded
class="ma-2"
type="submit"
:loading="loading2"
:disabled="loading2"
color="primary"
#click="loader = 'loading2'"
>
Login
<template v-slot:loader>
<span>Loading...</span>
</template>
</v-btn>
<!-- <v-btn rounded type="submit" color="primary" dark>Login</v-btn> -->
</div>
</v-card-actions>
</v-card>
</form>
</v-col>
</v-row>
</template>
<script>
export default {
middleware: ["guest"],
data() {
return {
loader: null,
loading: false,
loading2: false,
loading3: false,
loading4: false,
loading5: false,
form: {
mobile_number: "",
password: "",
},
};
},
watch: {
loader() {
const l = this.loader;
this[l] = !this[l];
setTimeout(() => (this[l] = false), 3000);
this.loader = null;
},
},
methods: {
async submit() {
await this.$auth.loginWith("local", {
data: this.form,
});
this.$router.push("/");
},
},
};
</script>
<style>
.custom-loader {
animation: loader 1s infinite;
display: flex;
}
#-moz-keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
#-webkit-keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
#-o-keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
#keyframes loader {
from {
transform: rotate(0);
}
to {
transform: rotate(360deg);
}
}
</style>

You create and use the variable "loading" but never set it.
For resolve your problem, please, add in your #click method something like
loading = true
....
and then make loading same to false, for stop the loading style of your button.
Good luck!

Related

I there any way to filter the data in table when clicked one part of section pie chart? laravel vue

i had implement the click event for the chart and it can filter the data in the table. But now i face the problem of the data does not return all the data in the table when click outside the chart are.How can make it return all the data when click outside the area chart? Thank you. Any help will be appreciated.
<script>
import VueApexCharts from "vue-apexcharts";
import faker from "faker";
export default {
components: {
VueApexCharts,
},
data() {
return {
Lists: [],
selectedData:{},
search1:'',
clicked:'',
clicked1:'',
isLoading1:true,
currentPage: 1,
series: [300, 200, 49, 100,290, 228, 119, 55],
chartOptions: {
colors: ['#7961F9', '#FF9F43', '#196EB0', '#2EAB56','#df87f2','#057FF2', '#14DA6E','#FF5500'],
legend: {
fontSize: "14px",
position: "top",
},
dataLabels: {
enabled: true,
minAngleToShowLabel: 0,
distributed: false,
style: {
colors: ['#111'],
},
background: {
enabled: true,
foreColor: '#fff',
borderWidth: 0,
}
},
chart: {
width: 500,
type: 'pie',
events: {
legendClick: (chartContext, seriesIndex,w, config) => {
this.clicked = w.config.labels[seriesIndex];
console.log(this.clicked);
console.log(seriesIndex);
},
dataPointSelection: (event,chartContext,config) => {
this.clicked1 = config.w.config.labels[config.dataPointIndex];
console.log(this.clicked1);
},
},
},
labels: ['Private', 'Local','Dental', 'Government','Cyber Security', 'Health', 'Foreign','Medical'],
responsive: [
{
breakpoint: 480,
options: {
legend: {
position: "bottom",
fontSize: "12px",
},
},
},
],
},
}
},
created() {
this.getData();
this.getData1();
},
computed:{
filterLists(){
let list = this.Lists;
if(this.search1 !=''){
list = list.filter((tr)=>{
return tr.agency.toUpperCase().includes(this.search1.toUpperCase())
});
}
if (this.clicked !=''&& this.clicked){
list = list.filter((tr)=>{
return tr.projectCategory.toUpperCase().includes(this.clicked.toUpperCase())
});
}
if (this.clicked1 !=''&& this.clicked1){
list = list.filter((tr)=>{
return tr.projectCategory.toUpperCase().includes(this.clicked1.toUpperCase())
});
}
return list;
},
},
methods: {
next(page) {},
getData1() {
this.isLoading1 = true;
for (let i = 0; i < this.randInt(8, 4); i++) {
let index = Math.floor(Math.random() * 2);
let projectCategory = this.rotate([
'Private', 'Local','Dental', 'Government','Cyber Security', 'Health', 'Foreign','Medical'
]);
this.Lists.push({
projectCategory:projectCategory,
project_name: faker.company.catchPhrase(),
agency: faker.company.companyName(),
logo: faker.image.abstract(),
});
}
this.maxPage = 2;
this.isLoading1 = false;
},
next1(page) {
if (page == -2) {
this.currentPage = 1;
} else if (page == -3) {
this.currentPage = this.maxPage;
} else {
if (
this.currentPage + page < 1 ||
this.currentPage + page > this.maxPage
) {
return;
}
this.currentPage += page;
}
this.showLoader("#card-list");
this.Lists = [];
this.isLoading1 = true;
setTimeout(() => {
this.closeLoader("#card-list");
this.getData1();
}, 1500);
},
},
};
</script>
<style>
#card > header{
padding: 1.5rem 2rem;
background-color: #2E3839;
}
#card{
--tw-bg-opacity: 1;
background-color: rgba(249, 250, 251, var(--tw-bg-opacity));
}
.con-img.vs-avatar--con-img img {
object-fit: cover !important;
}
.apexcharts-toolbar {
position:absolute;
margin-right:12px;
}
vs-button.btn:hover{
background-color: rgba(255,255,255,0);
cursor: pointer;
}
</style>
<template>
<div class="mb-base">
<div class="vx-row mb-base">
<div class="vx-col 2/3 w-full mb-base">
<vs-card
id="card"
class="vs-con-loading__container h-full"
>
<template slot="header">
<div class="flex">
<div>
<img
src=""
alt="Info"
class="h-12 inline-block mr-4 object-scale-down"
/>
</div>
<div class="flex flex-col justify-center w-full text-start">
<h3 class="text-white">Source of Fund</h3>
<span class="text-sm text-white">Based on Total Billed (Yearly)</span>
</div>
<div>
</div>
</div>
</template>
<div class="flex flex-wrap mt-2">
<div class="lg:w-1/3 w-full">
<vue-apex-charts
type="donut"
:options="chartOptions"
:series="series"
width="100%"
class="items-center justify-center flex mt-16 content-center"
/>
</div>
<div class="lg:w-2/3 w-full lg:pl-6 pl-0 mt-6">
<div class="flex justify-end items-end">
<vx-input-group class="mb-base lg:w-1/2 w-full">
<template slot="append">
<div class="append-text btn-addon">
<vs-button color="#A9A9A9"><i class="fas fa-search"></i></vs-button>
</div>
</template>
<vs-input
v-model="search1"
placeholder="Project Code or name"
/>
</vx-input-group>
</div>
<div id="card-list">
<vs-list v-if="!isLoading1">
<vs-list-item
v-for="(tr, index) in filtertLists"
:key="index"
class="hover:shadow cursor-pointer text-base mb-4"
>
<template slot="title">
<div
class="flex flex-col ml-2 cursor-pointer"
>
<div class="font-bold">{{ tr.project_name }}</div>
<div>{{ tr.agency }}</div>
</div>
</template>
<template slot="avatar">
<vs-avatar :src="tr.logo"></vs-avatar>
</template>
{{ tr.projectCategory }}
</vs-list-item>
<div v-if="!filterLists.length" class="flex">
<div class="items-center justify-center text-lg font-bold">No record...</div>
</div>
</vs-list>
<div v-else class="flex">
<div class="items-center justify-center">Fetching data...</div>
</div>
</div>
<div class="flex justify-end gap-4">
<div class="flex items-center justify-center text-sm">
Page {{ currentPage }} of
{{ maxPage }}
</div>
<div>
<vs-button
type="filled"
color=" rgba(243, 244, 246)"
class="w-10 mr-2 rounded-md bg-gray-400 text-black btn hover:text-black"
#click="next1(-1)"
>
<i class="fas fa-chevron-left"></i>
</vs-button>
<vs-button
type="filled"
color=" rgba(243, 244, 246)"
class="w-10 mr-2 rounded-md bg-gray-400 text-black btn"
#click="next1(1)"
>
<i class="fas fa-chevron-right"></i>
</vs-button>
</div>
</div>
</div>
</div>
</vs-card>
</div>
</div>
</div>
</template>
I am working with laravel vue pie chart. is there any way to filter the data in the table when click the element of pie chart. For example, when clicked the section pie chart, the table will be filter and display the the data in the table under that section..Any help will be appreciated
It is not possible to point directly to a solution as you have given so little detail. I will still try to explain the logic. For this process, you will get an input from the screen working with Vue.js and you will manipulate a data displayed on Vue.js.
So first; you need to know which part of your pie chart clicked on. I assume the pie chart you are using on your project have some events which triggered when you interact with charts. You will listen that event and catch the value of clicked item.
Now you have the value of clicked item and you need to filter your results by that.
To accomplish that you can use Vue.js Computed Properties and Watchers :https://v2.vuejs.org/v2/guide/computed.html
Lets say you have your data on your Vue.js application:
data () {
return {
clickedItem: null,
itemsOnTable: [ ... ]
}
}
You have all your table content in itemsOnTable and selected item's data in clickedItem
You can use computed to filter your data:
data () {
return {
clickedItem: null,
itemsOnTable: [ ... ]
}
},
computed: {
// filter itemsOnTable if clickedItem have any value
filteredItems: function () {
if(this.clickedItem==null) return this.itemsOnTable;
return this.itemsOnTable.filter(item => item.column = this.clickedItem);
}
}
Now in your Vue.js component you can directly use filteredItems for your table elements v-for
<table>
<tr v-for="items in filteredItems">
<td>{{ item.column }}</td>
<!-- other columns -->
</tr>
</table>
This examples explains basics of interactions and computed properties and aims to help you to understand basics.

Usng Vuetify v-data-table how to set chip color based and value and item

I would like to set the chip color for the value of an item in a v-data-table using both the items value and the items id. So i want to set values under 0.5 to red only if the id is "Cl2". ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[enter image description here][1]
Here's my table and code:
[1]: https://i.stack.imgur.com/Sp8DQ.jpg
<template>
<v-data-table
:headers="headers"
:items="parameters"
class="elevation-1"
hide-default-footer
calculate-widths
dense
>
<template v-slot:top>
<v-toolbar flat color="white">
<v-toolbar-title>Parameter Input {{ scanSite }} </v-toolbar-title>
<v-divider class="mx-4" inset vertical></v-divider>
<v-spacer></v-spacer>
<v-dialog v-model="dialog" persistent>
<v-card
max-width="315px"
max-height="240px"
style="position: absolute; top: 90px; left: 30px; right: 30px;"
>
<v-card-title>
<span class="">{{ editedItem.name }} - Enter/Edit Values </span>
</v-card-title>
<v-card-text class="card-text py-0 my-0">
<v-container>
<v-row class="row py-0">
<v-col cols="5">
<v-text-field
v-model="editedItem.value"
label="Value"
></v-text-field>
</v-col>
<v-col cols="5">
<v-text-field
v-model="editedItem.sid"
label="Save ID"
></v-text-field>
</v-col>
</v-row>
</v-container>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="blue darken-1" text #click="close">Cancel</v-btn>
<v-btn color="blue darken-1" text #click="save">Save</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-toolbar>
</template>
<template v-slot:[`item.actions`]="{ item }">
<v-icon small class="mr-2" #click="editItem(item)">
mdi-pencil
</v-icon>
<v-icon small class="mr-2" #click="addItem(item)">
mdi-plus
</v-icon>
</template>
<template v-slot:[`item.analyse`]="{ item }">
<v-simple-checkbox
color="green"
v-model="item.analyse"
></v-simple-checkbox>
</template>
<template v-slot:[`item.value`]="{ item }">
<v-chip :color="getColor(item.value)" small dark>{{ item.value }}</v-chip>
</template>
</v-data-table>
</template>
<script>
import { mapState } from "vuex";
export default {
data() {
return {
scanSite: "",
dialog: false,
selected: [],
editedItem: {
name: "",
value: 0,
sid: 0
}
};
},
computed: {
...mapState(["scanSite", "headers", "parameters", "sites"])
},
methods: {
editItem(item) {
this.editedIndex = this.parameters.indexOf(item);
this.editedItem = Object.assign({}, item);
this.dialog = true;
},
save() {
Object.assign(this.parameters[this.editedIndex], this.editedItem);
this.dialog = false;
},
close() {
this.dialog = false;
},
getColor(value, id) {
console.log(value, id);
if (value > 0.5 && id == "Cl2") return "red";
else if (value > 10) return "orange";
else return "green";
},
addItem(item) {
this.editedIndex = this.parameters.indexOf(item);
this.editedItem = Object.assign({}, item);
this.parameters.push(this.editedItem);
this.dialog = true;
}
}
};
</script>
I think you need to set both paramareters when you call your getColor() function, like so
<v-chip :color="getColor(item.value, item.id)" small dark>{{ item.value }}</v-chip>
or pass the whole item object and manage it in the getColor() function
<v-chip :color="getColor(item)" small dark>{{ item.value }}</v-chip>
...
getColor(item) {
if (item.value > 0.5 && item.id == "Cl2") return "red";
else if (item.value > 10) return "orange";
else return "green";
},

Vee Validate - Sever side validation and front end validation

I have a laravel lumen endpoint which i return errors from. The issue i am facing is that vee validate is not setting the errors?
<template>
<v-container fluid class="ma-0 pa-0">
<v-card route>
<v-card-title class="primary white--text">Create New User</v-card-title>
<v-row align="center" :class="{'px-6': $vuetify.breakpoint.xs, 'pa-8': $vuetify.breakpoint.smAndUp}">
<v-row justify="space-between" v-if="!isCreating">
<v-col cols="12" md="4">
<v-row align="center" justify="center" class="mb-4">
<v-avatar color="primary" size="128">
<v-icon size="48" dark v-if="!userAvatarName()">mdi-account</v-icon>
<span
v-if="userAvatarName()"
class="white--text display-2"
>{{ userAvatarName() }}</span>
</v-avatar>
</v-row>
</v-col>
<v-col>
<ValidationObserver v-slot="{ invalid, validated, passes }" ref="provider">
<v-form #submit.prevent="passes(handleSubmit)">
<v-row >
<v-col class="py-0">
<VTextFieldWithValidation vid="username" rules="required" v-model="newUser['username']" label="Username" />
</v-col>
</v-row>
<v-row >
<v-col class="py-0">
<VTextFieldWithValidation vid="email" rules="required|email" v-model="newUser['email']" label="Email address" />
</v-col>
</v-row>
<v-row >
<v-col class="py-0">
<VTextFieldWithValidation rules="required" v-model="newUser['first_name']" label="First name" />
</v-col>
<v-col class="py-0">
<VTextFieldWithValidation rules="required" v-model="newUser['last_name']" label="Last name" />
</v-col>
</v-row>
<v-row >
<v-col class="py-0 mt-4">
<ValidationProvider name="role" rules="required" v-slot="{ errors, valid }">
<v-select
:error-messages="errors"
:success="valid"
:items="roles"
item-text="name" item-value="value"
label="Role"
outlined
v-model="newUser['role']"
></v-select>
</ValidationProvider>
</v-col>
</v-row>
<v-divider></v-divider>
<v-row >
<v-col class="py-0">
<v-switch
v-model="newUser['send_activation']"
label="Send Activation Email"
></v-switch>
</v-col>
</v-row>
<v-row v-if="!newUser['send_activation']">
<v-col class="py-0">
<ValidationProvider name="password" :rules="!newUser['send_activation'] ? 'required|min:8' : ''" v-slot="{ errors, valid }">
<v-text-field
outlined
counter
:error-messages="errors"
:success="valid"
label="Password"
:append-icon="userPasswordVisibility.password_current_show ? 'mdi-eye' : 'mdi-eye-off'"
:type="userPasswordVisibility.password_current_show ? 'text' : 'password'"
#click:append="userPasswordVisibility.password_current_show = !userPasswordVisibility.password_current_show"
v-model="newUser['password']"
></v-text-field>
</ValidationProvider>
</v-col>
<v-col class="py-0">
<ValidationProvider name="confirmation" :rules="!newUser['send_activation'] ? 'required|password:#password' : ''" v-slot="{ errors, valid }">
<v-text-field
outlined
counter
:error-messages="errors"
:success="valid"
label="Password Confirmation"
:append-icon="userPasswordVisibility.password_new_show ? 'mdi-eye' : 'mdi-eye-off'"
:type="userPasswordVisibility.password_new_show ? 'text' : 'password'"
#click:append="userPasswordVisibility.password_new_show = !userPasswordVisibility.password_new_show"
v-model="newUser['password_confirmation']"
></v-text-field>
</ValidationProvider>
</v-col>
</v-row>
<v-row class="mt-4">
<v-col class="py-0">
<v-btn text color="primary" :to="{ name: 'organisation/users', params: { org_id: organisation.id }}">Cancel</v-btn>
</v-col>
<v-col class="py-0 text-right">
<v-tooltip top>
<template v-slot:activator="{ on }">
<v-btn outlined color="primary" v-on="on" #click="passes(handleSubmit)" :disabled="invalid || !validated">Add New User</v-btn>
</template>
<span>Create new user and add them to <strong>{{organisation.name}}</strong>.</span>
</v-tooltip>
</v-col>
</v-row>
</v-form>
</ValidationObserver>
</v-col>
</v-row>
<v-row align="center" justify="center" v-if="isCreating">
<v-progress-circular
class="ma-5"
indeterminate
color="teal accent-1 darken-2" >
</v-progress-circular>
</v-row>
</v-row>
</v-card>
</v-container>
</template>
<script>
import { mapState } from 'vuex'
import { CREATE_USER } from '#/_apollo/GraphQL/userGraphQL'
import BreadcrumbsManager from '#/_util/breadcrumbManager'
import VTextFieldWithValidation from '#/components/inputs/VTextFieldWithValidation'
import { ValidationProvider, ValidationObserver } from "vee-validate"
export default {
name: 'CreateUser',
mixins: [BreadcrumbsManager],
props: [
'organisation'
],
components: {
ValidationObserver,
ValidationProvider,
VTextFieldWithValidation
},
data() {
return {
userPasswordVisibility: {
password_current_show: false,
password_new_show: false,
password_new_confirm_show: false
},
newUser: {
send_activation: 1
},
roles: [
{ name: 'Admin', value: 'organisation_admin' },
{ name: 'User', value: 'user' }
],
isCreating: false
}
},
computed: {
...mapState({
authUser: state => state.AUTH_STORE.authUser
}),
},
methods: {
userAvatarName() {
let name = '';
if (this.newUser['first_name']) {
name += `${this.newUser['first_name'].charAt(0).toUpperCase()}`
}
if (this.newUser['last_name']) {
name += `${this.newUser['last_name'].charAt(0).toUpperCase()}`
}
if (name !== '') {
return name
}
return false
},
async handleSubmit() {
this.isCreating = true
this.newUser['reseller_id'] = this.authUser.reseller_id
this.newUser['organisation_id'] = parseInt(this.$route.params.org_id)
this.newUser['send_activation'] = this.newUser['send_activation'] ? 1 : 0
var refs = this.$refs.provider
this.$apollo
.mutate({
mutation: CREATE_USER,
variables: this.newUser
})
.then(response => {
this.isCreating = false
const user = response.data.createUser
this.$store.commit('USER_STORE/CREATE_USER', response.data.createUser)
this.$toast.success('Successfully created new user.')
this.$router.push({ name: "organisation/users", params: { org_id: this.$route.params.org_id }}, () => {})
}).catch((error) => {
this.isCreating = false
this.$toast.error(error.graphQLErrors[0].extensions.code.message)
let errors = error.graphQLErrors[0].extensions.code.errors
console.log(errors)
refs.setErrors(errors)
console.log(refs)
})
refs.validate();
}
},
created() {
this.setBreadcrumbs([
{ text: 'Dashboard' , path: '/' },
{ text: 'Organisations' , path: '/organisation/all/' },
{ text: ':organisation' },
{ text: 'Users', path: `/organisation/${this.organisation.org_id}/users/` },
{ text: 'Create' }
])
this.replaceBreadcrumb({
find: ':organisation',
replace: { text: this.organisation.name, path: `/organisation/${this.organisation.org_id}` }
})
}
}
</script>
In the catch error on handle submit i can see the sever errors but my view wont show an error on the field?
I have took inspiration from the following from the docs:
https://codesandbox.io/s/veevalidate-backend-driven-validation-ynrp9?from-embed=&file=/src/App.vue
But to no avail.
Can anyone help?
It turns out apollo client is a promise which isnt resolved so the fix is:
const userResponse = await this.$apollo
.mutate({
mutation: CREATE_USER,
variables: this.newUser
})
.then(response => {
this.isCreating = false
const user = response.data.createUser
this.$store.commit('USER_STORE/CREATE_USER', response.data.createUser)
this.$toast.success('Successfully created new user.')
this.$router.push({ name: "organisation/users", params: { org_id: this.$route.params.org_id }}, () => {})
}).catch((error) => {
this.isCreating = false
this.$toast.error(error.graphQLErrors[0].extensions.code.message)
return error.graphQLErrors[0].extensions.code.errors.detail
})
this.$refs.provider.setErrors(userResponse);

Cannot read property 'reduce' of undefined in vue js

I am having problems with vue.js when I want to get data to edit and I got the following error:
cannot read property 'reduce' of undefined"
But when I use console.log, the data are correct. I try more time to change this.$set(this.$data, "form", res.returnsale) of fetchRuturn on my code and then I got this error.
<template>
<v-app>
<v-card class="mx-5 my-5">
<div class="teal darken-1">
<v-card-title class="white--text">Edit​ Return Sale</v-card-title>
</div>
<v-divider></v-divider>
<div class="px-5">
<p class="caption font-italic pt-5">The field labels marked with * are required input fields.</p>
<v-row>
<v-col md="6" cols="12">
<label for="reference_no" class="font-weight-bold">Reference No</label>
<v-text-field solo outlined dense v-model="form.reference_no"></v-text-field>
</v-col>
<v-col md="6" cols="12">
<label class="font-weight-bold">Location*</label>
<v-autocomplete item-value="address" item-text="address" solo outlined dense label="Business Location"
return-object v-model="form.location" :items="locations"></v-autocomplete>
</v-col>
<v-col md="6" cols="12">
<label class="font-weight-bold">Supplier</label>
<v-autocomplete :items="suppliers" item-text="name" item-value="name" solo outlined dense return-object
v-model="form.supplier" label="Select Supplier"></v-autocomplete>
</v-col>
<v-col md="6" cols="12">
<label class="font-weight-bold">Account</label>
<v-autocomplete :items="accounts" item-text="name" item-value="name" solo outlined dense return-object
v-model="form.account" label="Select Account"></v-autocomplete>
</v-col>
<v-col cols="12">
<label class="font-weight-bold">Select Product</label>
<div>
<v-autocomplete dense solo item-text="name" item-value="name" return-object :items="products"
#input="addTocart"></v-autocomplete>
</div>
</v-col>
</v-row>
<div>
<label class="font-weight-bold mb-3">Product Table</label>
<table class="tableReturn">
<thead>
<tr class="tableReturn--header">
<td>Name</td>
<td>Code</td>
<td>Quantity</td>
<td>Unit Price</td>
<td>Discount</td>
<td>Total</td>
<td>Actions</td>
</tr>
</thead>
<tbody>
<tr class="tableReturn--td" v-for="(item, index) in form.items" :key="index">
<td>{{item.name}}</td>
<td>{{item.code}}</td>
<td>
<input type="number" class="table-quantity" v-model="form.items[index].quantity" />
</td>
<td>
<input type="number" class="table-quantity" v-model="form.items[index].unit_price"
placeholder="0.00" />
</td>
<td>
<input type="number" class="table-quantity" v-model="form.items[index].discount" placeholder="0.00" />
</td>
<td>USD {{ discountedPrice(item) | formatMoney }}</td>
<td>
<v-btn small color="red" outlined #click="removeItem(index)">
<v-icon>mdi-delete</v-icon>
</v-btn>
</td>
</tr>
<tr>
<td class="py-5" colspan="2">Total</td>
<td colspan="3">{{ Qty }}</td>
<td>USD {{ Total | formatMoney }}</td>
</tr>
</tbody>
</table>
</div>
<v-row>
<v-col md="6" cols="12">
<div class="d-flex flex-column mb-5">
<label for="" class="font-weight-bold">Return Note</label>
<textarea cols="30" rows="5" class="textarea" v-model="form.return_des"></textarea>
</div>
</v-col>
<v-col md="6" cols="12">
<div class="d-flex flex-column mb-5">
<label for="" class="font-weight-bold">Staff Note</label>
<textarea cols="30" rows="5" class="textarea" v-model="form.staff_des"></textarea>
</div>
</v-col>
</v-row>
</div>
<v-btn #click.prevent="updateReturn" class="blue mx-5 darken-2 mb-5 grey--text text--lighten-4">
<v-icon>mdi-check</v-icon>Update
</v-btn>
</v-card>
</v-app>
</template>
<script>
import Vue from "vue";
let numeral = require('numeral');
Vue.filter('formatMoney', function (value) {
return numeral(value).format('00,00.00')
});
export default {
name: "editReturn",
created() {
this.fetchReturn();
this.fetchLocation();
this.fetchSupplier();
this.fetchAccount();
this.fetchProduct();
},
data() {
return {
form: {
items: []
},
suppliers: [],
locations: [],
products: [],
returnsale: [],
accounts: []
};
},
computed: {
Qty() {
return this.form.items.reduce((total, item) => {
return total + Number(item.quantity);
}, 0);
},
Total() {
return this.form.items.reduce((total, item) => {
let subtotal = (item.unit_price - (item.unit_price * item.discount) / 100) *
item.quantity
return total + subtotal;
}, 0);
}
},
methods: {
discountedPrice(product) {
return (
(product.unit_price -
(product.unit_price * product.discount) / 100) *
product.quantity
);
},
fetchLocation() {
this.$axios.$get(`api/location`)
.then(res => {
// this.locations = res.locations.data;
this.$set(this.$data, "locations", res.locations.data);
console.log(res);
})
.catch(err => {
console.log(err.response);
});
},
fetchSupplier() {
this.$axios.$get(`api/supplier`)
.then(res => {
this.$set(this.$data, "suppliers", res.suppliers.data);
console.log(res);
})
.catch(err => {
console.log(err.response);
});
},
fetchAccount() {
this.$axios.$get(`api/account`)
.then(res => {
this.$set(this.$data, "accounts", res.accounts);
console.log(res)
})
.catch(err => {
console.log(err.response);
});
},
fetchProduct() {
this.$axios
.$get(`/api/product`)
.then(res => {
this.$set(this.$data, "products", res.products.data);
console.log(res);
})
.catch(err => {
console.log(err);
});
},
fetchReturn() {
this.$axios
.$get(`api/return-sale/` + this.$route.params.id)
.then(res => {
this.$set(this.$data, "form", res.returnsale);
console.log(res);
for (let i in this.form.items) {
Vue.set(this.form.items[i], 'quantity', this.form.items[i].pivot.quantity);
Vue.set(this.form.items[i], 'unit_price', this.form.items[i].pivot.unit_price);
Vue.set(this.form.items[i], 'discount', this.form.items[i].pivot.discount);
}
})
.catch(err => {
console.log(res.response);
});
},
updateReturn() {
this.$axios
.$patch(`api/return-sale/` + this.form.id, {
location: this.form.location,
products: this.form.products,
supplier: this.form.supplier,
account: this.form.account,
return_des: this.form.return_des,
staff_des: this.form.staff_des,
})
.then(res => {
this.returnsale = res.data;
// this.$set(this.$data, "returnsale", res.data);
this.$set(this.$data, "returnsale", res.returnsale);
this.$router.push(`/return/return-sale/view`);
console.log(res);
})
.catch(err => {
console.log(err.response);
});
},
addTocart(item) {
if (this.form.items.includes(item)) {
alert("already there");
} else {
this.form.items.push(item);
}
Vue.set(item, 'quantity', 1);
Vue.set(item, 'discount', 1);
},
removeItem(index) {
this.form.items.splice(index, 1);
}
}
};
</script>
<style lang="scss">
.textarea {
border: 1px solid rgba(0, 0, 0, 0.125);
outline: 1px solid #461577;
}
.tableReturn {
width: 100%;
margin-top: 10px;
border-collapse: collapse;
&--header {
font-weight: 500;
text-align: left;
border-bottom: 1px solid rgba(0, 0, 0, 0.125);
}
&--td {
border-bottom: 1px solid rgba(0, 0, 0, 0.125);
}
}
.table-quantity {
border: 1px solid rgba(0, 0, 0, 0.125);
padding: 5px 10px 5px 10px;
margin-top: 5px;
margin-bottom: 5px;
}
</style>
enter image description here
Try this
i have added one check if (this.form.hasOwnProperty("items"))
if form have value then only it will excute
<template>
<v-app>
<v-card class="mx-5 my-5">
<div class="teal darken-1">
<v-card-title class="white--text">Edit​ Return Sale</v-card-title>
</div>
<v-divider></v-divider>
<div class="px-5">
<p
class="caption font-italic pt-5"
>The field labels marked with * are required input fields.</p>
<v-row>
<v-col md="6" cols="12">
<label for="reference_no" class="font-weight-bold">Reference No</label>
<v-text-field solo outlined dense v-model="form.reference_no"></v-text-field>
</v-col>
<v-col md="6" cols="12">
<label class="font-weight-bold">Location*</label>
<v-autocomplete
item-value="address"
item-text="address"
solo
outlined
dense
label="Business Location"
return-object
v-model="form.location"
:items="locations"
></v-autocomplete>
</v-col>
<v-col md="6" cols="12">
<label class="font-weight-bold">Supplier</label>
<v-autocomplete
:items="suppliers"
item-text="name"
item-value="name"
solo
outlined
dense
return-object
v-model="form.supplier"
label="Select Supplier"
></v-autocomplete>
</v-col>
<v-col md="6" cols="12">
<label class="font-weight-bold">Account</label>
<v-autocomplete
:items="accounts"
item-text="name"
item-value="name"
solo
outlined
dense
return-object
v-model="form.account"
label="Select Account"
></v-autocomplete>
</v-col>
<v-col cols="12">
<label class="font-weight-bold">Select Product</label>
<div>
<v-autocomplete
dense
solo
item-text="name"
item-value="name"
return-object
:items="products"
#input="addTocart"
></v-autocomplete>
</div>
</v-col>
</v-row>
<div>
<label class="font-weight-bold mb-3">Product Table</label>
<table class="tableReturn">
<thead>
<tr class="tableReturn--header">
<td>Name</td>
<td>Code</td>
<td>Quantity</td>
<td>Unit Price</td>
<td>Discount</td>
<td>Total</td>
<td>Actions</td>
</tr>
</thead>
<tbody>
<tr class="tableReturn--td" v-for="(item, index) in form.items" :key="index">
<td>{{item.name}}</td>
<td>{{item.code}}</td>
<td>
<input type="number" class="table-quantity" v-model="form.items[index].quantity" />
</td>
<td>
<input
type="number"
class="table-quantity"
v-model="form.items[index].unit_price"
placeholder="0.00"
/>
</td>
<td>
<input
type="number"
class="table-quantity"
v-model="form.items[index].discount"
placeholder="0.00"
/>
</td>
<td>USD {{ discountedPrice(item) | formatMoney }}</td>
<td>
<v-btn small color="red" outlined #click="removeItem(index)">
<v-icon>mdi-delete</v-icon>
</v-btn>
</td>
</tr>
<tr>
<td class="py-5" colspan="2">Total</td>
<td colspan="3">{{ Qty }}</td>
<td>USD {{ Total | formatMoney }}</td>
</tr>
</tbody>
</table>
</div>
<v-row>
<v-col md="6" cols="12">
<div class="d-flex flex-column mb-5">
<label for class="font-weight-bold">Return Note</label>
<textarea cols="30" rows="5" class="textarea" v-model="form.return_des"></textarea>
</div>
</v-col>
<v-col md="6" cols="12">
<div class="d-flex flex-column mb-5">
<label for class="font-weight-bold">Staff Note</label>
<textarea cols="30" rows="5" class="textarea" v-model="form.staff_des"></textarea>
</div>
</v-col>
</v-row>
</div>
<v-btn
#click.prevent="updateReturn"
class="blue mx-5 darken-2 mb-5 grey--text text--lighten-4"
>
<v-icon>mdi-check</v-icon>Update
</v-btn>
</v-card>
</v-app>
</template>
<script>
import Vue from "vue";
let numeral = require("numeral");
Vue.filter("formatMoney", function(value) {
return numeral(value).format("00,00.00");
});
export default {
name: "editReturn",
created() {
this.fetchReturn();
this.fetchLocation();
this.fetchSupplier();
this.fetchAccount();
this.fetchProduct();
},
data() {
return {
form: {
items: []
},
suppliers: [],
locations: [],
products: [],
returnsale: [],
accounts: []
};
},
computed: {
Qty() {
if (this.form.hasOwnProperty("items")) {
return this.form.items.reduce((total, item) => {
return total + Number(item.quantity);
}, 0);
}
},
Total() {
if (this.form.hasOwnProperty("items")) {
return this.form.items.reduce((total, item) => {
let subtotal =
(item.unit_price - (item.unit_price * item.discount) / 100) *
item.quantity;
return total + subtotal;
}, 0);
}
}
},
methods: {
discountedPrice(product) {
return (
(product.unit_price - (product.unit_price * product.discount) / 100) *
product.quantity
);
},
fetchLocation() {
this.$axios
.$get(`api/location`)
.then(res => {
// this.locations = res.locations.data;
this.$set(this.$data, "locations", res.locations.data);
console.log(res);
})
.catch(err => {
console.log(err.response);
});
},
fetchSupplier() {
this.$axios
.$get(`api/supplier`)
.then(res => {
this.$set(this.$data, "suppliers", res.suppliers.data);
console.log(res);
})
.catch(err => {
console.log(err.response);
});
},
fetchAccount() {
this.$axios
.$get(`api/account`)
.then(res => {
this.$set(this.$data, "accounts", res.accounts);
console.log(res);
})
.catch(err => {
console.log(err.response);
});
},
fetchProduct() {
this.$axios
.$get(`/api/product`)
.then(res => {
this.$set(this.$data, "products", res.products.data);
console.log(res);
})
.catch(err => {
console.log(err);
});
},
fetchReturn() {
this.$axios
.$get(`api/return-sale/` + this.$route.params.id)
.then(res => {
this.$set(this.$data, "form", res.returnsale);
console.log(res);
for (let i in this.form.items) {
Vue.set(
this.form.items[i],
"quantity",
this.form.items[i].pivot.quantity
);
Vue.set(
this.form.items[i],
"unit_price",
this.form.items[i].pivot.unit_price
);
Vue.set(
this.form.items[i],
"discount",
this.form.items[i].pivot.discount
);
}
})
.catch(err => {
console.log(res.response);
});
},
updateReturn() {
this.$axios
.$patch(`api/return-sale/` + this.form.id, {
location: this.form.location,
products: this.form.products,
supplier: this.form.supplier,
account: this.form.account,
return_des: this.form.return_des,
staff_des: this.form.staff_des
})
.then(res => {
this.returnsale = res.data;
// this.$set(this.$data, "returnsale", res.data);
this.$set(this.$data, "returnsale", res.returnsale);
this.$router.push(`/return/return-sale/view`);
console.log(res);
})
.catch(err => {
console.log(err.response);
});
},
addTocart(item) {
if (this.form.items.includes(item)) {
alert("already there");
} else {
this.form.items.push(item);
}
Vue.set(item, "quantity", 1);
Vue.set(item, "discount", 1);
},
removeItem(index) {
this.form.items.splice(index, 1);
}
}
};
</script>
<style lang="scss">
.textarea {
border: 1px solid rgba(0, 0, 0, 0.125);
outline: 1px solid #461577;
}
.tableReturn {
width: 100%;
margin-top: 10px;
border-collapse: collapse;
&--header {
font-weight: 500;
text-align: left;
border-bottom: 1px solid rgba(0, 0, 0, 0.125);
}
&--td {
border-bottom: 1px solid rgba(0, 0, 0, 0.125);
}
}
.table-quantity {
border: 1px solid rgba(0, 0, 0, 0.125);
padding: 5px 10px 5px 10px;
margin-top: 5px;
margin-bottom: 5px;
}
</style>

How to upload my image file with my token, Vue JS

I'm trying to upload my image so that it goes to my API, the thing is the validation needs to stay on the page, after they have logged in. It works with the upload through postman, by when i try to do it in the frontend of my Vue code it doesn't.
If someone knows how I can change this code to make it work, please let me know.
<template>
<v-app id="inspire">
<v-content>
<v-layout align-center justify-center>
<v-flex xs12 sm8 md4>
<v-card class="elevation-12">
<v-toolbar dark color="green">
<v-toolbar-title>Upload Images</v-toolbar-title>
<v-spacer></v-spacer>
<v-btn slot="activator" icon large href="https://codepen.io/johnjleider/pen/wyYVVj" target="_blank">
<v-icon large>mdi-codepen</v-icon>
</v-btn>
</v-toolbar>
<v-card-text>
<v-form class="upload" #keydown.enter="addImage" method="post">
<v-text-field v-model="uploadImage.title" prepend-icon="title" id="title" name="title" label="Image Title" type="text"></v-text-field>
<v-text-field v-model="uploadImage.description" id="description" prepend-icon="description" name="description" label="Description" type="text"></v-text-field>
<v-text-field v-model="uploadImage.album_path" id="ablum_path" prepend-icon="photo_album" name="album_path" label="Choose an album" type="search"></v-text-field>
<input #change="onFileSelected" type="file" name="image" id="image">
</v-form>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn v-on:click="addImage" type="submit" color="green white--text">Upload</v-btn>
</v-card-actions>
</v-card>
</v-flex>
</v-layout>
</v-content>
</v-app>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
feedback: null,
selectedFile: null,
uploadImage: [
{ title: '' },
{ description: '' },
{ album_path: '' },
{image: null}
]
}
},
methods:{
addImage() {
axios.post('/api/image/upload',
{
'title': this.uploadImage.title,
'description': this.uploadImage.description,
'album_path': this.uploadImage.album_path,
'image': this.selectedFile,
},
{ headers: { 'Authorization': 'Bearer ' + this.$store.state.token }
})
.then(response => {
console.log("headers: {'Authorization': 'Bearer '" + this.$store.state.token);
console.log(response);
})
}
}
}
</script>
<style>
.red-text {
color: red;
font-size: 20px;
text-align: center;
}
</style>
you can try adding content-type to your header for axios before sending post request.
"Content-Type": "multipart/form-data; boundary=----",
},

Resources