0

I am working on displaying and properly sorting data in a bootstrap table in VueJS.

I am trying to replace the date format in an array from January 21, 2010 format to MM/DD/YYYY format so it can be properly sorted by a bootstrap table. The array that is coming from the API has multiple values that are joined to fill one cell in the table. In order to do this, I have been using the join function on the mapped array. For the date field, since there is only ever one value, instead of joining I wanted to create a custom function that would change the date format as stated from ex. January 21, 2010 to 01/21/2010. I was planning on just hard coding this in methods.

When I attempted to declare a function in methods I keep getting the following error: [Vue warn]: Error in render: "TypeError: item.LastUpdatePostDate.newFunction is not a function" How would I go about fixing this? Is this a very inefficient way to change the date?

computed: {
    mappedItems() {
      return this.items.map((item) => {
        return {
          Ids: item.Ids.join(""),
          Acronyms: item.Acronyms.join(", "),
          LastUpdatePostDate: item.LastUpdatePostDate.newFunction(),
        };
      });
    },
    
  },
  methods: {
    newFunction: function () {
      
      return arguments
      
    },}

2 Answers 2

2

The value item.LastUpdatePostDate is a string... it has no newFunction method. The newFunction method is available via this.newFunction, and you should pass the date string as argument:

LastUpdatePostDate: this.newFunction(item.LastUpdatePostDate),

newFunction itself could look like this:

newFunction: function (dateString) {
    let [monthName, day, year] = dateString.match(/\w+/g);
    let month = "JanFebMarAprMayJunJulAugSepOctNovDec".indexOf(monthName.slice(0, 3)) / 3 + 1;
    return `${day}/${month}/${year}`.replace(/\b\d\b/g, "0$&");
}

But maybe give it a more telling name ;-)

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

Comments

1

You cannot call a vue method on a value.

Call the method with the value as argument:

...
    LastUpdatePostDate: this.newFunction(item.LastUpdatePostDate),
...
newFunction(date) {
    // do stuff with date
    return newvalue
}
....

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.