Vuejs pulling database from Sqlite3 over axios; DOM not re-rendering - sqlite

So I am working on a project including Flask, VueJS, Sqlite3 and Axios. On the VueJS side, using a 'GET' request I am getting all the database data and saving it in a JavaScript array. I am trying to iterate through the array and show all the database information in HTML. If I 'refresh' the page the information is displayed. But I would like the information to update 'live' when I add information to the database from a 'POST' axios request to FLASK.
I just put the critical code down below; I tried to make it shorter.
So the axios 'GET' request fetches the entire sqlite3 database; then saves it in the 'database[]' array
The database has three columns; id, FIRST_NAME, LAST_NAME
#app.route('/api/database', methods = ['POST'])
def database_write():
if request.method == 'POST':
conn = sql.connect('test.sqlite3')
conn.execute("INSERT INTO DATABASE (FIRST_NAME,LAST_NAME) \
VALUES (?, ?)", (request.json[0], request.json[1]))
conn.commit()
print("Records created successfully")
conn.close()
return("OK")
#app.route('/api/getdatabase', methods = ['GET'])
def database_get():
if request.method == 'GET':
conn = sql.connect('test.sqlite3')
cursor = conn.cursor()
cursor.execute("SELECT * FROM DATABASE ORDER By id")
rows = cursor.fetchall()
return jsonify(rows)
<template>
<div class="form-group col-6">
<input class="form-control" v-model="form1" type="text">
<input class="form-control" v-model="form2" type="text">
<button class="btn btn-primary" type="submit" #click='writeToDatabase'>Submit</button>
</div>
<table class="table table-hover">
<thead>
<tr>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
<tr v-for="database in database" :key="database[0]">
<td>{{ database[1] }}</td>
<td>{{ database[2] }}</td>
<td>
<div class="btn-group" role="group">
<button type="button" class="btn btn-danger btn-sm" #click="deleteDatabaseValue(database)">Delete</button>
</div>
</td>
</tr>
</tbody>
</table>
</template>
<script>
data () {
return {
form1: "",
form2: "",
database: [],
}
},
writeToDatabase () {
const path = `http://localhost:5000/api/database`
axios.post(path, [this.form1, this.form2])
this.form1 = ""
this.form2 = ""
},
getDatabase() {
const path = 'http://localhost:5000/api/getdatabase'
axios.get(path)
.then((response) => {
// console.log(res.data)
this.database = response.data;
})
.catch((error) => {
// eslint-disable-next-line
console.error(error);
});
},
created() {
this.getDatabase();
},
</script>
I want the v-for to render anytime I add a value to the database or delete a value from the database. It only rerenders when I refresh the page manually and the 'created()' function is called which resets the database.

Related

How to change the data-source for a table on dropdown-list change

I just can't find any examples that can help me. I have a drop-down list of customers and a table which displays services/products a customer provides. On page load the ddl is set to customer 0 and that customer's data is displayed in the table. I now need to changed the data-source and refresh the table when the drop-down is changed.
The main bits of my code are...
<div class="card-body">
<div class="row">
<div class="col-4">
<lable for="customerFilter" class="control-label">Customer Filter: </lable>
</div>
<div class="col-8">
<input id="customerFilter" class="form-control" />
</div>
</div>
<div id="toolbar">
<div class="alert alert-info">You can refine this list by entering an additional filter in the search box on the right. Any text you type will filter the list based on any of the fields containing the text typed.</div>
</div>
<div>
<table id="table"
data-classes="table table-hover table-condensed"
data-striped="true"
data-toolbar="#toolbar"
data-pagination="true"
data-click-to-select="true"
data-search="true"
data-show-export="true"
data-show-pagination-switch="true"
data-show-toggle="true"
data-show-columns="true"
data-url='#Url.Content("~/SSTCodes/GetSSTCodesByCustomer?CustomerID=0")'>
<thead>
<tr>
<th data-field="sstID" data-formatter="btnViewFormatter" data-sortable="true">ID</th>
<th data-field="sstRU" data-sortable="true" data-formatter="sstFormatter">Recoverability</th>
<th data-field="sstProductCode" data-sortable="true">Product Code</th>
<th data-field="sstProductName" data-sortable="true">Product Name</th>
<th data-field="sstStockLevel" data-formatter="lowStockFormatter">Stock Level</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
<script type="text/javascript">
cData = #Html.Raw(ViewData("Customers"));
$("#customerFilter").kendoComboBox({
dataTextField: "Text",
dataValueField: "Value",
change: customerFilterChanged,
dataSource: cData,
filter: 'contains'
});
function customerFilterChanged() {
// NEED TO CHANGE THE DATASOURCE Url
var customer = this.value()
var url = '#Url.Content("~/SSTCodes/GetSSTCodesByCustomer?CustomerID=")' + customer;
// NEED TO SET THIS AS THE TABLES data-url AND REFRESH THE DATASOURCE....
}
var $table = $('#table');
function btnViewFormatter(value) {
return '<a class="btn btn-primary btn-sm" href="#Url.Content("~/SSTCodes/Edit?id=")' + value + '">' + value + '</a>';
}
function sstFormatter(value) {
//Removed for clarity
Return value
}
function lowStockFormatter(value) {
//Removed for clarity
Return value
}
function getSelectedRow() {
var index = $table.find('tr.success').data('index');
return $table.bootstrapTable('getData')[index];
}
$(function () {
$table.bootstrapTable({
fixedColumns: true,
fixedNumber: 1,
exportDataType:"all",
exportTypes:['csv', 'txt', 'xlsx']
});
$table.on('click-row.bs.table', function (e, row, $element) {
$('.success').removeClass('success');
$($element).addClass('success');
});
$table.on('dbl-click-row.bs.table', function (e, row, $element) {
var url = '#Url.Content("~/SSTCodes/Edit?id=")' + getSelectedRow().sstID;
window.open(url, '_self');
})
});
</script>
The function customerFilterChanged is where I need help.
I have changed my approach...
The customerFilterChange function now just redirects the page
function customerFilterChanged() {
window.location.href = "#Url.Content("~/SSTCodes/Index?id=")" + this.value();
}
The controller has been amended so that it has an optional id...
Function Index(Optional id As Integer = 0) As ActionResult
If IsNothing(id) Then
ViewData("CustomerID") = 0
Else
ViewData("CustomerID") = id
End If
ViewData("Customers") = Newtonsoft.Json.JsonConvert.SerializeObject(CustomerModel.CustomerDDLList.GetAllCustomers())
Return View()
End Function
And the data-url value for the table is now
data-url='#Url.Content("~/SSTCodes/GetSSTCodesByCustomer?CustomerID=") + #ViewData("CustomerID")' >
Job done...

Vue JS unable to properly show data table (v-for)

i'm trying to show a table of data that is coming from Firebase Firestore, i've already managed to put all data into a array, but when i try to show that content, the entire array shows up instead of the single items, see the image:
And heres my code:
<template>
<v-simple-table>
<template v-slot:default>
<thead>
<tr>
<th class="text-left">
Name
</th>
<th class="text-left">
Calories
</th>
</tr>
</thead>
<tbody>
<tr
v-for="(item, index) in result"
v-bind:key="index"
>
<td>{{ result }}</td>
</tr>
</tbody>
</template>
</v-simple-table>
</template>
<script>
import firebase from 'firebase'
import {db} from '../service/firebase'
export default {
data () {
return {
userName: null,
result: [],
name: null,
email: null,
userMail: null,
telefone: null,
userPhone: null,
userAccept: null,
}
},
async created() {
var userData = []
await db.collection("users").get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(`${doc.id} => ${doc.data()}`);
userData.push(doc.data())
});
});
this.result = userData.map(a => a.name)
console.log(result)
}
}
</script>
How do i show a table of single items of the array instead of a table of arrays?
Thank you in advance!
You're printing the whole content using {{result}}, the right syntax is the following :
<tr v-for="(item, index) in result" v-bind:key="index" >
<td v-for="(record,i) in item" :key="i">{{ record }}</td>
</tr>

Vue: [Vue warn]: Error in render: "TypeError: product.data is not a function"

I am trying to retrieve the data from cloud firestore database.
But I got an error,
[Vue warn]: Error in render: "TypeError: product.data is not a
function"
I want to show the each product name and price in my table.
But I have no idea why this issue comes up.
So I hope somebody can help me out.
If I don't use data() in the vue template, I can see all the data as I expected.
<template>
<h3>Product List</h3>
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>Modify</th>
</tr>
</thead>
<tbody>
<tr v-for="(product, index) in products" :key="index">
<td>{{ product.data().name }}</td>
<td>{{ product.data().price }}</td>
<td>
<button #click="editProduct(product)" class="btn btn-primary">Edit</button>
<button #click="deleteProduct(product.id)" class="btn btn-danger">Delete</button>
</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import { fb, db } from '../firebase'
export default {
name: 'Products',
props: {
msg: String
},
data () {
return {
products: [],
product: {//object
name: null,
price: null
}
}
},
methods: {
editProduct(product) {
$('#edit').modal('show')
},
readData() {
db.collection("products").get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
this.products.push(doc.data());
});
});
},
saveData() {
// Add a new document with a generated id.
db.collection("products").add(this.product)
.then((docRef) => {
console.log("Document written with ID: ", docRef.id);
this.product.name = "",
this.product.price = ""
this.readData()
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
}
},
created() {
this.readData();
}
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
</style>
I will have to agree with #Mostafa, the naming convention is not very readable. Your error is telling you that you are trying to invoke a function that is not a function or does not exist in your data.
Change:
<td>{{ product.data().name }}</td>
<td>{{ product.data().price }}</td>
To:
<td>{{ product.name }}</td>
<td>{{ product.price }}</td>
This should fix it, as you are iterating over the products list (of which isn't clear), so i advise you should change:
<tr v-for="(product, index) in products" :key="index">
<td>{{ product.name }}</td>
<td>{{ product.price }}</td>
<td>
<button #click="editProduct(product)" class="btn btn-primary">Edit</button>
<button #click="deleteProduct(product.id)" class="btn btn-danger">Delete</button>
To:
<tr v-for="(productItem, index) in products" :key="index">
<td>{{ productItem.name }}</td>
<td>{{ productItem.price }}</td>
<td>
<button #click="editProduct(productItem)" class="btn btn-primary">Edit</button>
<button #click="deleteProduct(productItem.id)" class="btn btn-danger">Delete</button>
Your code is very confusing.
I don't understand why you are calling data method on product and why you have product and products in your data when you just need one.
So i'm assuming Vue is mixing product in your for loop and the product object in your data.
So either change the product name in your for loop to something else:
v-for="(item,index) in products"
or change product in your data (just remove it if you can) cause it doesn't have any data method in it.

How to repeat bound controls based on clicking Add new in Asp.net mvc and Knockoutjs

I want to repeat a dropdownlist that is already bound using a Viewbag property and another textbox when a user click on Add Course.
I have used asp.net mvc and knockout.js based on a tutorial i saw, but the tutorial does not exactly handle using bound controls, please how can i achieve this using asp.net mvc and knockout.js.
Below is my code.
Thanks
<table id="jobsplits">
<thead>
<tr>
<th>#Html.DisplayNameFor(m => m.selectedCourse.FirstOrDefault().FK_CourseId)</th>
<th>#Html.DisplayNameFor(m => m.selectedCourse.FirstOrDefault().CourseUnit)</th>
<th></th>
</tr>
</thead>
<tbody data-bind="foreach: courses">
#for (int i = 0; i < Model.selectedCourse.Count; i++)
{
<tr>
<td>
#Html.DropDownListFor(model => model.selectedCourse[i].FK_CourseId, new SelectList(ViewBag.Course, "Value", "Text", Model.FK_CourseId), "Select Course", new { #class = "form-control", data_bind = "value: courses" })
</td>
<td>
#Html.TextBoxFor(model => model.selectedCourse[i].CourseUnit, new { htmlAttributes = new { #class = "form-control", #readonly = "readonly", data_bind = "value: courseUnit" } })
</td>
<td>
<button type="button" data-bind="click: $root.removeCourse" class="btn delete">Delete</button>
</td>
</tr>
}
</tbody>
</table>
<div class="col-md-4">
<button data-bind="click: addCourse" type="button" class="btn">Add Course</button>
</div>
This is the script section
#section Scripts{
#Scripts.Render("~/bundles/knockout")
<script>
function CourseAdd(course, courseUnit) {
var self = this;
self.course = course;
self.courseUnit = courseUnit;
}
function CourseRegViewModel() {
var self = this;
self.addCourse = function () {
self.courses.push(new CourseAdd(self.course, self.courseUnit));
}
self.courses = ko.observableArray([
new CourseAdd(self.course, self.courseUnit)
]);
self.removeCourse = function (course) {
self.courses.remove(course)
}
}
ko.applyBindings(new CourseRegViewModel());
</script>
}
Edit:
i have been able to get this sample working from: http://learn.knockoutjs.com/#/?tutorial=collections
but it is only an hard-coded observableArray.
I want to be able to populate the select from the database. But it is not getting populated.
This is my sample code below:
<table id="jobsplits">
<thead>
<tr>
<th>Persenger Name</th>
<th>Meal</th>
<th></th>
</tr>
</thead>
<tbody data-bind="foreach: seats">
<tr>
<td>
<input data-bind="value: name" />
</td>
<td>
<select data-bind="options:coursesArray, optionsText:'Text', optionsValue:'Value', optionsCaption: 'Choose...'"></select>
</td>
<td>
<button type="button" data-bind="click: $root.removeSeat" class="btn delete">Delete</button>
</td>
</tr>
</tbody>
</table>
<div class="col-md-4">
<button data-bind="click: addSeat">Add Seat</button>
</div>
This is the adjusted script section:
<script>
function SeatReservation(name, initialMeal) {
var self = this;
self.name = name;
self.meal = ko.observable(initialMeal);
}
function ReservationsViewModel() {
var self = this;
//This is what i want to put in dropdown instead
self.thecourses.subscribe(function () {
getCourses();
});
// Editable data
self.seats = ko.observableArray([
new SeatReservation("Steve", self.thecourses),
new SeatReservation("Bert", self.thecourses)
]);
self.addSeat = function () {
self.seats.push(new SeatReservation("", self.availableMeals[0]));
}
self.removeSeat = function (seat) { self.seats.remove(seat) }
var getCourses = function () {
var collegeCode = $("#Colleges").val();
var departmentCode = $("#Departments").val();
var url = '#Url.Action("GetCourses", "Student")';
$.getJSON(url, { deptId: departmentCode, collegeId: collegeCode }, function (data) {
self.coursesArray(data)
});
}
}
ko.applyBindings(new ReservationsViewModel());
</script>

get table column value of selected row in table asp.net in jQuery

I am working on asp.net mvc project.
I want to get the cell value from the selected row (row in which "Manage "button is clicked).
in this case value of userID.
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th width="45%">User ID</th>
<th width="45%">User Name</th>
<th width="5%">View</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.TypeList)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.UserId)
</td>
<td>
#Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
<input id="Manage2" class="btn btn-primary" type="button" value="Manage" />
</td>
</tr>
}
</tbody>
I am calling jQuery click function and Ajax call and want to send the UserId value in data from selected row to the controller.
below is the jQuery ajax call,
<script type="text/javascript">
$(document).ready(function () {
$('#Manage2').click(function () {
//alert(1);
var url = '#Url.Action("ManageUserRole", "UserRoleCompany")';
$.ajax({
url: url,
data: { Id: '1' },
cache: false,
type: "POST",
success: function (data) {
$("#Data").html(data);
},
error: function (reponse) {
alert("error : " + reponse);
}
});
});
});
</script>
Below is the view screen shot,
You can actually store the UserId value with html 5 data attribute on the button itself.
Your current code has Id attribute value hard coded inside a loop, which will create more than one element with same Id value. That is invalid HTML!
Remove the Id and use a more generic selector to wire up the click event. Here I added another CSS class "manage" to the button.
<input data-userid="#item.UserId" class="btn btn-primary manage"
type="button" value="Manage" />
and now
$(document).ready(function () {
$('.manage').click(function () {
var id = $(this).data("userid");
alert(id);
// use id for your ajax call
});
});
You do not necessarily need to add the extra css class only to use that as the selector later. Since you are adding a data attribute, you may simply use that as well for your jQuery selector.
$(function () {
$('[data-userid]').click(function () {
var id = $(this).data("userid");
alert(id);
// use id
});
});
You have invalid html because of the duplicate id attributes in your button.
Remove the id="Manage2" and replace with a class name and data- attribute for the value
<input data-id="#item.UserId" class="btn btn-primary Manage2" type="button" value="Manage" />
Then in the script, get the value using
$('.Manage2').click(function () { // class selector
var id = $(this).data('id);
var url = '#Url.Action("ManageUserRole", "UserRoleCompany")';
$.ajax({
url: url,
data: { Id: id },
....
Alternatively you could use relative selectors
$('.Manage2').click(function () {
var id = $(this).closest('tr').children('td:first').text();
....

Resources