Skip to content

Commit e980793

Browse files
author
ahmadhussnain
committed
Added CategoriesIndex.vue component
* web.php route added for the categories. * index.blade.php renders the CategoriesIndex.vue component. * Added CategoriesIndex.vue component to show the categories in the HTML table.
1 parent 5df70a1 commit e980793

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

resources/js/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Vue.prototype._ = _;
99
Vue.component('front-page', require('./components/FrontPage.vue').default);
1010

1111
// id app
12+
let categoriesIndex = Vue.component('categories-index', require('./components/CategoriesIndex.vue').default);
1213
let categoriesCreate = Vue.component('categories-create', require('./components/CategoriesCreate.vue').default);
1314

1415
// Load third party vue component `laravel-vue-pagination`
@@ -18,6 +19,7 @@ Vue.component('pagination', require('laravel-vue-pagination'));
1819
new Vue({
1920
el: '#app',
2021
components: {
22+
categoriesIndex: categoriesIndex,
2123
categoriesCreate: categoriesCreate
2224
}
2325
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<template>
2+
<div class="container py-5">
3+
<div class="row">
4+
<div class="col-12">
5+
6+
<div v-if="loading" class="text-center">Loading....</div>
7+
<div v-if="Array.isArray(categories) && categories.length > 0" class="table-responsive">
8+
<a href="/categories/create" class="btn btn-primary my-4">Add new Category</a>
9+
<table class="table table-striped">
10+
<thead>
11+
<tr>
12+
<th scope="col">#</th>
13+
<th scope="col">Name</th>
14+
<th scope="col">Photo</th>
15+
<th scope="col">Actions</th>
16+
</tr>
17+
</thead>
18+
<tbody>
19+
20+
<tr v-for="(category, index) in categories" :key="category.id">
21+
<th scope="row">{{ index + 1 }}</th>
22+
<td>{{ _.capitalize(category.name) }}</td>
23+
<!-- Photo -->
24+
<td v-if="category.hasOwnProperty('photo') && category.photo">
25+
<img :src="category.photo" style="width: 80px;height: 80px" alt="Photo"/>
26+
</td>
27+
<td v-else><em>Null</em></td>
28+
<!-- End - Photo -->
29+
<td>
30+
<a :href="`/categories/${category.id}/edit`" class="btn btn-primary mr-2">Edit</a>
31+
<a :href="`/categories/${category.id}`" class="btn btn-danger">Delete</a>
32+
</td>
33+
</tr>
34+
</tbody>
35+
</table>
36+
</div>
37+
</div>
38+
</div>
39+
40+
</div>
41+
</template>
42+
43+
<script>
44+
export default {
45+
name: 'CategoriesIndex',
46+
data: function () {
47+
return {
48+
categories: [],
49+
loading: true
50+
}
51+
},
52+
mounted() {
53+
this.loadCategories();
54+
},
55+
methods: {
56+
loadCategories: function () {
57+
// Note: Please only destructure those properties which are read only
58+
// e.g. Props are read only and state should not be destructure.
59+
const self = this;
60+
axios.get('/api/categories').then(function (response) {
61+
setTimeout(function () {
62+
self.categories = response.data.data;
63+
self.loading = false;
64+
}, 2000);
65+
}).catch(function (err) {
66+
self.loading = false;
67+
console.log('Error from the GET Request', err);
68+
});
69+
}
70+
}
71+
72+
}
73+
</script>

resources/views/index.blade.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@extends('master')
2+
3+
@section('vue')
4+
<div id="app">
5+
<categories-index></categories-index>
6+
</div>
7+
@endsection
8+

routes/web.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
Route::view('/', 'home');
1919

2020
// Categories create.blade.php file for the form creation.
21+
Route::view('/categories', 'index')->name('categories.index');
2122
Route::view('/categories/create', 'create')->name('categories.create');

0 commit comments

Comments
 (0)