From a32f0f5c1dfabc480e9014cb6671921000645c08 Mon Sep 17 00:00:00 2001
From: Saladin21 <13519187@std.stei.itb.ac.id>
Date: Thu, 31 Mar 2022 22:56:20 +0700
Subject: [PATCH] add table with endpoint props

---
 src/components/table-2.vue | 205 +++++++++++++++++++++++++++++++++++++
 src/components/table.vue   |   3 +-
 src/http-common.js         |   6 +-
 src/router/index.js        |   6 ++
 src/views/Test.vue         |  66 ++++++++++++
 5 files changed, 281 insertions(+), 5 deletions(-)
 create mode 100644 src/components/table-2.vue
 create mode 100644 src/views/Test.vue

diff --git a/src/components/table-2.vue b/src/components/table-2.vue
new file mode 100644
index 0000000..8951921
--- /dev/null
+++ b/src/components/table-2.vue
@@ -0,0 +1,205 @@
+<template>
+  <div class="table-responsive">
+    <table class="table table-bordered">
+      <thead>
+        <tr>
+          <template  v-for="c in columns" :key="c">
+          <th
+            v-if="!c.hidden"
+            @click="sort(c.data)"
+            class="text-blue clickable"
+          >
+            {{ c.name}}
+          </th>
+          </template>
+          <th class="text-blue">Aksi</th>
+        </tr>
+      </thead>
+      <tbody>
+        
+        <tr
+          v-for="e in this.entries.slice(
+            (this.page - 1) * this.entry_per_page,
+            this.page * this.entry_per_page
+          )"
+          :key="e.id"
+        >
+            <template v-for="c in columns" :key="c">
+              <template v-if="!c.hidden">
+                <td v-if="c.clickable" @click="this.$emit('detail-entry', e[columns.Id.data])" class="clickable">
+                    {{ e[c.data] }}
+                </td>
+                <td v-else >
+                    {{ e[c.data]}}
+                </td>
+              </template>
+            </template>
+
+          <td style="text-align: center">
+            <button @click="this.$emit('edit-entry', e[columns.Id.data])" class="btn btn-blue"> <i class="icon ion-android-create" style="font-size: 20px"></i> Edit </button>
+            <button @click="this.$emit('delete-entry', e[columns.Id.data])" class="btn-red"> <i class="icon ion-ios-trash-outline" style="font-size: 23px"></i> </button>
+          </td>
+        </tr>
+      </tbody>
+    </table>
+    <nav aria-label="Page navigation">
+      <ul class="pagination justify-content-end">
+        <li v-if="this.page > 1" class="page-item">
+          <div class="page-link" @click="prevPage()">Previous</div>
+        </li>
+        <li v-else class="page-item disabled">
+          <div class="page-link">Previous</div>
+        </li>
+        <template v-for="p in this.pagination" :key="p">
+          <li v-if="this.page == p" class="page-item disabled">
+            <div class="page-link">{{ p }}</div>
+          </li>
+          <li v-else class="page-item">
+            <div class="page-link" @click="goPage(p)">{{ p }}</div>
+          </li>
+        </template>
+        <li v-if="this.page < this.page_num" class="page-item">
+          <div class="page-link" @click="nextPage()">Next</div>
+        </li>
+        <li v-else class="page-item disabled">
+          <div class="page-link">Next</div>
+        </li>
+      </ul>
+    </nav>
+  </div>
+</template>
+
+<script>
+
+import { HTTP } from '../http-common'
+
+export default {
+  name: "Table2",
+  props: {
+    //map nama kolom ke data
+    columns: Object,
+    endpoint: String,
+    filter: String,
+
+  },
+  data() {
+    return {
+      pagination: [],
+      sorted: {
+        column: "",
+        asc: true,
+      },
+      entries: [],
+      entry_per_page: 10,
+      page: 1,
+      page_num: 0,
+    };
+  },
+  mounted() {
+      this.getData(1)
+  },
+  methods: {
+    getData(page){
+        HTTP.get(`${this.endpoint}?page=${page}`).then((res)=>{
+            if (res.status == 200){
+                this.entries = res.data.data.data
+                this.page = res.data.data.current_page
+                this.entry_per_page = res.data.data.per_page
+                this.page_num = res.data.data.last_page
+                this.updatePagination();
+            }
+        })
+    },
+    sort(column) {
+        console.log("sort "+ column)
+      if (column === this.sorted.column) {
+        this.entries.reverse();
+        this.sorted.asc = !this.sorted.asc;
+      } else {
+        var col = this.columns[column];
+        this.entries.sort(function (a, b) {
+          let x = a[col];
+          let y = b[col];
+          if (x < y) {
+            return -1;
+          }
+          if (x > y) {
+            return 1;
+          }
+          return 0;
+        });
+        this.sorted.column = column;
+      }
+      this.goPage(1);
+    },
+    nextPage() {
+      if (this.page < this.page_num) {
+        this.page += 1;
+        this.getData(this.page)
+      }
+    },
+    prevPage() {
+      if (this.page > 1) {
+        this.page -= 1;
+        this.getData(this.page)
+      }
+    },
+    goPage(i) {
+      this.page = i;
+      this.getData(this.page)
+    },
+    updatePagination() {
+      this.pagination = [];
+      for (let i = this.page - 2; i <= this.page + 2; i++) {
+        if (i <= this.page_num && i > 0) {
+          this.pagination.push(i);
+        }
+      }
+    },
+    /* eslint-disable no-unused-vars */
+    filterEntry(entry) {
+      for (const [key, value] of Object.entries(this.columns)) {
+        if (entry[value.data].toLowerCase().includes(this.filter)) {
+          return true;
+        }
+      }
+      return false;
+    },
+    /* eslint-enable no-unused-vars */
+  },
+};
+</script>
+
+<style scoped>
+li {
+  padding: 0;
+}
+
+.table-bordered td,
+.table-bordered th,
+.table-bordered thead {
+  border: 1px solid #6992b4 !important;
+}
+
+.btn-blue {
+  background-color: #6992b4;
+  color: #f1f7fc;
+  border-radius: 15px;
+  padding: 5px;
+  margin-right: 5px;
+  vertical-align:middle
+}
+
+.btn-red {
+  background-color: #f4476b;
+  color: #f1f7fc;
+  border-radius: 8px;
+  padding-left: 5px;
+  padding-right: 5px;
+  vertical-align:middle
+}
+
+th{
+    text-align: center;
+}
+</style>
diff --git a/src/components/table.vue b/src/components/table.vue
index f418a5f..e7d9e76 100644
--- a/src/components/table.vue
+++ b/src/components/table.vue
@@ -43,7 +43,7 @@
       </tbody>
     </table>
     <nav aria-label="Page navigation">
-      <ul class="pagination justify-content-end">
+      <ul class="pagination justify-content-end clickable">
         <li v-if="this.page > 1" class="page-item">
           <div class="page-link" @click="prevPage()">Previous</div>
         </li>
@@ -80,7 +80,6 @@ export default {
   },
   data() {
     return {
-      // entries: [],
       pagination: [],
       sorted: {
         column: "",
diff --git a/src/http-common.js b/src/http-common.js
index 79a652d..00d782e 100644
--- a/src/http-common.js
+++ b/src/http-common.js
@@ -2,7 +2,7 @@ import axios from 'axios';
 
 export const HTTP = axios.create({
   baseURL: `https://simkpi-backend.herokuapp.com/api`,
-//   headers: {
-//     Authorization: 'Bearer {token}'
-//   }
+  // headers: {
+  //   Authorization: 'Bearer {token}'
+  // }
 })
diff --git a/src/router/index.js b/src/router/index.js
index 02fd825..aac63d1 100644
--- a/src/router/index.js
+++ b/src/router/index.js
@@ -11,6 +11,7 @@ import KaryawanEdit from "../views/KaryawanEdit.vue";
 import AdminInsert from "../views/AdminInsert.vue";
 import AdminEdit from "../views/AdminEdit.vue";
 import KpiIndex from "../views/KpiIndex.vue";
+import Test from "../views/Test.vue";
 
 const routes = [
   {
@@ -73,6 +74,11 @@ const routes = [
     name: "kpiIndex",
     component: KpiIndex,
   },
+  {
+    path: "/test",
+    name: "test",
+    component: Test,
+  },
 ];
 
 const router = createRouter({
diff --git a/src/views/Test.vue b/src/views/Test.vue
new file mode 100644
index 0000000..fea4a46
--- /dev/null
+++ b/src/views/Test.vue
@@ -0,0 +1,66 @@
+<template>
+    <div class="container-fluid">
+            <div class="row flex-nowrap">
+                <Sidebar current_page = "About"></Sidebar>
+                <div class="col ps-md-2 pt-2">
+                    <Header></Header>
+                        <Table2 :endpoint="'kpis'" :columns="columns" @edit-entry="print" @delete-entry="print" @detail-entry="print" ></Table2>
+                </div>
+            </div>
+    </div>
+</template>
+
+<script>
+
+import Header from "../components/header"
+import Sidebar from "../components/sidebar"
+import Table2 from "../components/table-2";
+
+
+export default {
+    name: "Template",
+    components: {
+        Header,
+        Sidebar,
+        Table2
+    }, 
+    data(){
+        return{
+            columns: {
+                Id: {
+                name : "Id",
+                data : "id",
+                hidden : true,
+                clickable : false
+                },
+                Nama: {
+                name : "Nama",
+                data : "name",
+                hidden : false,
+                clickable : true
+                },
+            },
+        }
+    },
+    methods: {
+        print(id){
+            console.log(id)
+        }
+    }
+}
+</script>
+
+<style scoped>
+* {
+    margin: 0;
+    padding: 0;
+    box-sizing: border-box;
+}
+
+body {
+    min-height: 100vh;
+    background-color: #fff;
+}
+
+
+</style>
\ No newline at end of file
-- 
GitLab