I have a Blazor project that uses Entity Framework and Binds to DataTable.js using JavaScript InterOp.
The project works well but in cases where records loaded from my table are bulky i experience delay and my app crashes at some point.
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover table-bordered"
id="example"
width="100%"
cellspacing="0">
<thead>
<tr style="font-size:14px">
<th>FEEDER ID</th>
<th>FEEDER NAME</th>
<th>FEEDER TYPE</th>
<th>DISTRICT ID</th>
<th>TCN 132Kv ID</th>
<th>INJECTION ID </th>
<th>FEEDER NO</th>
<th>FEEDER LENGTH </th>
<th>STATUS</th>
<th>FEEDER CODE</th>
<th>DOWNLOAD MAP</th>
</tr>
</thead>
<tbody>
#foreach (var data in feederview)
{
<tr style="font-size:14px">
<td>#data.FeederId</td>
<td>#data.FeederName</td>
<td>#data.FeederType</td>
<td>#data.DistrictId</td>
<td>#data.Tcn132kv33kvId</td>
<td>#data.InjectionssId</td>
<td>#data.FeederNo</td>
<td>#data.FeederLength</td>
<td>#data.Status</td>
<td>#data.FeederCode</td>
<td align="center">
<button class="btn-info btn-sm" id="btnDelete" value="delete"><i class="fa fa-eye fa-1x " aria-hidden="true"></i></button>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
Above is my HTML Code definition with bindings
C# Blazor Server side code using Entity Framework
feederview = context.FeederView.Select(c => new FeederView()
{
FeederId = c.FeederId,
FeederName = c.FeederName,
FeederType = c.FeederType,
DistrictId = c.DistrictId,
Tcn132kv33kvId = c.Tcn132kv33kvId,
Tcn132kv33kvTransformerId = c.Tcn132kv33kvTransformerId,
InjectionssId = c.InjectionssId,
InjectionssTransformerId = c.InjectionssTransformerId,
FeederNo = c.FeederNo,
FeederLength = c.FeederLength,
Status = c.Status,
FeederCode = c.FeederCode,
}).ToList();
The list returns up to 2000 records which makes my app slow and crashes.
Above is the sample records shown ( for this i just loaded small records)
How can i add pagination to Datatable.js to allow me retrieve a particular amount of records on load and the remaining loads later?
I think you would need to look at server-side processing https://datatables.net/manual/data/
Otherwise, perhaps there is a way of utilising the new virtualization feature.
https://learn.microsoft.com/en-us/aspnet/core/blazor/components/virtualization?view=aspnetcore-5.0
Related
So I have created an HTML file and it has a table with several columns in it. What I need to do is to find a way to display data from my SQL database into this HTML table. I have created a connection with the database using Node, but I have no idea how to display the data in the table. I have been looking everywhere but all I can find is PHP code, and I do not want to use it. I need it to be in Node. How should I start this?
Thank you all.
Here is what I have so far regarding the HTML:
<div id="table">
<table align="right" cellspacing="0" cellpadding="5">
<tr>
<th>Match </th>
<th>Time</th>
<th>Red 1</th>
<th>S</th>
<th>Red 2</th>
<th>S</th>
<th>Red 3</th>
<th>S</th>
<th>Blu 1</th>
<th>S</th>
<th>Blu 2</th>
<th>S</th>
<th>Blu 3</th>
<th>S</th>
<th>Red Sc</th>
<th>Blu Sc</th>
</tr>
</table>
</div>
Regarding the Javascript file, I have created a connection with the database from Azure and a SELECT query for retrieving data from it. Now, I just need to put the data that I have inside the HTML table above.
Just an example, you'll get the idea.
Nodejs:
var express = require('express')
var app = express()
app.get('/getData', function(req, res) {
... // Get data from database
})
html:
<table>
<thead>
<th>id</th>
<th>name</th>
</thead>
<tbody id="place-here"></tbody>
</table>
<script>
$.get('/getData', {}, function(result) {
let res = '';
result.forEach(data => {
res += `<tr><td>${data.id}</td><td>${data.something}</td></tr>`
})
$('#place-here').html(res)
});
</script>
I'm working on an app where I need a calendar skeleton (without the standard events) so I can put tables inside each cell, so I'm using the Angular Bootstrap Calendar with custom cell templates. I have everything working fine in terms of displaying the custom template in each cell and being able to navigate between months, but I need to be able to access each individual day and make data available in each one.
Here's my controller:
(function() {
angular.module('myApp')
.controller('calendarController', function($scope, $state, moment, calendarConfig) {
var vm = this;
calendarConfig.templates.calendarMonthCell = 'views/calendar/dayTemplate.html';
calendarConfig.dateFormatter = 'moment';
vm.events = [];
vm.calendarView = 'month';
vm.viewDate = moment().startOf('month').toDate();
$scope.$on('$destroy', function() {
calendarConfig.templates.calendarMonthCell = 'mwl/calendarMonthCell.html';
});
});
})();
and the corresponding dayTemplate.html:
<div class="cal-month-day">
<span
class="pull-right"
data-cal-date
ng-click="calendarCtrl.dateClicked(day.date)"
ng-bind="day.label">
</span>
<!--
<small style="position: absolute; bottom: 10px; left: 5px">
There are {{ day.events.length }} events on this day
</small> -->
<!-- <table class="table table-bordered table-condensed"> -->
<table class="table table-bordered table-condensed">
<thead>
<tr>
<td>Station</td>
<td>Position</td>
<td>Name</td>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="3" align="top">1</td>
<td>Position</td>
<td>Name</td>
</tr>
<tr>
<td>Position</td>
<td>Name</td>
</tr>
<tr>
<td>Position</td>
<td>Name</td>
</tr>
</tbody>
</table>
<table class="table table-bordered table-condensed">
<tbody>
<tr>
<td rowspan="3" align="top">2</td>
<td>Position</td>
<td>Name</td>
</tr>
<tr>
<td>Position</td>
<td>Name</td>
</tr>
<tr>
<td>Position</td>
<td>Name</td>
</tr>
</tbody>
</table>
<table class="table table-bordered table-condensed">
<tbody>
<tr>
<td rowspan="3" align="top">3</td>
<td>Position</td>
<td>Name</td>
</tr>
<tr>
<td>Position</td>
<td>Name</td>
</tr>
<tr>
<td>Position</td>
<td>Name</td>
</tr>
</tbody>
</table>
</div>
When using the calendar as it normally is used, you can see that the days.events object has the data, but I need to access that object, or create my own so I can fill my tables. Is there a simple (or even not so simple) way to do that?
Thanks.
UPDATE: I just went back and read the docs and noticed this
An optional expression that is evaluated on each cell generated for
the year and month views. calendarCell can be used in the expression
and is an object containing the current cell data which you can modify
(see the calendarHelper service source code or just console.log it to
see what data is available). If you add the cssClass property it will
be applied to the cell.
Due to my lack of knowledge, I'm not understanding how to use this to override. If I console.log calendarCell in my calendarController it chokes because that object doesn't exist. If I'm reading this correctly, I can intercept the cell data and modify, but I'm not understanding how.
In this case, RTFM turned out to be the correct answer. Per the docs:
<div ng-controller="CellModifierCtrl as vm">
<ng-include src="'calendarControls.html'"></ng-include>
<mwl-calendar
events="vm.events"
view="vm.calendarView"
view-date="vm.viewDate"
cell-modifier="vm.cellModifier(calendarCell)">
</mwl-calendar>
</div>
goes with this in the controller:
vm.cellModifier = function(cell) {
console.log(cell);
if (cell.label % 2 === 1 && cell.inMonth) {
cell.cssClass = 'odd-cell';
}
cell.label = '-' + cell.label + '-';
};
and voila!, you have access to the data. I'm still trying to figure out how to pass additional data into the function, but I'll open a separate question for that.
I am binding data to a table using Knockout JS and the JQuery/Bootstrap based; Data Table API. The table becomes unresponsive sporadically when sorted or loaded. There are no errors in the log.
It also shows 0 of 0 data as illustrated in the screenshot below:
I have seen similar errors/issues but could not get a solutions for them, E.g. This post:
Code:
function viewModel(){
var self = this;
self.Data = ko.observableArray([]);
$.getJSON('https://restcountries.eu/rest/v1/all', function(data){
self.Data(data);
});
}
ko.applyBindings(viewModel());
$(document).ready(function() {
$('#example').dataTable();
});
HTML:
<div class="table-responsive">
<table id="example" cellspacing="0"
class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th scope="col">Country</th>
<th scope="col">Capital</th>
<th scope="col">Population</th>
<th scope="col">Region</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="col">Country</th>
<th scope="col">Capital</th>
<th scope="col">Population</th>
<th scope="col">Region</th>
</tr>
</tfoot>
<tbody data-bind="foreach: Data">
<tr>
<td data-bind="text: name"></td>
<td data-bind="text: capital"></td>
<td data-bind="text: population"></td>
<td data-bind="text: region"></td>
</tr>
</tbody>
</table>
</div>
Here is a full working example (JSFiddle) utilizing a REST API so that the exact problem is accurately replicated:
I think the problem with your example may be with how you're dealing with your data when you get it back from the API call.
I've put together a quick example that achieves what I think you're trying to achieve and the sorting and searching work quickly for me.
When I get the JSON data back from the API, I use the Knockout arrayMap utility function to create an array of "Country" objects that have observable properties that I have mapped the JSON data to. I've bound the table to my observableArray of Country objects.
Initialising the data table in the same way you have works fine for me in this case.
The full working solution is here: http://plnkr.co/edit/eroIox6zqBFOVnf86Mdk?p=preview
script.js
var ViewModel = function(jsonData) {
var countries = ko.utils.arrayMap(jsonData, function(item) {
return new Country(item)
});
this.Countries = ko.observableArray(countries);
};
var Country = function(jsonItem) {
this.Name = ko.observable(jsonItem.name);
this.Capital = ko.observable(jsonItem.capital);
this.Population = ko.observable(jsonItem.population);
this.Region = ko.observable(jsonItem.region);
};
window.onload = function() {
$.getJSON('https://restcountries.eu/rest/v1/all', function(data) {
ko.applyBindings(new ViewModel(data));
$("#example").dataTable();
});
}
index.html
<table id="example" cellspacing="0" class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th scope="col">Country</th>
<th scope="col">Capital</th>
<th scope="col">Population</th>
<th scope="col">Region</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="col">Country</th>
<th scope="col">Capital</th>
<th scope="col">Population</th>
<th scope="col">Region</th>
</tr>
</tfoot>
<tbody data-bind="foreach: Countries">
<tr>
<td data-bind="text: Name"></td>
<td data-bind="text: Capital"></td>
<td data-bind="text: Population"></td>
<td data-bind="text: Region"></td>
</tr>
</tbody>
</table>
Tables are fairly slow to render within Knockout, and if your table is based on a computed, I could see how you could have some issues with redrawing time. But that's not happening here.
Apart from loading the data and binding it to the table rows, there's no data manipulation going on in your viewmodel. All the data manipulation is done by the dataTable plug-in, which you initialize with a single jQuery call. Properly, that should be done within a binding handler. You also need to know what is going on within the plug-in when it sorts, filters, or whatever it does, because you may need to mediate those changes back to your observableArray within your binding handler.
Bottom line: you need a binding handler for the dataTable. There may be one already written; I haven't Googled for it. Give that a try.
As you can see the Datatools [Copy, CSV, Excel, PDF, Print] tools is displayed that way that it forces the Search Filter to go to the middle.
I believe my code is correct I am inserting that tool before the dataTables_wrapper which is existing. The Datatools should go to the top and Search Filter should be aligned horizontally with the Show Entries Tool.
var t = $('#formList').DataTable({ "order": [[ 1, "desc" ]] });
var tt = new $.fn.dataTable.TableTools(t);
$( tt.fnContainer()).insertBefore('div.dataTables_wrapper');
My HTML
<table id="formList" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th colspan="6"> <center>Form Information<center></th>
</tr>
<tr>
<th>ID</th>
<th>Form #</th>
<th>Date</th>
<th>Plan Holder</th>
<th>Telephone</th>
<th>Status</th>
</tr>
</thead>
</table>
The Datatables Tool should align like this:
I am using fooTable jQuery plugin as date grid in jQuery mobile. I have list of data I need to populate in the table. Initially with filtering and cell formatting. First I want to populate data in the table. I am using an external .js file to keep JavaScript separately.
HTML5 code, "$.App.initGridView" is not calling from "data-init" tag. Why isn't it working?
<div data-role="view" data-title="Platform" onloadeddata="$.App.initGridView" data-init="$.App.initGridView">
<table class="footable table" id="my-table">
<thead >
<tr data-theme="b">
<th >Ticket#</th>
<th >Date</th>
<th data-hide="phone">Operator Name</th>
<th data-hide="phone">Lease Name</abbr></th>
<th >Tank#</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
jQuery Code:
initGridView: function () {
var rtList = $.App.tempDB.getTicketList();
// I want to populate table here
}