Home > Software design >  pushing selected items in an array so it can be used on next page
pushing selected items in an array so it can be used on next page

Time:12-21

I have this code which is pushing the full row to the variable i declared and then pushing it to the next page where it is needed.

let HistoryData = [];
        for (const [key, value] of Object.entries(this.Items)) {
          HistoryData.push(value)  
        }
this.$router.push({ name: "History" });

i only want to send the following keys with keys and its values: instead of full row

"Code": "red",                                                  
"AccNumber": "12345",                                               
"InvNum": "1234"

the above can be multiple as i select multiple rows and then the above can be a multiple values as objects inside an array

i think i need some filters to filter down to send me only this

CodePudding user response:

For something this simple, I would just create an array of the keys you want to send and then do an if statement before deciding whether or not to push it.

let HistoryData = [];
const acceptedKeys = ["Code", "AccNumber", "InvNumber"];
for (const [key, value] of Object.entries(this.Items)) {
  if (acceptedKeys.includes(key))
    HistoryData.push(value)  
}
this.$router.push({ name: "History" });

CodePudding user response:

If I understand correctly, you want to pass your data and use this data on your "History" page.

Firstly parse your 3 rows and push them into your array:

let keysToBePushed = ["Code", "AccNumber", "InvNum"];
let historyData = [];
for (let i = 0; i < Object.keys(this.Items).length; i  ) {
  if (keysToBePushed.includes(Object.keys(this.Items)[i])) {
    historyData.push(this.Items[i]);  
  }
}

Then, send the data to your next page:

this.$router.push({
     name: 'History', // Write here your own path's name.
     params: {
                historyData: historyData
             }
})

Finally, go to your mounted() method inside of the view that related with "History" route and fetch your data. After that, do whatever you want with them:

mounted(): void {
    if (this.$route.params.historyData.length > 0) {
     // some code here
    }
}

But please try to provide more details about what you want. I had to make guesses here.

  • Related