Already done by someone on github, search and find it on github and study and get ideas how they did it. https://nortonsafe.search.ask.com/web?q=vue%20editable%20table&chn=prev&o=APN11910&geo=en_US&prt=&ctype=&ver=&tpr=121
How to create an Editable Table with Vue.js
I have recently started to learn Laravel and try to now get into Vue.js at the same time. My project got a page,which displays a list of Users which get read out from the database. I now thought that it would be nice to be able to add a new User directly on that page so I looked around how I could achieve this with Vue and found a nice script. I now decided that it would be nice to have the option to "Edit" as well, so I tried to look around how to achieve this and got totally lost. I thought that a click on a column should maybe add a input field with the cell value in it, but how is this possible to achieve? How could I let my clickList function know which table cell I clicked and how would I add the input field back? And would I not need to send a form tag back as well to be able to save it ? Should I add a Form tag to around the and add a submit button next to the "trash" so that you have to click on that after you are finish to save the changes? Or is there a better solution? Does anyone know of a Tutorial I should read?
my view index.blade.php
<div id="vue-wrapper">
<div class="content">
<p class="text-center alert alert-danger" v-if="hasError">Please enter some value!</p>
{{ csrf_field() }}
<p class="text-center alert alert-success" v-if="hasDeleted">Deleted Successfully!</p>
<div class="table table-borderless" id="table">
<table class="table table-borderless" id="table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Postcode</th>
<th>Actions</th>
</tr>
</thead>
<tr v-for="item in items">
<td v-on:click="clickList(item)">@{{ item.id }}</td>
<td v-on:click="clickList(item)">@{{ item.name }}</td>
<td>@{{ item.postcode }}</td>
<td @click.prevent="deleteItem(item)" class="btn btn-danger">
<span class="glyphicon glyphicon-trash"></span></td>
</tr>
<tr>
<div class="form-group row">
<th></th>
<th><input type="text" class="form-control" id="name" name="name" required v-model="newItem.name" placeholder=" Enter some name"></th>
<th><input type="text" class="form-control" id="name" name="postcode" required v-model="newItem.postcode" placeholder=" Postcode"></th>
<th><button class="btn btn-primary" @click.prevent="createItem()" id="name" name="name">
<span class="glyphicon glyphicon-plus"></span> ADD </button>
</th>
</div>
</tr>
</table>
</div>
</div>
</div>
resources\assets\js\app.js
const app = new Vue({
el: '#vue-wrapper',
data: {
items: [],
hasError: false,
hasDeleted: false,
but: '',
newItem : {'name':''}
},
mounted : function(){
this.getVueItems();
},
methods : {
getVueItems: function(){
axios.get('/vueitems').then(response => {
this.items = response.data;
});
},
createItem: function(){
this.hasDeleted = false;
var input = this.newItem;
if(input['name'] == ''){
this.hasError = true;
this.hasDeleted = false;
}
else{
this.hasError = false;
axios.post('/vueitems',input)
.then(response => {
this.newItem = {'name':''};
this.getVueItems();
});
this.hasError = false;
}
},
clickList: function (item) {
var $row = $(item).parents('tr'); //accede a la fila
var $cols = $row.find('td'); //lee campos
console.log("clickList fired with " + $row +item.id);
},
editItem: function(but){
var $row = $(but).parents('tr'); //accede a la fila
var $cols = $row.find('td'); //lee campos
if (ModoEdicion($row)) return; //Ya está en edición
//Pone en modo de edición
IterarCamposEdit($cols, function($td) { //itera por la columnas
var cont = $td.html(); //lee contenido
var div = '<div style="display: none;">' + cont + '</div>'; //guarda contenido
var input = '<input class="form-control input-sm" value="' + cont + '">';
$td.html(div + input); //fija contenido
});
FijModoEdit(but);
},
saveItem: function(item){
},
deleteItem: function(item){
axios.post('/vueitems/'+item.id).then((response) => {
this.getVueItems();
this.hasError = false,
this.hasDeleted = true
});
},
}
});
Please or to participate in this conversation.