简体   繁体   English

需要使用一个组件中的复选框过滤数据,这里的数据来自另一组件中的axios

[英]Need to filter data using check boxes in one component, here data which is coming from axios in another component

I am able to get the data from axios in one component but failing to filter it in checkbox on click event. 我能够从axios的一个组件中获取数据,但是无法在click事件的复选框中对其进行过滤。 I am struggling with how to transfer axios data to another component, which is parent and which is child? 我在如何将axios数据传输到另一个组件(父级和子级)方面感到很挣扎。

I had created checkboxes in child component and data from axios in parent component trying to add event in child component checkbox to filter data according to colors in my parent data 我在子组件中创建了复选框,并在父组件中从axios创建了数据,试图在子组件中添加事件复选框以根据父数据中的颜色过滤数据

> // data to get from axios [      {  
>       "img_slug":"./products/product_1.jpg",
>       "color":"White",
>       "size":"S",
>       "styles":"Solid",
>       "Fit":"slim",
>       "sleeves":"full"    },    {  
>       
>       "img_slug":"./products/product_2.jpg",
>       "color":"Red",
>       "size":"L",
>       "styles":"Solid",
>       "Fit":"slim",
>       "sleeves":"full"    },    {  
>       
>       "img_slug":"./products/product_3.jpg",
>       "color":"Blue",
>       "size":"M",
>       "styles":"Solid",
>       "Fit":"slim",
>       "sleeves":"full"    } ]
> 
> // parent component
> 
> created () {
>     axios
>       .get('/products.json')
>         .then(response =>  
> { 
>             console.log(response.data);
>             this.data = response.data })
>         .catch(error => console.log(error))   },         methods: {
>     handleClick: function() {
>       this.$emit('clickedSomething')
>     }   },   props:{
>     item: {           type: Object,           required: false         }   }
> 
> // Child component export default {   name: 'parent',   components: {
>     Child   },   props:{
>     item: {           type: Object,           required: true      }   },   computed: {        selectedFilters: function() {           let filters = [];           let
> checkedFiters = this.color.filter(obj => obj.checked);
>           checkedFiters.forEach(element => {
>               filters.push(element.value);            });             return filters;         }   },   data(){
>       return {
>           
>       // list of tags to giving each stack a different color
>                   colors: [
>               {
>                   checked: false,
>                   value: 'Red'
>               },
>               {
>                   checked: false,
>                   value: 'Blue'
>               },
>               {
>                   checked: false,
>                   value: 'Black'
>               },
>               {
>                   checked: false,
>                   value: 'White'
>               }
>           
>       ]
>       };
>          },   methods: {
>     // handleClickInParent: function() {
>     //   // console.log('This is Parent Component');
>     // }
>     handleClickInParent: function() {
> 
>   this.filteredData = data;            
>        let filteredDataByfilters = [];            
>        let filteredDataBySearch = [];
>           // first check if filters where selected            if
> (this.selectedFilters.length > 0) {
>               filteredDataByfilters= this.filteredData.filter(obj => 
    this.selectedFilters.every(val => obj.color.indexOf(val) >= 0));
>               this.filteredData = filteredDataByfilters;          }                   }   } }

使用Vuelidate验证输入。

Need to add eventBus to instance and call at data change, and use that emit when we need use the changed data. 需要将eventBus添加到实例并在数据更改时调用,并在需要使用更改的数据时使用该方法发出。

For reference 以供参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要将数据从一个组件传递到另一个组件 - Need to pass data from one component to another component 在React.js中使用来自另一个组件的数据 - Using data from one component in another in Reactjs 从另一个组件返回到其组件时,数据会发生变化 - Data changes when coming back to it's component from another component 访问从一个组件到另一个组件的数据 - Accessing the data from one component to another component 使用来自父组件的数据更新状态,该状态是一个数组 - updating the state which is an array with the data coming from the parent Component 如何使用来自reducer的一些数据更新组件的状态 - How to update state of component with some data which is coming from reducer 使用 Axios 获取时,如何使用 ReactJs 中的 history.push 将数据数组传递给一个组件到另一个组件 - How to pass an array of data to one component to another component using history.push in ReactJs when fetched using Axios 在这里反应新手:如何将数据从一个组件导出到另一个组件? - React newbie here: How do I export data from one component to another? 反应-将数据从Axios req传递到另一个组件 - React - Pass data from Axios req to another component 如何使用异步将数据从一个组件获取到另一个组件并等待 - How to get data from one component to another using Async and await
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM