How to remove Id using put method - vue.js

Indexing.vue;
<template>
<div class="row">
<div class="col-md-12">
<card class="card-plain" card-body-classes="table-full-width" header-classes="row">
<template slot="header" :class="row">
<h3 class="card-title">Indexing</h3>
<base-button type="primary" size="sm" #click="addNewIndexing" class="ml-4">Add Indexing</base-button>
</template>
<div class="row indexing-cards-container pl-5">
<div class="col card " v-for="data in indexingList" :key="data.id">
<div class="row">
<p class="pl-4">{{data.indexing_name}}</p>
</div>
<div class="row">
<p class="pl-4">{{data.indexing_url}}</p>
</div>
<div class="row IndexingImage pb-2 ">
<img :src="data.indexing_image_url" class="rounded mx-auto d-block"/>
</div>
<div class="row">
<div class="col pl-4">
<base-switch
class="mt-2"
v-model="data.is_active"
type="primary"
on-text="Active"
off-text="Inactive"
></base-switch>
</div>
<div class="col">
<base-button type="primary" #click="deleteIndexing(data.id)">Delete</base button>
</div>
</div>
</div>
</div>
</card>
<!-- </div>
</div>
</card> -->
</div>
</div>
</template>
<script>
import { Table, TableColumn } from 'element-ui';
import { BaseSwitch } from 'src/components/index';
export default {
components: {
[Table.name]: Table,
[TableColumn.name]: TableColumn,
BaseSwitch
},
data() {
return {
indexingList: [],
};
},
methods: {
addNewIndexing() {
this.$router.push({path: 'addindexing'})
},
deleteIndexing: function() {
this.api.putData('indexing/'+store.state.journalId, ).then((res)=> {
console.log(res.data)
})
},
getIndexingList: function() {
this.api.getDataModule('indexing/'+store.state.journalId, 'journals/v1/').then((res) => {
console.log(res.data)
this.indexingList = res.data.indexingList
}, (err) => {
console.log(err)
})
},
},
mounted: function() {
this.getIndexingList()
}
};
api.js;
putData (action, data) {
let url = `${BASE_URL}`
url += action + '.json'
return instance.put(url, data)
},
Issue :
We have separately created one component called (AddIndexing.vue) to get user details like indexing_name, indexing_url and indexing_image_url. And we wrote post method to post those user datas to the server. That is working fine. In this component (Indexing.vue) I'm showing those received datas from the server, that is also working fine.
I want to implement the delete function in the deleteIndexing method. We wrote the complete API code in api.js file. But I have mentioned only putData function because my boss told me to implement the delete option by putData api function. So now for action parameter in the putData function I passed "indexing/'+store.state.journalId" as an argument through deleteIndexing function and for data parameter in the putData function
I don't know what to pass as an argument. My boss told me to create a temporary object in the name of indexing_id and he asked me to pass as an second argument because indexing_id is the name of the id for each index in the database. But I don't know how to implement exactly. If I click delete option then that button should delete one index data based on the ID.
This is what I need to do. I can post the data and I can get the data but I don't know how to delete. After filling all the fields user entered the submit option means that data will send to the server, and it will create a new index module to have that data for single customer. If we are filling two customer details it means it will create two indexes separately and those two customer details will show in the "indexing.vue" component.
So now by using delete option I want to delete any user details by the ID.' API code everything was working fine but I don't know how to implement. The I do have one toggle button for active state and inactive state for that also I have to write a code by using putData api function.
How to implement this so I can do based on that.
I want to know how to do active and inactive toggle button function also. I tried some ways but that doesn't work for me. I'm quite beginner here so that I'm struggling a lot to do these API's.

Related

Vuejs shows the same data on multiple components

I have the following component chain in my project (templates are in different vue files, here ... acts as a separator to make it readable):
<!-- Sensors template -->
<div class="container">
<div class= "sensors_item">
<SensorItem v-for="i in sensors" :sensordata="i" :key="sensors.id"></SensorItem>
</div>
</div>
...
<!-- SensorItem template -->
<div>
<SensorParameterItem v-for="i in sensordata.sensorparams" :parameterdata="i" :key="sensordata.sensorparams.id"></SensorParameterItem>
</div>
...
<!-- SensorParameterItem template -->
<div class="col parameter-icon-clickable" v-on:click="openChart">
<i class="fas fa-chart-line" color="white"></i>
</div>
</div>
<ChartCollapsible class="parameter-icon-clickable" :isOpen="isChartOpen" :pdata="parameterdata"/>
</div
...
<!-- ChartCollapsible template-->
<div>
<transition appear name="modal">
<div v-if="isOpen">
<div class="chart-container">
<apexcharts height="400" width="100%" ref="chart" type="area" :options="o1" :series="setSeries"></apexcharts>
<!-- OK -->
</div>
</div>
</transition>
</div>
...
Functions
...
openChart: function() {
let data = {count: 144};
console.log('openChart');
this.$store.dispatch('getsensordata', data)
this.isChartOpen = !this.isChartOpen;
}
...
computed: {
setSeries() {
console.log("Computed.")
if(this.$store.getters.authStatus == "received") {
this.s1 = _.cloneDeep(this.$store.getters.getData);
} else {
this.s1 = _.cloneDeep([{data: [{x:0,y:0}]}]);
}
return this.s1;
}
}
I'm calling the backend by clicking in the SensorParameterItem (openChart fn) to receive chart data. Then, in ChartCollapsible I have computed which verifies that new data is received. After that it deep copies the new data into a property and returns that property to the chart component. I'd expect that each ChartCollapsible component would have its own chart data but it's not: I have 10 ChartCollapsibles rendered and all is updated with the same data, when I click any openchart button.
Any help would be great how to solve this issue!
Okay I've found what caused this behaviour: there is a computed property in the component, which monitors this.$store.getters.authStatus == "received" state. Since ALL component instances monitor this single state, when it changes, ALL component instances will be updated.
A possible fix is to send some component specific data (title, id, etc.) along this.$store.dispatch('getsensordata', data) request which will be replied back to all component instances, but only one component will be updated:
computed: {
setSeries() {
if(this.$store.getters.authStatus == "received") {
// Filter down to our chart otherwise all charts will be updated!
if(this.header.title == this.$store.getters.getData.header.title && this.parameterdata.pidx == this.$store.getters.getData.header.pidx) {
this.s1 = _.cloneDeep(this.$store.getters.getData.data);
}
}
return this.s1;
}
}

How to pass id to modal window component

Im using Vue and Ionic, I dont know how to pass my lesson.id to the method openModal()
explanation: I have a card with my data - lesson data, where are also comments, when user clicks on them, modal window is open, I need to pass id of the lesson to my modal window as props, so I can display comments for the lesson.
<ion-content>
<div
class="lesson-card"
v-for="lesson in lesson.video_lessons"
:key="lesson"
>
<div class="lesson-content">
<h2>{{ lesson.content_description }}</h2>
<div class="tags">
<span v-for="tag in lesson.tags" :key="tag">
#{{ tag }}
</span>
</div>
<img
v-if="lesson.content_thumbnail"
:src="`${lesson.content_thumbnail}`"
alt="theme-img"
height="600"
/>
</div>
<div class="sidebar-icons">
bookmark
heart
<p>{{ lesson.likes }}</p>
<a #click="openModal">comments</a>
<p>lesson id: {{ lesson.id }}</p>
</div>
</div>
</ion-content>
this is my method
async openModal() {
const modal = await modalController.create({
component: CommentsModal,
componentProps: { id: 1 }, // i need to replace this 1 with id of the lesson
})
return modal.present()
},
In template, pass it like
<a #click="openModal(lession.id)">comments</a>
and in method
async openModal(payload) { // change added
const modal = await modalController.create({
component: CommentsModal,
componentProps: { id: payload}, // Change added
})
return modal.present()
},

Which is the best approach to write a reusable component / customElement in aurelia?

I need to create multiple tables of same html structure but data and columns are different for each table. So I want to create one table component which has common html structure and can get columns and data as parameter , I'm not sure which is the best approach in aurelia to do the same.
app.html
<div class="wrapper">
<div class="main-container">
<header>HEADING</header>
<compose view-model="./table-component" model.bind="items" inherit-binding-context></compose>
</div>
</div>
another-table.html
<div class="wrapper">
<div class="main-container">
<header>HEADING 2</header>
<compose view-model="./table-component" model.bind="items1" inherit-binding-context></compose>
</div>
</div>
table-component.js
export class TableComponent {
constructor() {
this.message = 'Hello world';
}
activate(model) {
this.items = model;
}
getMoreFn(){
this.parent.getMore(); // calling respective component api function
}
bind(bindingContext, overrideContext) {
this.parent = overrideContext.parentOverrideContext.bindingContext;
}
}
table-component.html
<template>
<header id="level-one-head" class="level-one-header">
<div></div>
<div>No.</div>
<div>Name</div>
<div>Type</div>
</header>
<div class="level-data-section ">
<div
repeat.for="item of items"
infinite-scroll-next="getMoreFn"
>
<div class="row">
<div>${$index}</div>
<div>${item}</div>
</div>
</div>
</div>
</template>
Using compose like above and passing the data is right approach or is there any better way to handle dynamic data ?
Planning to pass the column headers also in compose depending on the table

DOM does not show updated data of array in vue.js & laravel while axios call

after several attempts to find a solution here I decided to post it.
I am a beginner and using laravel + vue.js + mysql
Approach
I fetch data via an Axios call and update the data within the vue instance. Then I would like to show this data and update the DOM. I use mounted() to get the data and update the array "users".
Problem
I am able to get the data, update the array (when console logging), however I am not able to show it in the DOM. It wont update. What am I missing?
My vue component:
<template>
<div class="container">
<form class="form" v-on:submit.prevent="submituser">
<div class="row">
<label id="username">Username</label>
<input type="text" v-model="username" name="username" />
</div>
<div class="row">
<label id="password">Password</label>
<input type="password" name="password" v-model="password" v-on:keyup="CheckPrint" />
</div>
<div class="row">
<button type="submit" class="mt-2 btn btn-primary">Sign up</button>
</div>
</form>
<h2>All Users</h2>
<p>{{users}}</p>
<ul>
<li v-for="user in users" :key="user.name">{{ user.name }}</li>
</ul>
</div>
</template>
<script>
import axios from "axios";
export default {
data: function() {
return {
password: "",
username: "",
users: []
};
},
mounted() {
var vm = this;
axios.get("api/user").then(function(response) {
vm.users = response.data;
console.log(vm.users, "User updated");
});
}
};
</script>
My app.js component:
/**
* First we will load all of this project's JavaScript dependencies which
* includes Vue and other libraries. It is a great starting point when
* building robust, powerful web applications using Vue and Laravel.
*/
require("./bootstrap");
window.Vue = require("vue");
/**
* Next, we will create a fresh Vue application instance and attach it to
* the page. Then, you may begin adding components to this application
* or customize the JavaScript scaffolding to fit your unique needs.
*/
import Vue from "vue";
import Vuex from "vuex";
import store from "../store/store";
Vue.use(Vuex);
Vue.component("Signup", require("./components/User/Signup.vue"));
const app = new Vue({
el: "#app",
store
});
Thanks a lot for your help. I am sure its an easy one for most of you!
If you're replacing the entire Array with another entire Array, it should work properly.
But analysing your code, I've figured that, maybe, you don't need to use that var vm = this in this case...
I'd try to use arrow function in that response function and try using only this.users = response.data to see if it works.
i'd like to put this in a comment but i'm a new user too.

Update component data from the template

Not too sure what is wrong here, it seems fine to me! I'm simply trying to update the data property display to true when I click the input within my component.
I have passed the data to the slot scope, so can't see that being the issue. It just simply won't update, using a function to toggle it works, however not what I really want to do, seems pointless.
<time-select>
<div slot-scope="{ time }" class="is-inline-block">
<label for="businessHoursTimeFrom"><i class="fas fa-clock"></i> From</label>
<input type="text" name="businessHoursTimeFrom[]" v-model="time" v-on:click="display = true">
</div>
</time-select>
The code behind:
<template>
<div>
<p>{{ display }}</p>
<slot :time="time" :display="display"></slot>
<div class="picker" v-if="display">
<p>Test</p>
</div>
</div>
</template>
<script>
export default {
props: [],
data: function () {
return {
time: '',
display: false
}
},
mounted() {
},
methods: {
}
}
</script>