0

I am trying to make a sort table in Laravel using Vue.js. I tried some solutions on the internet, but it still doesn't work for me.

Here is my template file:

<template>
  <div class="container">
    <table class="table table-bordered">
      <thead>
        <tr>
          <th v-for="column in columns">{{ column }}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(emp, key) in emp">
          <td>{{ emp.id }}</td>
          <td>{{ emp.emp_name }}</td>
          <td>{{ emp.salary }}</td>
          <td>{{ emp.phone_number }}</td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

My script file:

<script>
export default {
  data() {
    return {
      emp: [],
      columns: ['No','Name', 'Salary', 'Phone Number']
    }
  },
  mounted() {
    this.fetchUser();
  },
  methods: {
    fetchUser(){    
      fetch('api/employee').then(res => res.json())
      .then(res => {
        this.emp = res.data;
        console.log(res);
      })
    },  
  }
}
</script>

I'll appreciate of all your help. Thanks in advance..

6
  • what is the error ? Commented Aug 4, 2019 at 15:50
  • i dont know how to sort table Commented Aug 4, 2019 at 15:54
  • 1
    what do you mean by sort , you can sort your data from you backend [ laravel ] Commented Aug 4, 2019 at 15:56
  • want to sort it by click using vue.js bro Commented Aug 4, 2019 at 16:00
  • <div v-if="reverseList"> {{ reverseList }} </div> <button v-on:click="reverseList">Reverse list</button> methods: { reverseList() { return emp.reverse(); } } Commented Aug 4, 2019 at 16:09

1 Answer 1

1

new Vue({
  el: "#app",
  data() {
    return {
      emp: [{
          id: 1,
          emp_name: "Andrew",
          salary: 256,
          phone_number: "123"
        }, {
          id: 2,
          emp_name: "John",
          salary: 512,
          phone_number: "321"
        },
        {
          id: 3,
          emp_name: "Sam",
          salary: 1024,
          phone_number: "456"
        }
      ],
      columns: [{
        label: "No"
      }, {
        label: "Name",
        key: "emp_name",
        sortable: true
      }, {
        label: "Salary",
        key: "salary",
        sortable: true,
        isNumber: true
      }, {
        label: "Phone Number"
      }],
      sortKey: "",
      sortOrder: "desc"
    }
  },
  computed: {
    sortedEmp() {
      if (!this.sortKey) return this.emp;

      const arr = JSON.parse(JSON.stringify(this.emp));
      const {
        sortKey,
        sortOrder
      } = this;

      if (this.columns.find(column => column.key === sortKey).isNumber) {
        arr.sort((a, b) => this.numberComporator(a[sortKey], b[sortKey]));
      }

      return sortOrder === "asc" ? arr : arr.reverse();
    }
  },
  mounted() {
    this.fetchUser();
  },
  methods: {
    fetchUser() {
      fetch('api/employee').then(res => res.json())
        .then(res => {
          this.emp = res.data;
        })
    },
    sortHandler(key) {
      if (key === this.sortKey) {
        this.sortOrder = this.sortOrder === "asc" ? "desc" : "asc";
      } else {
        this.sortKey = key;
      }
    },
    numberComporator(a, b) {
      return a - b;
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app" class="container">
  <table class="table table-bordered">
    <thead>
      <tr>
        <th v-for="column in columns" :key="column.label">
          {{ column.label }}
          <button v-if="column.sortable" type="button" @click="sortHandler(column.key)">
            Sort
            <span v-if="sortKey === column.key">{{ sortOrder }}</span>
          </button>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="emp in sortedEmp" :key="emp.id">
        <td>{{ emp.id }}</td>
        <td>{{ emp.emp_name }}</td>
        <td>{{ emp.salary }}</td>
        <td>{{ emp.phone_number }}</td>
      </tr>
    </tbody>
  </table>
</div>

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

1 Comment

What explanations do you need?)

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.