Vue js apply filter on v-model in an input field

前端 未结 7 1697
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-03 22:37

Hope someone can help me! I have made a directive wrapping the Jasny Bootstrap Plugin more specifically the input-mask thing and everything goes well!

Now I have made a

7条回答
  •  渐次进展
    2021-02-03 22:57

    I understand what you are trying to do, however, because of the two way binding when using v-model, it may be better to just format the date as you receive it from the server, and then, use it with the desired format in your front-end app ('DD/MM/YYYY').

    When sending the data back to the back-end, you just format it back to the desired server format ('YYYY-MM-DD').

    In your Vue app, the work flow would be something like this:

     new Vue({
        el: 'body',
        data: {
          date: null,
        },
        methods: {
            getDataFromServer: function() {
                    // Ajax call to get data from server
    
                    // Let's pretend the received date data was saved in a variable (serverDate)
                    // We will hardcode it for this ex.
                    var serverDate = '2015-06-26';
    
                    // Format it and save to vue data property
                    this.date = this.frontEndDateFormat(serverDate);
            },
            saveDataToServer: function() {
                // Format data first before sending it back to server
                var serverDate = this.backEndDateFormat(this.date);
    
                // Ajax call sending formatted data (serverDate)
            },
            frontEndDateFormat: function(date) {
                return moment(date, 'YYYY-MM-DD').format('DD/MM/YYYY');
            },
            backEndDateFormat: function(date) {
                return moment(date, 'DD/MM/YYYY').format('YYYY-MM-DD');
            }
        }
      });
    

    This works well for me, hope it helps.

    Here is a fiddle for it:

    https://jsfiddle.net/crabbly/xoLwkog9/

    Syntax UPDATE:

        ...
        methods: {
            getDataFromServer() {
                    // Ajax call to get data from server
    
                    // Let's pretend the received date data was saved in a variable (serverDate)
                    // We will hardcode it for this ex.
                    const serverDate = '2015-06-26'
    
                    // Format it and save to vue data property
                    this.date = this.frontEndDateFormat(serverDate)
            },
            saveDataToServer() {
                // Format data first before sending it back to server
                const serverDate = this.backEndDateFormat(this.date)
    
                // Ajax call sending formatted data (serverDate)
            },
            frontEndDateFormat(date) {
                return moment(date, 'YYYY-MM-DD').format('DD/MM/YYYY')
            },
            backEndDateFormat(date) {
                return moment(date, 'DD/MM/YYYY').format('YYYY-MM-DD')
            }
        }
      })
    

提交回复
热议问题