React.js Table Component - reactjs

I am creating a pizza app with React.js and would like to display pizza options in a table. I would like to develop my table using like this table, rather than the way i am doing it in choices.js.
Choices.js
return (
<div className="page-wrap">
<table>
<thead>
<tr>
<th>Pizza Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td key={index}>
<a href onClick={this.handleChoice.bind(this, pizza)}>
{pizza.name}</a></td>
</tr>
<tr>
<td>${pizza.price}</td>
</tr>
</tbody>
</table>
</div>
)
});
Options.js
var pizzas = [
{
name: 'Cheese Pizza',
cheese: 'Mozzarella',
toppings: [],
price: 5
},
{
name: 'Papas Special',
cheese: 'Parmesan',
toppings: ['Spinach', 'Lobster', 'Hot Oil'],
price: 50
},
{
name: 'Wild West',
cheese: 'Spicy Mozzarella',
toppings: ['Red Onions', 'Texas Chilli', 'Grilled Chicken'],
price: 25
},
{
name: 'California Pizza',
cheese: 'Mozzarella',
toppings: ['Spinach', 'Guacamole', 'Cherry Tomato'],
price: 25
},
{
name: 'Buffalo Chicken Pizza',
cheese: 'Spicy Blue Cheese',
toppings: ['Red Onions', 'Texas Chilli'],
price: 25
},
{
name: 'Jerk Chicken Pizza',
cheese: 'Mozzarella',
toppings: ['Red Onions', 'Jerk Sauce'],
price: 25
},
{
name: 'Salad Pizza',
cheese: 'Mozzarella',
toppings: ['Red Onions', 'Lettuce', 'Tomato'],
price: 25
}
];

Are you tring to do this? http://jsfiddle.net/dahdx6eu/337/
var cols = [
{ key: 'Name', label: 'Name' },
{ key: 'Cheese', label: 'Cheese' },
{ key: 'Toppings', label: 'Toppings' },
{ key: 'Price', label: 'Price' },
];
var data = [
{
id: 1,
name: 'Cheese Pizza',
cheese: 'Mozzarella',
toppings: [],
price: 5
},
{
id: 2,
name: 'Papas Special',
cheese: 'Parmesan',
toppings: ['Spinach', 'Lobster', 'Hot Oil'],
price: 50
},
{
id: 3,
name: 'Wild West',
cheese: 'Spicy Mozzarella',
toppings: ['Red Onions', 'Texas Chilli', 'Grilled Chicken'],
price: 25
},
{
id: 4,
name: 'California Pizza',
cheese: 'Mozzarella',
toppings: ['Spinach', 'Guacamole', 'Cherry Tomato'],
price: 25
},
{
id: 5,
name: 'Buffalo Chicken Pizza',
cheese: 'Spicy Blue Cheese',
toppings: ['Red Onions', 'Texas Chilli'],
price: 25
},
{
id: 6,
name: 'Jerk Chicken Pizza',
cheese: 'Mozzarella',
toppings: ['Red Onions', 'Jerk Sauce'],
price: 25
},
{
id: 7,
name: 'Salad Pizza',
cheese: 'Mozzarella',
toppings: ['Red Onions', 'Lettuce', 'Tomato'],
price: 25
}
]
var Table = React.createClass({
render: function() {
var headerComponents = this.generateHeaders(),
rowComponents = this.generateRows();
return (
<table>
<thead> {headerComponents} </thead>
<tbody> {rowComponents} </tbody>
</table>
);
},
generateHeaders: function() {
var cols = this.props.cols; // [{key, label}]
// generate our header (th) cell components
return cols.map(function(colData) {
return <th key={colData.key}> {colData.label} </th>;
});
},
generateRows: function() {
var cols = this.props.cols, // [{key, label}]
data = this.props.data;
return data.map(function(item) {
// handle the column data within each row
console.log(item)
return (<tr key={item.id}><td>{item.name}</td><td>{item.cheese}</td><td>{item.toppings} </td><td>{item.price}</td></tr>);
});
}
});

Related

Dynamic Row Span in react mantine tablle

import { Table } from "#mantine/core";
import React from "react";
interface TableProps {
columns: ColumnProps[];
data: {
[key: string]: string | number | null;
}[];
groupBy: string;
groupedColumn: string;
}
interface ColumnProps {
key: string;
label: string;
actions?: React.ReactNode;
}
type modifiedType = { [key: string]: string | number | (string | number)[] | null };
export const RowGrouping = ({ columns, data, groupBy, groupedColumn }: TableProps) => {
const transformData = data.reduce<modifiedType[]>((modifiedObj: modifiedType[], dataObj) => {
const similar: modifiedType | undefined = modifiedObj.find((e) => e[groupBy] == dataObj[groupBy]);
const groupedKey = Object.keys(dataObj).find((key) => key.includes(groupedColumn)) ?? "";
return (
similar
? (similar[groupedKey] as (string | number | null)[]).push(dataObj[groupedKey])
: modifiedObj.push({
...dataObj,
[groupedKey]: [dataObj[groupedKey]] as (string | number)[],
}),
modifiedObj
);
}, []);
const tableHead = () => {
return (
<tr>
{columns.map((c, index) => (
<th key={index}>{c.label}</th>
))}
</tr>
);
};
const tableData = () => {
return (
<>
{transformData.map((c, index) => (
<>
<tr key={index}>
<td rowSpan={(c[groupedColumn] as string).length + 1}>{c[groupBy]}</td>
</tr>
{(c[groupedColumn] as (string | number | null)[]).map((a: string | number | null) => (
<tr key={a}>{a?.toString().trim() == "" || a?.toString().trim() == null ? <td> </td> : <td>{a}</td>}</tr>
))}
</>
))}
</>
);
};
return (
<div>
<Table withBorder withColumnBorders>
<thead>{tableHead()}</thead>
<tbody>{tableData()}</tbody>
</Table>
</div>
);
};
Following is the Input data to the above code
columns = [
{
key: "state_name",
label: "State",
},
{
key: "district",
label: "District",
},
];
data = [
{
state_name: "Karnataka",
district: "Banglore",
gender: "Boys",
},
{
state_name: "Karnataka",
district: "Banglore",
gender: "Girls",
},
{
state_name: "Telangana",
district: null,
gender: "Boys",
},
{
state_name: "Telangana",
district: "Hyderabad",
gender: "Boys",
},
{
state_name: "Karnataka",
district: "Mysore",
gender: "Girls",
},
{
state_name: "Karnataka",
district: " ",
gender: "Boys",
},
{
state_name: "Karnataka",
district: "Manglore",
gender: "Transgender",
},
{
state_name: "Goa",
district: "North",
gender: "Girls",
},
{
state_name: "Andhra Pradesh",
district: "Chittor",
gender: "Boys",
},
{
state_name: "Andhra Pradesh",
district: "Chittor",
gender: "Girls",
},
{
state_name: "Goa",
district: "South",
gender: "Transgender",
},
{
state_name: "Andhra Pradesh",
district: "Vizag",
gender: "Boys",
},
{
state_name: "kerala",
district: "pollachi",
gender: "Boys",
},
];
groupBy = "state_name";
groupedColumn = "district";
below is the output for this code
this code is perfectly okay when there are only two columns but i want to continue to group the remaining columns also here for example gender and again if columns are added it should group them as well... im unable to do it please help and i want to do it without hardcodes. so by removing grouped column and just by giving groupby it should group all columns without altering the props but i can transform the data
Taking the rowspan and colspan values ​​in the header will tire the system less. this way you won't need to do any grouping.
columns = [
{
key: "state_name",
label: "State",
colspan:2
},
{
key: "district",
label: "District",
colspan:3
},
];
Example : `const tableData = () => data?.map((item) => (
<tr>
{columns?.map(({key,colspan,...other}) => (
<td colSpan={colspan}>
item?.[key]
</td>
))}
</tr>
))`

merging the rows and displaying it in mantine table in react

import { Table } from "#mantine/core";
import React from "react";
interface TableProps {
columns: ColumnProps[];
data: {
[key: string]: string | number;
}[];
group: string[];
}
interface ColumnProps {
key: string;
label: string;
actions?: React.ReactNode;
}
export const RowGrouping = ({ columns, data, group }: TableProps) => {
columns = [
{
key: "state_name",
label: "State",
},
{
key: "district",
label: "District",
},
{
key: "gender",
label: "Gender",
},
];
data = [
{
state_name: "Karnataka",
district: "Banglore",
gender: "Boys",
},
{
state_name: "Karnataka",
district: "Banglore",
gender: "girls",
},
{
state_name: "Telangana",
district: "Hyderabad",
gender: "Boys",
},
{
state_name: "Karnataka",
district: "Mysore",
gender: "Girls",
},
{
state_name: "Karnataka",
district: "Mysore",
gender: "Boys",
},
{
state_name: "Karnataka",
district: "Manglore",
gender: "Transgender",
},
{
state_name: "Goa",
district: "North",
gender: "Girls",
},
{
state_name: "Andhra Pradesh",
district: "Chittor",
gender: "Boys",
},
{
state_name: "Andhra Pradesh",
district: "Chittor",
gender: "Girls",
},
{
state_name: "Goa",
district: "South",
gender: "Transgender",
},
{
state_name: "Andhra Pradesh",
district: "Vizag",
gender: "Boys",
},
];
group = ["state_name", "district"];
const mergeData = data.reduce((a, b) => {
const similar = a.find((e) => e.state_name == b.state_name);
return similar ? similar.district.push(b.district) : a.push({ ...b, district: [b.district] }), a;
}, []);
const tableHead = () => {
return (
<tr>
{columns.map((c, index) => (
<th key={index}>{c.label}</th>
))}
</tr>
);
};
const tableData = () => {
return (
<>
{mergeData.map((c, index) => (
<tr key={index}>
<td>{c.state_name}</td>
{c.district.map((d, i) => (
<tr key={i}>
<tr>
<td>{d}</td>
</tr>
</tr>
))}
</tr>
))}
</>
);
};
return (
<div>
<Table withBorder withColumnBorders style={{ width: "50%", margin: "0 auto", marginTop: "50px" }}>
<thead>{tableHead()}</thead>
<tbody>{tableData()}</tbody>
</Table>
</div>
);
};
Here in this example, what I'm trying to do is that I have three columns - state, district and gender, and I'm trying to group the rows and display it in table. but I'm able to group only one column that is state but I'm unable to group district wise, and I don't know how to generalize this without hardcode means data columns should be grouped for any data by column name and displayed in table.[ this is the output I'm getting but i want the districts to be displayed properly means inside karnataka rowspan there should be only banglore, mysore and manglore and inside banglore rowspan there should be boys and girls and same for mysore and chittor as well without hardcoded. please help...
You could adjust your merge fn so that it only pushes the district if it's not already in the district array.
const mergeData = data.reduce((a, b) => {
const similar = a.find((e) => e.state_name == b.state_name);
return (
similar
? !similar.district.includes(b.district) &&
similar.district.push(b.district)
: a.push({
...b,
district: [b.district],
}),
a
);
}, []);
Output :
[
{
district: ["Banglore", "Mysore", "Manglore"],
gender: "Boys",
state_name: "Karnataka",
},
{
district: ["Hyderabad"],
gender: "Boys",
state_name: "Telangana",
},
{
district: ["North", "South"],
gender: "Girls",
state_name: "Goa",
},
{
district: ["Chittor", "Vizag"],
gender: "Boys",
state_name: "Andhra Pradesh",
},
];
Fiddle : https://jsfiddle.net/RyanZee/bnh6u7mx/4/
As for the gender being displayed, you're not returning it in your map:
{
mergeData.map((c, index) => (
<tr key={index}>
<td>{c.state_name}</td>
{c.district.map((d, i) => (
<tr key={i}>
<tr>
<td>{d}</td>
</tr>
</tr>
))}
<td>{c.gender}</td>
</tr>
));
}

How to set local JSON data into functional components in React?

I'm new to react. I got stucked here. I'm not sure how to pass json data that is getting returned as function to useState.I used classes and everything worked perfectly fine. Now i'm trying to convert that code into functional components. When I delete an item it displays an error. movie.filter is not a function.
index.js
import React, { Component,useState } from 'react'
import {getMovies} from "../services/fakeMovieService"
function Movies() {
const movies = getMovies()
const [movie, setMovie] = useState(movies);
const handleDelete = (movie) => {
const newM= movie.filter(m => m._id != movie._id)
setMovie({newM})
}
return (
<React.Fragment>
<table className="table">
<thead>
<tr>
<th>Title</th>
</tr>
</thead>
<tbody>
{movie.map(movie =>(
<tr key={movie._id}>
<td>{movie.title}</td>
<td>{movie.genre.name}</td>
<td>{movie.numberInStock}</td>
<td>{movie.dailyRentalRate}</td>
<td><button onClick={()=>handleDelete(movie)} className="btn btn-danger btn-sm">Delete</button></td>
</tr>
))
}
</tbody>
</table>
</React.Fragment>
);
}
export default Movies;
JSON
import * as genresAPI from "./fakeGenreService";
const movies = [
{
_id: "5b21ca3eeb7f6fbccd471815",
title: "Terminator",
genre: { _id: "5b21ca3eeb7f6fbccd471818", name: "Action" },
numberInStock: 6,
dailyRentalRate: 2.5,
publishDate: "2018-01-03T19:04:28.809Z"
},
{
_id: "5b21ca3eeb7f6fbccd471816",
title: "Die Hard",
genre: { _id: "5b21ca3eeb7f6fbccd471818", name: "Action" },
numberInStock: 5,
dailyRentalRate: 2.5
},
{
_id: "5b21ca3eeb7f6fbccd471817",
title: "Get Out",
genre: { _id: "5b21ca3eeb7f6fbccd471820", name: "Thriller" },
numberInStock: 8,
dailyRentalRate: 3.5
},
{
_id: "5b21ca3eeb7f6fbccd471819",
title: "Trip to Italy",
genre: { _id: "5b21ca3eeb7f6fbccd471814", name: "Comedy" },
numberInStock: 7,
dailyRentalRate: 3.5
},
{
_id: "5b21ca3eeb7f6fbccd47181a",
title: "Airplane",
genre: { _id: "5b21ca3eeb7f6fbccd471814", name: "Comedy" },
numberInStock: 7,
dailyRentalRate: 3.5
},
{
_id: "5b21ca3eeb7f6fbccd47181b",
title: "Wedding Crashers",
genre: { _id: "5b21ca3eeb7f6fbccd471814", name: "Comedy" },
numberInStock: 7,
dailyRentalRate: 3.5
},
{
_id: "5b21ca3eeb7f6fbccd47181e",
title: "Gone Girl",
genre: { _id: "5b21ca3eeb7f6fbccd471820", name: "Thriller" },
numberInStock: 7,
dailyRentalRate: 4.5
},
{
_id: "5b21ca3eeb7f6fbccd47181f",
title: "The Sixth Sense",
genre: { _id: "5b21ca3eeb7f6fbccd471820", name: "Thriller" },
numberInStock: 4,
dailyRentalRate: 3.5
},
{
_id: "5b21ca3eeb7f6fbccd471821",
title: "The Avengers",
genre: { _id: "5b21ca3eeb7f6fbccd471818", name: "Action" },
numberInStock: 7,
dailyRentalRate: 3.5
}
];
export function getMovies() {
return movies;
}
setMovie({newM})
should be
setMovie(newM)
because your state is an array. The argument movie and state movie have the same name so you're trying to use Array.prototype.filter on an object.
Rename the restructured array values of useState to movies and setMovies:
const initialMovies = getMovies()
const [movies, setMovies] = useState(initialMovies);
Use functional state update as the new state depends on the old state:
const handleDelete = (movie) => {
setMovies(previousMovies => previousMovies.filter(m => m._id !== movie._id))
}
and use movies to render
{movies.map(movie => (...

TypeError: Cannot destructure property 'title' of 'collections' as it is undefined

I am trying to get collections using selectors. Here is my code:
Selectors
import { createSelector } from "reselect";
export const selectShop = state => state.shop;
export const selectCollections = createSelector(
[selectShop],
shop => shop.collections
)
export const selectCollectionForPreview = createSelector(
[selectCollections],
collections => Object.keys(collections).map(key => collections[key])
)
export const selectCollectionItem = collectionUrl =>
createSelector(
[selectCollections],
collections => collections[collectionUrl]
//It seems that the selector above is returning collections as an array instead of object, so collections[collectionUrl] is returning undefined
)
Component
const CollectionPage = ({collections}) => {
// const {title, items} = Object.keys(collections)
const {title, items} = collections
console.log('collection', collections);
return(
// <></>
<div className="collection-page">
<h2 className='title'>{title}</h2>
<div className='items'>
{items.map( item =>
<CollectionItem key={item.id} item={item}/>
)}
</div>
</div>
)
}
const mapStateToProps = (state,ownProps) => ({
collections: selectCollectionItem(ownProps.match.params.collectionId)(state)
})
export default connect(mapStateToProps)(CollectionPage);
SHOP_DATA
const SHOP_DATA = {
hats:{
id: 1,
title: 'Hats',
routeName: 'hats',
items: [
{
id: 1,
name: 'Brown Brim',
imageUrl: 'https://i.ibb.co/ZYW3VTp/brown-brim.png',
price: 25
},
{
id: 2,
name: 'Blue Beanie',
imageUrl: 'https://i.ibb.co/ypkgK0X/blue-beanie.png',
price: 18
},
{
id: 3,
name: 'Brown Cowboy',
imageUrl: 'https://i.ibb.co/QdJwgmp/brown-cowboy.png',
price: 35
},
{
id: 4,
name: 'Grey Brim',
imageUrl: 'https://i.ibb.co/RjBLWxB/grey-brim.png',
price: 25
},
{
id: 5,
name: 'Green Beanie',
imageUrl: 'https://i.ibb.co/YTjW3vF/green-beanie.png',
price: 18
},
{
id: 6,
name: 'Palm Tree Cap',
imageUrl: 'https://i.ibb.co/rKBDvJX/palm-tree-cap.png',
price: 14
},
{
id: 7,
name: 'Red Beanie',
imageUrl: 'https://i.ibb.co/bLB646Z/red-beanie.png',
price: 18
},
{
id: 8,
name: 'Wolf Cap',
imageUrl: 'https://i.ibb.co/1f2nWMM/wolf-cap.png',
price: 14
},
{
id: 9,
name: 'Blue Snapback',
imageUrl: 'https://i.ibb.co/X2VJP2W/blue-snapback.png',
price: 16
}
]
},
sneakers:{
id: 2,
title: 'Sneakers',
routeName: 'sneakers',
items: [
{
id: 10,
name: 'Adidas NMD',
imageUrl: 'https://i.ibb.co/0s3pdnc/adidas-nmd.png',
price: 220
},
{
id: 11,
name: 'Adidas Yeezy',
imageUrl: 'https://i.ibb.co/dJbG1cT/yeezy.png',
price: 280
},
{
id: 12,
name: 'Black Converse',
imageUrl: 'https://i.ibb.co/bPmVXyP/black-converse.png',
price: 110
},
{
id: 13,
name: 'Nike White AirForce',
imageUrl: 'https://i.ibb.co/1RcFPk0/white-nike-high-tops.png',
price: 160
},
{
id: 14,
name: 'Nike Red High Tops',
imageUrl: 'https://i.ibb.co/QcvzydB/nikes-red.png',
price: 160
},
{
id: 15,
name: 'Nike Brown High Tops',
imageUrl: 'https://i.ibb.co/fMTV342/nike-brown.png',
price: 160
},
{
id: 16,
name: 'Air Jordan Limited',
imageUrl: 'https://i.ibb.co/w4k6Ws9/nike-funky.png',
price: 190
},
{
id: 17,
name: 'Timberlands',
imageUrl: 'https://i.ibb.co/Mhh6wBg/timberlands.png',
price: 200
}
]
},
jackets:{
id: 3,
title: 'Jackets',
routeName: 'jackets',
items: [
{
id: 18,
name: 'Black Jean Shearling',
imageUrl: 'https://i.ibb.co/XzcwL5s/black-shearling.png',
price: 125
},
{
id: 19,
name: 'Blue Jean Jacket',
imageUrl: 'https://i.ibb.co/mJS6vz0/blue-jean-jacket.png',
price: 90
},
{
id: 20,
name: 'Grey Jean Jacket',
imageUrl: 'https://i.ibb.co/N71k1ML/grey-jean-jacket.png',
price: 90
},
{
id: 21,
name: 'Brown Shearling',
imageUrl: 'https://i.ibb.co/s96FpdP/brown-shearling.png',
price: 165
},
{
id: 22,
name: 'Tan Trench',
imageUrl: 'https://i.ibb.co/M6hHc3F/brown-trench.png',
price: 185
}
]
},
womens:{
id: 4,
title: 'Womens',
routeName: 'womens',
items: [
{
id: 23,
name: 'Blue Tanktop',
imageUrl: 'https://i.ibb.co/7CQVJNm/blue-tank.png',
price: 25
},
{
id: 24,
name: 'Floral Blouse',
imageUrl: 'https://i.ibb.co/4W2DGKm/floral-blouse.png',
price: 20
},
{
id: 25,
name: 'Floral Dress',
imageUrl: 'https://i.ibb.co/KV18Ysr/floral-skirt.png',
price: 80
},
{
id: 26,
name: 'Red Dots Dress',
imageUrl: 'https://i.ibb.co/N3BN1bh/red-polka-dot-dress.png',
price: 80
},
{
id: 27,
name: 'Striped Sweater',
imageUrl: 'https://i.ibb.co/KmSkMbH/striped-sweater.png',
price: 45
},
{
id: 28,
name: 'Yellow Track Suit',
imageUrl: 'https://i.ibb.co/v1cvwNf/yellow-track-suit.png',
price: 135
},
{
id: 29,
name: 'White Blouse',
imageUrl: 'https://i.ibb.co/qBcrsJg/white-vest.png',
price: 20
}
]
},
mens:{
id: 5,
title: 'Mens',
routeName: 'mens',
items: [
{
id: 30,
name: 'Camo Down Vest',
imageUrl: 'https://i.ibb.co/xJS0T3Y/camo-vest.png',
price: 325
},
{
id: 31,
name: 'Floral T-shirt',
imageUrl: 'https://i.ibb.co/qMQ75QZ/floral-shirt.png',
price: 20
},
{
id: 32,
name: 'Black & White Longsleeve',
imageUrl: 'https://i.ibb.co/55z32tw/long-sleeve.png',
price: 25
},
{
id: 33,
name: 'Pink T-shirt',
imageUrl: 'https://i.ibb.co/RvwnBL8/pink-shirt.png',
price: 25
},
{
id: 34,
name: 'Jean Long Sleeve',
imageUrl: 'https://i.ibb.co/VpW4x5t/roll-up-jean-shirt.png',
price: 40
},
{
id: 35,
name: 'Burgundy T-shirt',
imageUrl: 'https://i.ibb.co/mh3VM1f/polka-dot-shirt.png',
price: 25
}
]
}
};
export default SHOP_DATA;
Please provide any pointers or advice on what am I doing wrong? I keep getting the error. It seems that the collection is undefined. I don't understand why. Ideally it should return an object.
You are exporting the component like this
const mapStateToProps = (state,ownProps) => ({
collections: selectCollectionItem(ownProps.match.params.collectionId)(state)
})
export default connect(mapStateToProps)(CollectionPage);
The ownProps.match.params.collectionId might be undefined.
Add this at the top of your file
import { withRouter} from 'react-router-dom'
and export your component like
export default connect(mapStateToProps)(withRouter(CollectionPage));

Vue.js create selector that shows Data from selected Dataset

Hello Everybody I've tried to create a select input field where i fill the options with a label from a dataset i created, which looks like this:
visitsList: [
{
label: '2017',
values: [
{ id: 1, title: "January", value: 20000 },
{ id: 2, title: "February", value: 30000 },
{id: 3,title: "March", value: 40000},
{ id: 4, title: "April", value: 40000},
{id: 5,title: "May",value: 50000},
{ id: 6,title: "June",value: 60000},
{id: 7, title: "July",value: 20000},
{ id: 8,title: "August", value: 70000},
{ id: 9,title: "September",value: 70000},
{id: 10, title: "October",value: 80000},
{id: 11,title: "November",value: 90000},
{id: 12,title: "December",value: 100000}
]},
{
label: '2018',
values: [
{ id: 1, title: "January", value: 20000 },
{ id: 2, title: "February", value: 30000 },
{id: 3,title: "March", value: 40000},
{ id: 4, title: "April", value: 40000},
{id: 5,title: "May",value: 50000},
{ id: 6,title: "June",value: 60000},
{id: 7, title: "July",value: 20000},
{ id: 8,title: "August", value: 70000},
{ id: 9,title: "September",value: 70000},
{id: 10, title: "October",value: 80000},
{id: 11,title: "November",value: 90000},
{id: 12,title: "December",value: 100000}
]},
{
label: '2019',
values: [
{ id: 1, title: "January", value: 20000 },
{ id: 2, title: "February", value: 30000 },
{id: 3,title: "March", value: 40000},
{ id: 4, title: "April", value: 40000},
{id: 5,title: "May",value: 50000},
{ id: 6,title: "June",value: 60000},
{id: 7, title: "July",value: 20000},
{ id: 8,title: "August", value: 70000},
{ id: 9,title: "September",value: 70000},
{id: 10, title: "October",value: 80000},
{id: 11,title: "November",value: 90000},
{id: 12,title: "December",value: 100000}
]
}
],
selectedYear: [],
The goal is if I select an option with the year it should show the values.
The template looks like this
<select v-model="widget.selectedYear">
<option v-for="year in widget.visitsList" v-bind:key="year.values">
{{year.label}}
</option>
</select>
<!--v-select :option="widget.visitsList.label" ></v-select-->
<table class="table table-bordered table-striped mb-0">
<thead>
<tr>
<th scope="col">Month</th>
<th scope="col">Views</th>
</tr>
</thead>
<tbody>
<tr v-for="year in widget.visitsList" v-bind:key="year.label" ><!--v-if="year.label == selectedYear"-->
<th scope="row">{{visit.title}}</th>
<td>{{visit.value}}</td>
</div>
</tr>
</tbody>
</table>
I tried so many things, but somehow i didn't find the right solution. Maybe there is no solution.
Thanks for the help
Regards Maxim
I think you should use computed properties for this, bind your first selector to a data property then you can have a computed property watch that data property and return the values for the second select based on the changes in the data property.

Resources