Advertisement
stanzinofree

Svelte_refresh_table

Nov 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script context="module">
  2.     export function preload({ params, query }) {
  3.         return this.fetch(`http://localhost:1337/projects`).then(r => r.json()).then(projects => {
  4.             return { projects };
  5.         });
  6.     }
  7. </script>
  8.  
  9. <script>
  10.     import { afterUpdate } from 'svelte';
  11.     import axios from 'axios';
  12.     import { goto } from '@sapper/app';
  13.     import New from './new.svelte';
  14.  
  15.     export let projects;
  16.     let nome;
  17.     let descrizione;
  18.  
  19.     function progetti(){
  20.         fetch(`http://localhost:1337/projects`).then(r => r.json()).then(projects => {
  21.             return { projects };
  22.         });
  23.     }
  24.  
  25.     function disattiva(id){
  26.         axios.put(`http://localhost:1337/projects/${id}`, {
  27.         active: false
  28.         })
  29.         .then(function (response) {
  30.             progetti()
  31.             goto('/projects');
  32.         })
  33.         .catch(function (error) {
  34.             console.log(error);
  35.         });
  36.     }
  37.  
  38.     function attiva(id){
  39.         axios.put(`http://localhost:1337/projects/${id}`, {
  40.         active: true
  41.         }).then(function (response) {
  42.             progetti()
  43.             goto('/projects');
  44.         }).catch(function (error) {
  45.             console.log(error);
  46.         });
  47.     }
  48.  
  49.     function add_project(){
  50.             axios.post('http://localhost:1337/projects', {
  51.             nome: nome,
  52.             descrizione: descrizione
  53.         })
  54.         .then(function (response) {
  55.             console.log(response);
  56.             window.$('#newproject').modal('hide');
  57.             goto('/projects');
  58.         })
  59.         .catch(function (error) {
  60.             console.log(error);
  61.         });
  62.     }
  63. </script>
  64.  
  65. <style>
  66. </style>
  67.  
  68. <svelte:head>
  69.     <title>Projects</title>
  70. </svelte:head>
  71.  
  72.  
  73. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#newproject">
  74.   Aggiungi nuovo progetto
  75. </button>
  76.  
  77.  
  78. <div class="modal fade" id="newproject" tabindex="-1" role="dialog" aria-labelledby="newproject" aria-hidden="true" >
  79.   <div class="modal-dialog" role="document">
  80.     <div class="modal-content">
  81.       <div class="modal-header">
  82.         <h5 class="modal-title" id="newproject">Aggiungi Progetto</h5>
  83.         <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  84.           <span aria-hidden="true">&times;</span>
  85.         </button>
  86.       </div>
  87.       <div class="modal-body">
  88.         <form on:submit|preventDefault={add_project}>
  89.             <input class="js-todo-input" type="text" aria-label="Inserisci un nuovo progetto" placeholder="E.g. Build a web app" bind:value={nome}>
  90.             <input class="js-todo-input" type="text" aria-label="Inserisci una descrizione" placeholder="E.g. Build a web app" bind:value={descrizione}>
  91.             <button bind={add_project}>Crea</button>
  92.         </form>
  93.       </div>
  94.     </div>
  95.   </div>
  96. </div>
  97.  
  98.  
  99. <table class="table table-striped table-dark">
  100.   <thead>
  101.     <tr>
  102.       <th scope="col">Nome</th>
  103.       <th scope="col">Descrizione</th>
  104.       <th scope="col">Active</th>
  105.     </tr>
  106.   </thead>
  107.   <tbody>
  108.     {#each projects as project}
  109.         <tr>
  110.             <th scope="row"><a rel='prefetch' href='projects/{project._id}'>{project.nome}</a></th>
  111.             <td>{project.descrizione}</td>
  112.             <td>
  113.             {#if project.active == true}
  114.                 <button type="button" class="btn btn-success" on:click={() => disattiva(project._id)}>Attivo</button>
  115.             {:else}
  116.                 <button type="button" class="btn btn-danger" on:click={() => attiva(project._id)}>Disattiva</button>
  117.             {/if}
  118.             </td>
  119.             <td>{project.active}</td>
  120.         </tr>
  121.     {/each}
  122.   </tbody>
  123. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement