api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <html lang="en">
  2. <head>
  3. <meta charset="utf-8">
  4. <meta http-equiv="x-ua-compatible" content="ie=edge">
  5. <title>Vue.js CRUD application</title>
  6. <meta name="description" content="">
  7. <meta name="viewport" content="width=device-width, initial-scale=1">
  8. <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.1.10/vue.js"></script>
  9. <script src="https://cdnjs.cloudflare.com/ajax/libs/vue-router/2.2.1/vue-router.js"></script>
  10. <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.15.3/axios.js"></script>
  11. <script src="../lib/php_crud_api_transform.js"></script>
  12. <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
  13. <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap-theme.min.css">
  14. <style>
  15. .logo {
  16. width: 50px;
  17. float: left;
  18. margin-right: 15px;
  19. }
  20. .form-group {
  21. max-width: 500px;
  22. }
  23. .actions {
  24. padding: 10px 0;
  25. }
  26. .glyphicon-euro {
  27. font-size: 12px;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div class="container">
  33. <header class="page-header">
  34. <div class="branding">
  35. <img src="https://vuejs.org/images/logo.png" alt="Logo" title="Home page" class="logo"/>
  36. <h1>Vue.js CRUD application</h1>
  37. </div>
  38. </header>
  39. <main id="app">
  40. <router-view></router-view>
  41. </main>
  42. </div>
  43. <template id="post-list">
  44. <div>
  45. <div class="actions">
  46. <router-link class="btn btn-default" v-bind:to="{path: '/add-post'}">
  47. <span class="glyphicon glyphicon-plus"></span>
  48. Add post
  49. </router-link>
  50. </div>
  51. <div class="filters row">
  52. <div class="form-group col-sm-3">
  53. <label for="search-element">Filter</label>
  54. <input v-model="searchKey" class="form-control" id="search-element" requred/>
  55. </div>
  56. </div>
  57. <table class="table">
  58. <thead>
  59. <tr>
  60. <th>Content</th>
  61. <th class="col-sm-2">Actions</th>
  62. </tr>
  63. </thead>
  64. <tbody>
  65. <tr v-if="posts===null">
  66. <td colspan="4">Loading...</td>
  67. </tr>
  68. <tr v-else v-for="post in filteredposts">
  69. <td>
  70. <router-link v-bind:to="{name: 'post', params: {post_id: post.id}}">{{ post.content }}</router-link>
  71. </td>
  72. <td>
  73. <router-link class="btn btn-warning btn-xs" v-bind:to="{name: 'post-edit', params: {post_id: post.id}}">Edit</router-link>
  74. <router-link class="btn btn-danger btn-xs" v-bind:to="{name: 'post-delete', params: {post_id: post.id}}">Delete</router-link>
  75. </td>
  76. </tr>
  77. </tbody>
  78. </table>
  79. </div>
  80. </template>
  81. <template id="add-post">
  82. <div>
  83. <h2>Add new post</h2>
  84. <form v-on:submit="createpost">
  85. <div class="form-group">
  86. <label for="add-content">Content</label>
  87. <textarea class="form-control" id="add-content" rows="10" v-model="post.content"></textarea>
  88. </div>
  89. <button type="submit" class="btn btn-primary">Create</button>
  90. <router-link class="btn btn-default" v-bind:to="'/'">Cancel</router-link>
  91. </form>
  92. </div>
  93. </template>
  94. <template id="post">
  95. <div>
  96. <b>Content: </b>
  97. <div>{{ post.content }}</div>
  98. <br/>
  99. <span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span>
  100. <router-link v-bind:to="'/'">Back to post list</router-link>
  101. </div>
  102. </template>
  103. <template id="post-edit">
  104. <div>
  105. <h2>Edit post</h2>
  106. <form v-on:submit="updatepost">
  107. <div class="form-group">
  108. <label for="edit-content">Content</label>
  109. <textarea class="form-control" id="edit-content" rows="3" v-model="post.content"></textarea>
  110. </div>
  111. <button type="submit" class="btn btn-primary">Save</button>
  112. <router-link class="btn btn-default" v-bind:to="'/'">Cancel</router-link>
  113. </form>
  114. </div>
  115. </template>
  116. <template id="post-delete">
  117. <div>
  118. <h2>Delete post {{ post.id }}</h2>
  119. <form v-on:submit="deletepost">
  120. <p>The action cannot be undone.</p>
  121. <button type="submit" class="btn btn-danger">Delete</button>
  122. <router-link class="btn btn-default" v-bind:to="'/'">Cancel</router-link>
  123. </form>
  124. </div>
  125. </template>
  126. <script>
  127. var posts = null;
  128. var api = axios.create({
  129. baseURL: '../api.php'
  130. });
  131. function findpost (postId) {
  132. return posts[findpostKey(postId)];
  133. };
  134. function findpostKey (postId) {
  135. for (var key = 0; key < posts.length; key++) {
  136. if (posts[key].id == postId) {
  137. return key;
  138. }
  139. }
  140. };
  141. var List = Vue.extend({
  142. template: '#post-list',
  143. data: function () {
  144. return {posts: posts, searchKey: ''};
  145. },
  146. created: function () {
  147. var self = this;
  148. api.get('/posts').then(function (response) {
  149. posts = self.posts = php_crud_api_transform(response.data).posts;
  150. }).catch(function (error) {
  151. console.log(error);
  152. });
  153. },
  154. computed: {
  155. filteredposts: function () {
  156. return this.posts.filter(function (post) {
  157. return this.searchKey=='' || post.content.indexOf(this.searchKey) !== -1;
  158. },this);
  159. }
  160. }
  161. });
  162. var post = Vue.extend({
  163. template: '#post',
  164. data: function () {
  165. return {post: findpost(this.$route.params.post_id)};
  166. }
  167. });
  168. var postEdit = Vue.extend({
  169. template: '#post-edit',
  170. data: function () {
  171. return {post: findpost(this.$route.params.post_id)};
  172. },
  173. methods: {
  174. updatepost: function () {
  175. var post = this.post;
  176. api.put('/posts/'+post.id,post).then(function (response) {
  177. console.log(response.data);
  178. }).catch(function (error) {
  179. console.log(error);
  180. });
  181. router.push('/');
  182. }
  183. }
  184. });
  185. var postDelete = Vue.extend({
  186. template: '#post-delete',
  187. data: function () {
  188. return {post: findpost(this.$route.params.post_id)};
  189. },
  190. methods: {
  191. deletepost: function () {
  192. var post = this.post;
  193. api.delete('/posts/'+post.id).then(function (response) {
  194. console.log(response.data);
  195. }).catch(function (error) {
  196. console.log(error);
  197. });
  198. router.push('/');
  199. }
  200. }
  201. });
  202. var Addpost = Vue.extend({
  203. template: '#add-post',
  204. data: function () {
  205. return {post: {content: '', user_id: 1, category_id: 1}}
  206. },
  207. methods: {
  208. createpost: function() {
  209. var post = this.post;
  210. api.post('/posts',post).then(function (response) {
  211. post.id = response.data;
  212. }).catch(function (error) {
  213. console.log(error);
  214. });
  215. router.push('/');
  216. }
  217. }
  218. });
  219. var router = new VueRouter({routes:[
  220. { path: '/', component: List},
  221. { path: '/post/:post_id', component: post, name: 'post'},
  222. { path: '/add-post', component: Addpost},
  223. { path: '/post/:post_id/edit', component: postEdit, name: 'post-edit'},
  224. { path: '/post/:post_id/delete', component: postDelete, name: 'post-delete'}
  225. ]});
  226. app = new Vue({
  227. router:router
  228. }).$mount('#app')
  229. </script>
  230. </body>
  231. </html>