0

I have a vue function that I'm calling on page creation called fetchReport

That works fine, and I also have 2 other functions that I'm calling on change of select elements, which work as well, but here's my problem:

Currently, if I call handleTypeSelect or handleLocationSelect, they both submit the correct value but they submit it as itemType in the fetchReport call every time. I"m assuming it's because they only send one parameter value and that's the first parameter.

How can I modify this so that handleTypeSelect sends the value as itemType and handleLocationSelect sends the value as locationID?

created() {
    this.fetchReport();
},
methods: {
  fetchReport(itemType = '3', locationID = '10', startDate = new Date().toISOString().substring(0,10), endDate = new Date().toISOString().substring(0,10)) {
      axios.get('/report/assets/data', {params:{itemType: itemType, startDate: startDate, endDate: endDate, locationID: locationID}})
      .then(response => {
        // handle success
        console.log(response.data)
        this.rows = response.data
      })
  },
  handleTypeSelect() {
      this.fetchReport(itemTypes.value);
  },
  handleLocationSelect() {
      this.fetchReport(itemLocations.value);
  }
}

3 Answers 3

1

Refactor fetchReport's arguments:

Instead of taking a series of arguments (arg1, arg2, arg3), take a single parameter (arg)

This arg parameter will be an object including all the properties you need as arguments. As a followup, move the default values inside the function.

Result for you would be:

  fetchReport(myParams) {
      const defaults = { itemType: '3', locationID: '10', .... }; // list all other defaults, same as previous arguments
      const params = Object.assign(defaults, myParams);
      axios.get('/report/assets/data', { params })
      .then(response => {
        // handle success
        console.log(response.data)
        this.rows = response.data
      })
  },

Then, when you need to call fetchReport:

fetchReport({ itemType: 'MyValue' })

fetchReport({ locationID: 'MyValue' })

This is the best practice way of handling functions with multiple parameters when you don't want to think about order of parameters

Sign up to request clarification or add additional context in comments.

Comments

0

Use undefined to pass the first parameter.

 handleLocationSelect() {
      this.fetchReport(undefined, itemLocations.value);
  }

2 Comments

Won't that mess up the default value of '3' that I have for taskType, and instead send undefined?
@TomN. It won't . Default 3 will be used
0

You need to pass two params.

Use void to get the value undefined, because window.undefined can be changed in older browsers.

handleLocationSelect() {
    this.fetchReport(void, itemLocations.value);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.