1

In my view i have this :

  <select class="sort_by">
       <option selected disabled>SORT BY</option>
       <option value="name" >Name</option>
       <option value="date">Date</option>
  </select>

In my controller i have this:

   $comments = $article->comments()->orderBy('created_at','desc')->get();

In my vue i have this:

 loadComments: function () {
        articleid = this.article_id;
        this.$http.get('/article/'+ articleid +'/allcomments').then(function(response){
            // True
          data = response.data.comments;
          this.$set('all_comments', data);
          this.comments= data;


        }, function(response){
            // False
        });

      },

What i want is when user select name or date, to change orderBy and then to display it in view without refresh. Any suggestion how can i do that?

EDIT: In my ready function i have:

this.loadComments();
          setInterval(function () {

           this.loadComments();
         }.bind(this), 20000);

So i cant sort by in vue.js, or can i?

6
  • Lodash is your friend for these things, and it's included in the default Laravel project just like Vue. lodash.com/docs/4.17.4#sortBy Commented Jan 13, 2017 at 11:16
  • I dont know is this what i need because in my ready function im loading comments every n seconds, and if i do like this i will get only for the first time filter but in next time when comments are loaded i will get default sort... Commented Jan 13, 2017 at 11:21
  • Ok, wait a moment, I will write an answer. Commented Jan 13, 2017 at 11:31
  • add v-model attribute to a select like v-model="filter" and then filter output by this parameter whenever you need it by passing to a component via v-for="comment in comments | filterBy filter" Commented Jan 13, 2017 at 11:43
  • its not working for me Commented Jan 13, 2017 at 11:50

1 Answer 1

1

You can use the Lodash sortBy method inside a computed property which acts as a filter. Lodash is included by default in the newest Laravel versions.

Your .vue file could look like this:

(NOTE: This is a Vue 2.0 example, if you are using a previous version of Vue some things can differ)

<template>

    <select v-model="sortingBy" class="sort_by">
        <option selected disabled>SORT BY</option>
        <option value="name">Name</option>
        <option value="date">Date</option>
    </select>

    <div v-for="comment in sortedComments">
        // Here your HTML for each comment
    <div>

</template>

<script>
    export default {

        data() {
            return{
                comments: {},
                sortingBy: "name", // Default sorting type
            }
        },

        computed: {
            sortedComments: function () {
                return _.sortBy(this.comments, [this.sortingBy]);
            }
        },

        mounted() {
            this.loadComments();
            setInterval(function () {
                this.loadComments();
            }.bind(this), 20000);
        },

        methods: {
            loadComments: function () {
                this.$http.get('/article/' + this.article_id + '/allcomments').then((response) => {
                    this.comments = response.data;
                });
            },
        }
    }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

im not using vue 2.0 its 1.0.26

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.