vuejs Editing conditions of data from - vue.js

I'm making a table listing with vuejs. My data from the database is as above. I'm transferring the data that comes with axios to "data.userList". What I want to do is; if "transaction_type" I want to edit. If I try to convert the data from the table, it will be too long. what should i do for him?
Examle:
I want to make the conditions I have written more regular. Considering that there are thousands of "transaction_types" like this, it's a lot of work. how can i edit this?
"transaction_type" I want to rename it according to the type of incoming data.
const app = new Vue({
el: '#app',
data: {
userList: [{
id: 1,
name: "Prem",
age: 18,
status: "close",
gender: "male",
transaction_type: 'initial_account_balance'
},
{
id: 2,
name: "Chandu",
status: "close",
age: 20,
gender: "female",
transaction_type: 'contact_debit'
},
{
id: 3,
name: "Jong",
status: "open",
age: 21,
gender: "female",
transaction_type: 'contact_credit'
},
{
id: 4,
name: "Steew",
status: "open",
age: 33,
gender: "male",
transaction_type: 'account_debit'
},
{
id: 5,
name: "Abdollah",
status: "open",
age: 26,
gender: "female",
transaction_type: 'account_credit'
},
{
id: 6,
name: "Jerry",
status: "open",
age: 35,
gender: "male",
transaction_type: 'account_debit'
}
]
}
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.js"></script>
<script src="https://unpkg.com/vue-router#2.0.0/dist/vue-router.js"></script>
<div id="app">
<table class="table table-hover table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Status</th>
<th>Type</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<tr v-for="(user, i) in userList" class="position-relative">
<td>{{ user.name }}</td>
<td>{{ user.age }}</td>
<td>{{ user.status }}</td>
<td>
<p v-if="user.transaction_type ==='initial_account_balance'">Opening Balance</p>
<p v-if="user.transaction_type ==='contact_debit'">Customer Advance</p>
<p v-if="user.transaction_type ==='contact_credit'">Customer</p>
<p v-if="user.transaction_type ==='account_debit'">Customer sales</p>
<p v-if="user.transaction_type ==='account_credit'">Customer Buy</p>
</td>
<td>
{{ user.gender }}
<!-- This can be placed in any of the <td>'s -->
<a class="stretched-link" :href="`#${user.id}`"></a>
</td>
</tr>
</tbody>
</table>
</div>

One idea is to give the user a select based on transaction types found in the data. filter the large data set with the user's selection...
const app = new Vue({
el: '#app',
data: {
xactionType: '',
userList: [{
id: 1,
name: "Prem",
age: 18,
status: "close",
gender: "male",
transaction_type: 'initial_account_balance'
},
{
id: 2,
name: "Chandu",
status: "close",
age: 20,
gender: "female",
transaction_type: 'contact_debit'
},
{
id: 3,
name: "Jong",
status: "open",
age: 21,
gender: "female",
transaction_type: 'contact_credit'
},
{
id: 4,
name: "Steew",
status: "open",
age: 33,
gender: "male",
transaction_type: 'account_debit'
},
{
id: 5,
name: "Abdollah",
status: "open",
age: 26,
gender: "female",
transaction_type: 'account_credit'
},
{
id: 6,
name: "Jerry",
status: "open",
age: 35,
gender: "male",
transaction_type: 'account_debit'
}
]
},
computed: {
xactionTypes() {
return this.userList.map(e => e.transaction_type);
},
filteredData() {
return this.xactionType ? this.userList.filter(e => e.transaction_type === this.xactionType) : this.userList;
}
},
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.2/vue.js"></script>
<script src="https://unpkg.com/vue-router#2.0.0/dist/vue-router.js"></script>
<div id="app">
<div>
<label for="xactionType">Select transaction type:</label>
<select id="xactionType" v-model="xactionType">
<option v-for="(type, i) in xactionTypes" :key="i" :value="type">{{type}}</option>
</select>
</div>
<table class="table table-hover table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Status</th>
<th>Type</th>
<th>Gender</th>
</tr>
</thead>
<tbody>
<tr v-for="(user, i) in filteredData" class="position-relative">
<td>{{ user.name }}</td>
<td>{{ user.age }}</td>
<td>{{ user.status }}</td>
<td>
<p v-if="user.transaction_type ==='initial_account_balance'">Opening Balance</p>
<p v-if="user.transaction_type ==='contact_debit'">Customer Advance</p>
<p v-if="user.transaction_type ==='contact_credit'">Customer</p>
<p v-if="user.transaction_type ==='account_debit'">Customer sales</p>
<p v-if="user.transaction_type ==='account_credit'">Customer Buy</p>
</td>
<td>
{{ user.gender }}
<!-- This can be placed in any of the <td>'s -->
<a class="stretched-link" :href="`#${user.id}`"></a>
</td>
</tr>
</tbody>
</table>
</div>

Related

Vue.js filter table

I'm new in vue.js, I would like to filter the following table by "Director ID".
When I load the page I would like to show only rows where director ID is 18. I need the table to filter right away without any user input. I would like to "hardcode" the ID 18 in to the function.
Thank you for your help!
<template>
<div>
<div class="row">
<div class="col"><h1 class="mt-3">List of all Movies</h1></div>
</div>
<table class="table">
<thead>
<tr>
<th>Movie ID</th>
<th>Title</th>
<th>Release Year</th>
<th>Director ID</th>
<th></th>
</tr>
</thead>
<tbody>
<tr v-for="movie in movies" :key="movie.id">
<td>{{ movie.id }}</td>
<td>{{ movie.title }}</td>
<td>{{ movie.releaseYear }}</td>
<td>{{ movie.director.id }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
data: function () {
return {
movies: [
{
id: 1,
title: "They All Lie",
releaseYear: 1989,
director: {
id: 18,
firstName: "Darci",
lastName: "Overill",
nationality: "China",
birthdate: "07/13/1973",
},
},
{
id: 2,
title: "Star Wars",
releaseYear: 1997,
director: {
id: 18,
firstName: "Darci",
lastName: "Overill",
nationality: "China",
birthdate: "07/13/1973",
},
},
{
id: 3,
title: "Mamma mia",
releaseYear: 2005,
director: {
id: 19,
firstName: "John",
lastName: "Smith",
nationality: "USA",
birthdate: "07/13/1980",
},
},
],
};
},
};
</script>
What if you change the tr to:
<tr v-for="movie in movies" :key="movie.id" v-if="movie.director.id === 18">
UPDATE
We wrote at the same time with #tuhin47.
I prefer his way to be honest with only a minor change in computed.
I changed the el to movie just to be more clear :)
computed: {
filteredMovies() {
return this.movies.filter((movie) => movie.director.id === 18);
},
},
You can use a computed property for filtering data and v-for the filtered data.
Here is the codesandbox
<tbody>
<tr v-for="movie in filteredMovies" :key="movie.id">
<td>{{ movie.id }}</td>
<td>{{ movie.title }}</td>
<td>{{ movie.releaseYear }}</td>
<td>{{ movie.director.id }}</td>
</tr>
</tbody>
Here is the computed property:
computed: {
filteredMovies() {
return this.movies.filter((el) => el.director.id === 18);
},
},

Search input in dynamic table

I'm new to Vue.js, so thanks for the help!!
I'm trying to implement a Search input for each column that filters the results for the user
this is my HTML file:
<div id="table" >
<div class=" container jumbotron mt-3 ">
<enter-data :resources="resources" ></enter-data>
</div>
<div class=" container jumbotron mt-1 ">
<table-list :resources="resources" :heads="heads" ></table-list>
</div>
</div>
And this is my JS file:
Vue.component('table-list', {
props: ['resources', 'heads'],
data () {
return {
selected: [],
selectAll: true,
userIds: [],
searchQuery: ''
}
},
computed: {
filteredResources: function () {
if (this.searchQuery) {
return this.resources.filter(item => {
// return this.item.value.toLowerCase().includes(this.searchQuery.toLowerCase());
// return this.searchQuery.toLowerCase().split(' ').every(v => item.value.toLowerCase().includes(v))
return this.item.FName.startsWith(this.searchQuery)
// return item.indexOf(this.searchQuery)>= 0;
})
} else {
return this.resources
}
}
},
methods: {
sortTable: function (resource) {
this.resources.sort(function (a, b) {
if (a[resource.value] > b[resource.value]) {
return 1
} else if (a[resource.value] < b[resource.value]) {
return -1
}
return 0
})
}
},
template: `
<div class="w-100 text-center d-flex justify-content-center flex-wrap">
<div class="w-75">
<!-- <input type="text" placeholder="search.." v-modle="searchQuery" >-->
<table class="table table-striped">
<thead>
<tr>
<th>
<input type="checkbox">
</th>
<th v-for="resource in heads" class="cursor" #click="sortTable(resource)">
<div />
{{ resource.name }}
</th>
</tr>
</thead>
<tbody>
<tr>
<td />
<td />
<td>
<input v-mode="searchQuery" type="text">
</td>
<td>
<input type="text">
</td>
</tr>
<tr v-for="resource in filteredResources">
<td>
<input type="checkbox">
</td>
<td v-for="column in heads">
{{ resource[column.value] }}
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
`
})
var app = new Vue({
el: '#table',
data: {
resources: [
{ 'id': 0, 'FName': 'Setareh', 'LName': 'Pashapour', 'Extra': 'Alishah' },
{ 'id': 1, 'FName': 'Soheil', 'LName': 'Pashapour' },
{ 'id': 2, 'FName': 'Simin', 'LName': 'Hajizadeh' },
{ 'id': 3, 'FName': 'Nouyan', 'LName': 'Arman' },
{ 'id': 4, 'FName': 'Abed', 'LName': 'Hamrang' },
{ 'id': 5, 'FName': 'Navid', 'LName': 'Ahanj' }
],
heads: [{ name: '#', value: 'id' }, { name: 'First Name', value: 'FName' }, { name: 'Last Name', value: 'LName' }]
}
})
Unfortunately it doesn't work
searchQuery data doesn't have any impact to my filter computed and nothing happend if i type any thing on it
my Any suggestions?
You're using the wrong directive name for form input binding. Change from v-mode to v-model for below line.
<input v-mode="searchQuery" type="text">

vue draggable problem when fix first column i can't move column

vue draggable problem when fix first column i can't move column how i can solve this problem
I have a table of 4 columns, one column has a static list of values ​​and the next column I need to be draggable so that I can move the cells in the second column etc without affecting the first column.
<script>
import draggable from 'vuedraggable'
export default {
name: "table-column-example",
components: {
draggable
},
data() {
return {
headers: ["id", "name", "sport"],
list: [
{ id: 1, name: "Abby", sport: "basket" },
{ id: 2, name: "Brooke", sport: "foot" },
{ id: 3, name: "Courtenay", sport: "volley" },
{ id: 4, name: "David", sport: "rugby" }
],
dragging: false
};
}
};
</script>
<style scoped>
.ghost {
opacity: 0.5;
background: #fb1b27;
}
</style>
<template>
<div class="row">
<div class="col-8">
<h3>Draggable table</h3>
<table class="table table-striped">
<thead class="thead-dark">
<draggable :list="headers" v-model="headers" :options="{draggable:'.item'}" tag="tr">
<th>#</th>
<th class="item" v-for="header in headers" :key="header" scope="col">
{{ header }}
</th>
</draggable>
</thead>
<tbody>
<tr v-for="item in list" :key="item.name">
<td>dddddd</td>
<td v-for="header in headers" :key="header">{{ item[header] }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>

set checkbox checked if value same vuejs

I'm displaying a list of menus in a table and looping with v-for, i want to set checkbox is checked if "id" from data menus same with "id_menu" from grupMenu, anyone has the same problem, please share it how i can solved, I'm realy new in Vue, thank's for helping,
new Vue({
el: "#app",
data: {
menus: [{
id: 1,
name_menu: "Setting",
parent: 0
},
{
id: 2,
name_menu: "Users",
parent: 1
},
{
id: 3,
name_menu: "Menu",
parent: 1
},
{
id: 4,
name_menu: "Role",
parent: 1
},
],
grupMenu: [{
id: 1,
id_user_group: 1,
id_menu: 1,
role: 1
},
{
id: 2,
id_user_group: 1,
id_menu: 2,
role: 0
},
]
},
methods: {
}
})
.text-center {
text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<h2>Todos:</h2>
<table border="1">
<thead>
<tr>
<th rowspan="2" style="vertical-align:middle;text-align:center">Menu</th>
<th colspan="3" style="text-align:center">Previlege</th>
</tr>
<tr class="">
<th class="text-center">Insert</th>
</tr>
</thead>
<tbody>
<tr v-for="menu in menus" :key="menu.id">
<td v-if="menu.parent == 0" style="color:blue">
<input type="checkbox">
<!-- <input type="checkbox" v-model="checked"> -->
<b class="ml-2">{{menu.name_menu}}</b>
</td>
<td v-else>
<input type="checkbox" class="ml-5"> -- {{menu.name_menu}}
</td>
<td class="text-center">
<input type="checkbox">
</td>
<!-- </div> -->
</tr>
</tbody>
</table>
</div>
here in fiddle
If I understand correctly, you want the checkbox checked for each entry in menu whose id appears as a menu_id in grupMenu.
So write a method like this:
isInGrupMenu(id) {
return this.grupMenu.some((item) => item.id_menu === id);
}
and bind it to the checked attribute of each checkbox:
<input type="checkbox" :checked="isInGrupMenu(menu.id)">

Dynamic v-model with v-for

I have a v-for loop that is going to spit out multiple rows of inputs that I would like to save each separate row to an array object dynamically.
v-for:
<table class="table m-0">
<tbody>
<tr v-for="fund in defaultFunds">
<td>
{{fund.name}}
<b-input v-model="newEntries[fund.id]['id']"
:value="fund.id"
name="entryFund"
type="text"
class="d-none"
:key="fund.id" />
</td>
<td>
<b-input v-model="newEntries[fund.id]['amount']"
name="newFundAmount"
id="newFundAmount"
type="text"
placeholder="Amount"
:key="fund.id"/>
</td>
</tr>
</tbody>
</table>
Desired array (using example of entering 2 rows):
newEntries: [
{ id: '1', amount: '50.00' },
{ id: '2', amount: '123.45' }
],
I load newEntries as an empty array by default. I don't know how to get the kind of array object I want with v-for. With the above code I end up with this:
newEntries: [null, '50.00', '123.45']
What am I doing wrong?
Do you want something like this:
https://jsfiddle.net/e6L5seec/
<div id="app">
newEntries: {{ newEntries }}
<table>
<tr v-for="(fund, index) in defaultFunds">
<td>{{ fund.name }}</td>
<td>
<input v-model="newEntries[index].id"
name="entryFund"
:value="fund.id"
type="text" />
</td>
<td>
<input v-model="newEntries[index].amount"
name="entryFund"
type="text" />
</td>
</tr>
</table>
</div>
new Vue({
el: "#app",
data: function() {
return {
defaultFunds: [
{
id: 0,
name: 'fund 0'
},
{
id: 1,
name: 'fund 1'
}
],
newEntries: [{}, {}]
}
},
methods: {
}
});