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.

vue.html 6.6KB

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