Search code examples
node.jspostgresqlvue.jsvuetify.jspostgrest

Get vuetify.js checkbox selected values


So I've been trying to get some values from a postgres db with postgREST, show them in a table with vuetify and have checkboxes next to every value so that the user can delete a row from the table.

My problem is I don't know how to get the selected values from the checkbox, or even better only the ID column and use them in my script.

My html:

<template>
  <p v-if="state == 'error' " class="red">{{ error }}</p>

  <div v-else-if="state == 'ready' ">
      <div id="app">
          <v-app id="inspire">
                                  
            <button style="font-face: 'Comic Sans MS'; font-size: larger; color: white; background-color: #1e1e1e; " v-on:click="greet">Delete</button>
                  
            <h5  style="font-face: 'Comic Sans MS'; font-size: larger; color: white; background-color: #1e1e1e; " >Selected: {{ selected }}</h5>

            <v-data-table dark
                    v-model="selected" 
                    :headers="headers"
                    :items="exa_tb"
                    item-key="id" 
                    select-all
                    show-select
                    :items-per-page="3"
                    class="elevation-1">
            </v-data-table> 
            
            <button style="font-face: 'Comic Sans MS'; font-size: larger; color: white; background-color: #1e1e1e; " onClick="window.location.reload();">Refresh Page</button>
          
          </v-app>
      </div>
  </div>

  <p v-else-if="state == 'loading' "> Loading Messages...</p>  
        
</template> 

my javasript :

<script> 
  export default {
    data () {
      return {
        singleSelect: false,
        selected: [],
        
        headers: [{
            text: 'ID',
            align: 'start',
            sortable: false,
            value: 'id',
          },
          { text: 'Message', value: 'msg' },
          { text: 'Timestamp', value: 'timesmp' }, 
        ],
        exa_tb: [],
        error: '',
        state: 'loading',
      }
    },

    
    created(){
      this.loadExa_tb();
    },


    methods: {  
    greet: function () { 
      if(confirm('Are you sure you want to delete this item?')){
        for(var i = 1; i <=this.selected.length; i++){  
          
            var delete_this = //with the value from the selected checkbox 

            fetch('http://localhost:3000/exa_tb?id=eq.' + delete_this, {
            method: 'DELETE',
          })
          .then(res => res.text()) 
          .then(res => console.log(res))
        
        }
      }
    },
        async loadExa_tb(){
          try {
            const exa_tb = await fetch('http://localhost:3000/exa_tb');
            this.exa_tb = await exa_tb.json();
            this.state = 'ready';
          } catch(err) {
              this.error = err;
              this.state = 'error';
          }
        },  
      } 
  };
</script>

what my table looks like atm


Solution

  • You have just to get the value from the array:

    var delete_this = selected[i].id 
    

    Also array indexes start from 0 and your for loop starts from 1. so you have to change your for loop too