jQuery datatable pagination not working with mvc view - asp.net-mvc

I'm working on an MVC application. The table data is returned as a view from the controller. After binding the rows in the razor view, I'm initializing the datatable. The no. of rows returned is 50.
The issue is, the datatable displays all the 50 records and hence there is no pagination. I would like it to show only 10 records per page and then paginate.
Also, I do not want to get the data page by page. The total count, which is 50 in this case, is decided by the end user. That's the main reason, I choose datatables as it provides sorting, search & pagination with no coding.
So, does the pagination feature work if the data is bound to DOM and then initialized. If not, what is the best way to do it.
Here is the code:
#model ResultOutput
<table class="tblKeyMetrics" role="grid">
<thead>
<tr role="row">
<th class="">Value</th>
<th class="">Impressions</th>
</tr>
</thead>
#if (Model.KeyMetrics != null && Model.KeyMetrics.Count > 0)
{
<tbody>
for (int index = 0; index < Model.KeyMetrics.Count; index++)
{
KeyMetrics metric = Model.KeyMetrics[index];
<tr role="row">
<td>#metric.value</td>
<td>#metric.impressions</td>
</tr>
}
</tbody>
}
else
{
<tbody>
<tr class="row">
<td colspan="3">
No results found.
</td>
</tr>
</tbody>
}
</table>
<script>
$(document).ready(function () {
InitializeDataTable($(".tblKeyMetrics"));
});
function InitializeDataTable(tbl) {
if (!$.fn.DataTable.isDataTable($(tbl))) {
$(tbl).DataTable({
pageLength: 10,
iDisplayLength: 10,
"paging": true
});
}
}
</script>
using jquery.dataTables.min.js v1.10.16 with dataTables.bootstrap.min v3

Related

Tablesorter - How to ignore sort for certain cell value?

How can I prevent Tablesorter from sorting a certain value or keep these values at the bottom? More specifically, this certain value will always be a -
<table id="myTable" class="tablesorter">
<thead>
<tr>
<th>Name</th>
<th>Number</th>
</tr>
</thead>
<tbody>
<tr>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Bach</td>
<td>40</td>
</tr>
<tr>
<td>Doe</td>
<td>-</td>
</tr>
</tbody>
</table>
I want to keep Doe with the value "-" always at the bottom.
Tablesorter has an emptyTo option which can modify the sort behavior of empty, or non-numeric, cells (demo)
In this case, you might need to set a data-text attribute (modified by the textAttribute option to an empty string.
HTML
<td data-text="">-</td>
JS
$('#myTable').tablesorter({
// ...
emptyTo: 'bottom'
});
This worked for me.
Doc: https://mottie.github.io/tablesorter/docs/example-options-headers-digits-strings.html
jQuery(function($) {
$("#myTable").tablesorter({
stringTo: 'bottom'
});
});

Kendo grid MVC number link column sorting

I have a Kendo grid on a MVC .cshtml view page:
#model IEnumerable<Models.GetItems>
<script>
$(document).ready(function () {
$("#grid").kendoGrid({
sortable: true
});
});
</script>
<table class="table" id="grid">
<thead>
<tr>
<th data-field="Quantity">
Qty
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.ActionLink(item.Quantity.ToString(), "kendo", "Groups", new { ID = item.ID }, null)
</td>
</tr>
}
</tbody>
</table>
It displays the way I want; as a number with a link to a drill down page, but the sorting doesn't work at all.
How can I tell the grid I want the data type to be a number so it can sort it like a number?
(item.Quantity is Int16 from the model, but had to make it a string for the ActionLink to work)
(I'm open to binding the grid differently if I have to (bind to json output from controller and/or use rowTemplate and/or bind to empty div and define columns in JS possibly with template), but not sure at this point if that will matter, seems like a data type issue regardless of binding method???)

How to Hide Table in MVC View when there is No Data?

I am populating my datatable using Linq.
I have hard-coded headers. And populating body columns with Linq. Following is my code.
<table id="tableID">
<thead>
<tr>
<th>Name</th>
<th>ID</th>
<th style="width:2%;"></th>
</tr>
</thead>
<tbody>
#if(Model.Values !=null)
{
foreach(var value in Model.Values)
{
<tr>
<td>#value.Name</td>
<td>#value.ID</td>
</tr>
}
}
</tbody>
</table>
What I am thinking to do here, if there is no data table should not be visible.I thought of moving my conditional check if model is returning null prior to table creation but it will throw exception. I am fairly new to MVC. Any suggestion is appreciated.
Thank you
Simply put one if around table as well to check if property is not null and count of that list is greater than 0 then table should be rendered.
#if(Model != null)
{
if(Model.Values != null && Model.Values.Count != 0)
{
<table id="tableID">
<thead>
<tr>
<th>Name</th>
<th>ID</th>
<th style="width:2%;"></th>
</tr>
</thead>
<tbody>
#foreach(var value in Model.Values)
{
<tr>
<td>#value.Name</td>
<td>#value.ID</td>
</tr>
}
</tbody>
</table>
}
}

How do I pass an ID from View to the ViewModel as a parameter for GET function?

I'm creating a project using MVC, knockoutJS, Web API, Bootstrap and so forth, the database in use is MSSQL Server 2012. It's all working very well, the controllers have properly created CRUD operations. The data from DB is show in a grid table in the UI, and every row is clickable, and opens up a modal in which the data about that exact element is shown. The problem I'm experiencing is the inability to pass a certain value of the row, in this case an ID, to ViewModel as a parameter for getting a single result in modal. I can do it manually, and put some value in the ViewModel, and the data will show, but I'm unable to send the value from the View.
Here's the code for ViewModel:
var regionsModel = {
regionId: ko.observable(),
companyId: ko.observable(),
name: ko.observable(),
companyName: ko.observable()
};
var regionsListModel = {
regions: ko.observable()
};
function getRegions() {
get(apiUrl + "Regions/GetRegions", {}, function (data) {
regionsListModel.regions(data);
});
}
function getRegion() {
get(apiUrl + "Regions/GetRegion", { aiId: regionsModel.regionId() }, function (data) {
regionsModel.regionId(data.RegionID);
regionsModel.companyName(data.CompanyName);
regionsModel.companyId(data.CompanyID);
regionsModel.name(data.Name);
});
}
function viewRegion() {
$("#ViewRegionModal").modal('show');
//regionsModel.regionId($('#ViewRegion').val());
getRegion();
return false;
}
Here's the code for the View:
<table class="table table-striped table-bordered responsive" id="dtable">
<thead>
<tr>
<th style="width: 20px;">ID</th>
<th>Region Name</th>
<th>Company Name</th>
</tr>
</thead>
<tbody data-bind="foreach: regionsListModel.regions">
<tr id="ViewRegion" data-toggle="modal" data-bind="click: viewRegion, value: RegionID">
<td data-bind="text: RegionID"></td>
<td data-bind="text: Name"></td>
<td data-bind="text: CompanyName"></td>
</tr>
</tbody>
</table>
aiId parameter is for the GetRegion method in Controller.
This is the code for the View in which shows the data for a certain element:
<table class="table table-striped" data-bind="with: regionsModel">
<tbody>
<tr>
<th>Region ID:</th>
<td><span data-bind="text: regionsModel.regionId"></span></td>
</tr>
<tr>
<th>Region Name:</th>
<td><span data-bind="text: regionsModel.name"></span></td>
</tr>
<tr>
<th>Company Name:</th>
<td><span data-bind="text: regionsModel.companyName"></span></td>
</tr>
</tbody>
</table>
Any help would be appreciated!
Knockout adds the current bound object as first argument when it calls the event handler.
The second argument is the event object.
So the only thing you need to do is add a parameter to the viewRegion function.
function viewRegion(region) {
var regionID = region.RegionID;
// get data
return false;
}
I hope it helps.

Showing a dynamic table in GSP where column & data comes at run time

getting two arrays from controller and code is --
Sql db = new Sql(dataSource_wldb1) // Create a new instance of groovy.sql.Sql with the DB of the Grails app
def ivrColumns = []
db.eachRow(ivrColumnsQuery) {
rsRow ->
ivrColumns.add(rsRow.name) }
def ivrResults = []
db.eachRow(mssqlQuery) {rows ->
//print rows
ivrResults.add(rows)
}
one has all column names & other has all row data.as below-
return render(view:'xref',model:[ivrcolumns:ivrColumns,ivrresults:ivrResults] )
getting data in below format-
Columns
[ClientKey, Abbr, ConfigKey, Federal, State, DMA, Internal, Wireless, CRssing, CurfewExemption, CampaignID]
Data
[groovy.sql.GroovyResultSetExtension#12f8d75, groovy.sql.GroovyResultSetE
oovy.sql.GroovyResultSetExtension#12f8d75, groovy.sql.GroovyResultSetExtension#1
roovyResultSetExtension#12f8d75, groovy.sql.GroovyResultSetExtension#12f8d75, gr
tSetExtension#12f8d75, groovy.sql.GroovyResultSetExtension#12f8d75, groovy.sql.G
ion#12f8d75, groovy.sql.GroovyResultSetExtension#12f8d75]
view code is---
<g:if test="${ivrcolumns != null }">
<center>Database Location - WLDB1 <br>DB Name - IVR_GUARDIAN </center><br><br>
<table class="table loadTable" >
<thead>
<tr bgcolor="#f0f0f0" >
<g:each in="${ivrcolumns}" status="ii" var="columnivr">
<td nowrap>${columnivr}</td>
</g:each>
</tr>
</thead>
<tbody>
<g:each in="${ivrresults}" status="jj" var="hed">
<tr>
<g:each in="${ivrcolumns}" status="kk" var="col">
<td nowrap>${hed.col}</td> ///please suggest how to do it.
</g:each>
</tr>
</g:each>
</tbody>
</table>
now want to show in GSP page .i am able to display the column but having hard time to display data.not getting how the dot will be used to get correct data to each column.
Will appreciate any help.
thanks
Assuming that's just a sql result, you can just call ${ hed[ col ] } or ${ hed."$col" }

Resources