Using jQuery DataTables plug-in in MVC3 with jQuery Ajax - asp.net-mvc

I'm trying to use the DataTables jQuery plug-in in MVC3 using an ajax request as the data source for the table.
I have a View called "Search" which contains a search form...under the form, I have the following html...
<table id="caseTable" class="clear full-width dataTable">
<thead>
<tr>
<th>Case Name</th>
<th>Court</th>
<th>Case Number</th>
<th>Case Filed Date</th>
<th>Chapter</th>
<th>Last Researched</th>
<th>Disposition</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
When the search form is submitted, an ajax call is sent off the get the tr elements for each record to be displayed in the table.
$.ajax({
url: "/Home/SearchForCases",
type: "POST",
data: {
CaseNumber: caseNumber,
DebtorLastName: lastName,
Last4SSN: last4SSN,
FullSSN: fullSSN,
StartDate: $("#StartDate").val(),
EndDate: $("#EndDate").val(),
SelectedCourtDistrictId: $("#SelectedCourtDistrictId").val(),
SelectedChapterId: $("#SelectedChapterId").val()
},
success: function (result) {
$("#caseTable > tbody").html(result);
$("#caseTable").dataTable({
"bDestroy": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"asStripClasses": ["white-highlight"],
"bAutoWidth": false
});
$("#caseTable > tbody > tr").dblclick(function () {
$(this).removeClass("white-highlight").addClass("yellow-highlight");
});
},
complete: function () {
$("#ajaxProgress").dialog('close');
$("#searchResults").show();
}
});
Everything loads up fine on the first call, but on subsequent calls, the data returned from the ajax call is being appended vs replaced. I have checked the html being returned from the ajax call and it's always returning one row of data, <tr><td></td></tr> only.

Add the bDestroy option to remove the old datatable formatting first. You could also try removing the bRetrieve option.
success: function (result) {
$("#caseTable > tbody").html(result);
$("#caseTable").dataTable({
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"asStripClasses": ["white-highlight"],
"bDestroy": true
});
Edit
If possible, it might be better to change your code so you can use the built in datatable ajax functionality by passing in the sAjaxSource option when creating the datatable. This would require you to change your server side code to return json of data instead of html.
If that isn't possible, then you should use fnClearTable to clear the existing data before adding the new data.
var dataTable;
$.ajax({
...
success: function (result) {
// If it has been created, clear the existing data in the datatable
if(dataTable != null) {
dataTable.fnClearTable();
}
// Add your new data
$("#caseTable > tbody").html(result);
// Create / recreate the datatable
dataTable = $("#caseTable").dataTable({
"bDestroy": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"asStripClasses": ["white-highlight"],
"bAutoWidth": false
});
$("#caseTable > tbody > tr").dblclick(function () {
$(this).removeClass("white-highlight").addClass("yellow-highlight");
});
},
...
});

try initializing your datatable outside of the success callback and then just call the fnDraw() function to redraw the table after you have updated the html of the table.
EDIT
var dataTable = $("#caseTable").dataTable({
"bJQueryUI": true,
"bRetrieve": true,
"bServerSide": true,
"sAjaxSource": //Url to hit to query your database and return table data
"sPaginationType": "full_numbers",
"asStripClasses": ["white-highlight"]
});
$("#caseTable tbody tr").dblclick(function () {
$(this).removeClass("white-highlight").addClass("yellow-highlight");
});
$.ajax({
url: "/Home/SearchForCases",
type: "POST",
data: {
CaseNumber: caseNumber,
DebtorLastName: lastName,
Last4SSN: last4SSN,
FullSSN: fullSSN,
StartDate: $("#StartDate").val(),
EndDate: $("#EndDate").val(),
SelectedCourtDistrictId: $("#SelectedCourtDistrictId").val(),
SelectedChapterId: $("#SelectedChapterId").val()
},
success: function () {
dataTable.fnDraw();
},
complete: function () {
$("#ajaxProgress").dialog('close');
$("#searchResults").show();
}
});

Hey sorry for the poor formatting but just add this condition in your code. This will ensure that of your table already exists and you are just going from one page to another then your data will be replaced and not appended. Hope this helps
if(typeof datataTable !="undefined" && datatable != null)
{
$.ajax({
success: function (result) {
// If it has been created, clear the existing data in the datatable
if(dataTable != null) {
dataTable.fnClearTable();
}
// Add your new data
$("#caseTable > tbody").html(result);
// Create / recreate the datatable
dataTable = $("#caseTable").dataTable({
"bDestroy": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"asStripClasses": ["white-highlight"],
"bAutoWidth": false
});
$("#caseTable > tbody > tr").dblclick(function () {
$(this).removeClass("white-highlight").addClass("yellow-highlight");
});
},
});} else{dataTable.fnClearTable(0);dataTable.fnDraw();}

Related

set value to select2 which is retrive from database in ASP.NET MVC

i am using select2 control and i want to display or set value which i retrive from database
how to set value to select2 which is retrive from database in ASP.NET MVC
i am using select2 control and i want to display or set value which i retrive from database
how to set value to select2 which is retrive from database in ASP.NET MVC
<script type="text/javascript">
$(document).ready(function () {
var selectedValue1 = '43,48,47,57';
$('#ddlCity1').select2({
minimumInputLength: 0, //for listing all records > set 0
maximumInputLength: 20, //only allow terms up to 20 characters long
multiple: true,
placeholder: "Select",
allowClear: true,
tags: false, //prevent free text entry
width: "100%",
ajax: {
dataType: 'json',
delay: 250,
url: '#Url.Action("GetCityList", "DemoMVC")',
data: function (params) {
return {
searchTerm: params.term
};
},
processResults: function (data) {
var newData = [];
$.each(data, function (index, item) {
newData.push({
//id part present in data
id: item.Id,
//string to be displayed
text: item.City
});
});
return { results: newData };
},
cache: true
},
});
$(".city1").on("select2:select", function (e) {
currentSelectedVal = $(e.currentTarget).val();
console.log(currentSelectedVal) // get selected values
});
$('.city1').on('select2:unselect', function (e) {
currentSelectedVal = $(e.currentTarget).val();
console.log(currentSelectedVal) // get selected values
});
});
</script>
//Dropdown with select2 control
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<label>City</label>
<select id="ddlCity1" class="city1" style="width:500px"></select>
</div>
</div>
</div>

JqGrid searchoptions with select2 existing value

I'm trying to integrate select2 for JqGrid filter form. I'm using JqGrid min 4.6 & Select2 min 4.0.1. The filter works fine but I'm unable to retrieve the value that has been set through select2 once the filter form is closed and reopened. i.e. dataInit e1 does not return the existing value of the select input. I must be doing something wrong?
JqGrid Column Model:
{
name: 'CurrencyID', hidden: true, search: true, stype: 'select', searchtype: 'number', searchoptions: {
searchhidden: true,
sopt: ['eq', 'ne'],
dataInit: function (el) {
intiGridFilterSelecr2Field(el, paramFromView.CurrencyOptions);
}
},
searchrules: { required: true }
},
Parameters:
#section scripts{
<script>
var paramFromView = {
CurrencyOptions: {
searchURL: '#Url.Action("GetCurrency", "Controller")',
detailURL: '#Url.Action("CurrencyDetailsJson", "Controller")',
idField: 'CurrencyID',
txtField: 'Description'
}
};
</script>
}
Select2 Helper:
function intiGridFilterSelecr2Field(element, options) {
var comboPageSize = 15;
var quietMillis = 200;
var placeHolderText = 'Choose...'
var defaults = {
searchURL: '',
detailURL: '',
idField: '',
txtField: ''
};
var options = $.extend({}, defaults, options);
var select2Element = $(element);
select2Element.select2({
width: 'element',
minimumInputLength: 1,
placeholder: placeHolderText,
ajax: {
url: options.searchURL,
dataType: 'json',
quietMillis: quietMillis,
cache: false,
data: function (params) {
return {
name: params.term,
page: params.page,
pageSize: comboPageSize
};
},
processResults: function (data) {
var more = (data.page * comboPageSize) < data.total;
var resultsArr = [];
for (var i = 0; i < data.result.length; i++) {
resultsArr.push({ id: data.result[i][options.idField], text: data.result[i][options.txtField] });
}
return { results: resultsArr, more: more };
}
},
}).each(function (index, element) {
var idCombo = $(this);
// The problem is that idCombo.val() is always empty.
// element:select2-hidden-accessible
if (idCombo.val() != null && idCombo.val().length > 0) {
$.ajax(options.detailURL, {
data: {
id: idCombo.val()
},
dataType: 'json',
cache: false
}).done(function (data) {
var optselected = select2Element.find('option').filter(function () { return this.value == data[idField] && this.text == data[txtField] && this.selected })
if (optselected == undefined || optselected.length == 0) {
var $optionContact = $("<option selected></option>").val(data[idField].toString()).text(data[txtField]);
var toBeRemoved = select2Element.find('option').filter(function () { return this.value == data[idField] });
if (toBeRemoved != undefined) {
toBeRemoved.remove();
}
select2Element.append($optionContact).trigger('change.select2');
}
});
}
});
}
When the filter is being set...
When Loading the existing filter. How do I pass this CurrencyID = 1 to select2 helper?
Update:
With Oleg's answer, I updated my code as below.
{
name: 'CurrencyID', hidden: true, searchtype: 'number', search: true,
stype: "select", searchoptions: {
searchhidden: true,
sopt: ["eq", "ne"],
dataUrl: paramFromView.CurrencyOptions.searchURL,
buildSelect: function (data) {
var obj = jQuery.parseJSON(data);
var i, options = [];
for (i = 0; i < obj.result.length; i++) {
options.push("<option value='" + obj.result[i][paramFromView.CurrencyOptions.idField] + "'>" +
obj.result[i][paramFromView.CurrencyOptions.txtField] + "</option>");
}
return "<select>" + options.join("") + "</select>";
},
noFilterText: "Any",
selectFilled: function (options) {
setTimeout(function () {
$(options.elem).select2({
width: 'element',
});
}, 0);
}
},
searchrules: { required: true }
},
I'm almost there with what I wanted to achieve. However I'm still facing some difficulties.
When the filter is initially loaded, value is selected on the dropdown but query value is empty. i.e. if the user clicks on the find button soon after the filter form is loaded, no filter will be set.
I still cannot get select2 styles working.
I can demonstrate how to use select2 with free jqGrid fork of jqGrid, which I develop. I get the demo from the README of the old version 4.14.1 (the current released version is 4.15.3) and modified it to demonstrate the usage of select2.
The main part of the code could be
stype: "select",
searchoptions: {
sopt: ["eq", "ne"],
...
selectFilled: function (options) {
setTimeout(function () {
$(options.elem).select2({
width: "100%"
});
}, 0);
}
}
See https://jsfiddle.net/Lae6kee7/2/. You can try to choose an option in the filter toolbar in "Shipped via" column and the open the search dialog. You will see that the select2 will have the same option selected.
If you would load the data via Ajax request posted by select2 than your code will be much more complex as it could be. It's important to understand that such way is really required only for really large set of possible value. I means the number of items larger as 100000 items for example. On the other side, the most use cases required less as 1000 options. In the case it would be more effective to load all the data as options of select and then convert the select to select2. select2, which uses local select works much more quickly from the users point of view.
The code will be easier in my opinion if you will use dataUrl instead of ajax option of select2. You can use dataUrl to return from the server all different values, which can be used in select2 and to use buildSelect to build <select> from JSON data returned from the server. The demo https://jsfiddle.net/Lae6kee7/23/ demonstrates that. I made the demo for JSFiddle, which supports Echo service (see here), which allows to simulate server responses. Your real code should contains mostly only dataUrl, buildSelect and the code of selectFilled, which I included above.
Additionally, I'd recommend you to consider to use <datalist> (see here for example), which could be good alternative to select2. All modern web browsers contains native support of <datalist> and thus <datalist> works very quickly. Try to search in the first Client column of my demos. You will see control, which will be very close to select2. Additional advantage of <datalist>: one will be able not search for only exact predefined values like test10, test11 or test12, but for substrings like 1. Compare
with
or
with

Hide/Show Editable links in webGrid CSHTML code

I am trying to Hide/Show the 'Edit' link in each row of a given Grid. Basically only few user roles have the access to Modify the information. I wrote a method ActionResult in controller class to check the user access and returning Boolean value. Based on True/False I need to Hide/Show the 'Edit' Links in a grid. The design code is written in MVC CSHTML. Please suggest how it can be achieved. I tried using JQuery but I can hide the Edit column only for the first row of the grid. Below is the code in CSHTML and Jquery.
CSHTML Code
<table border="1" cellpadding="0" cellspacing="1" style="width: 90%;"><tr>
<td>
#grid.GetHtml(tableStyle: "webGridMedium",
headerStyle: "header",
alternatingRowStyle: "alt",
selectedRowStyle: "select",
columns: grid.Columns(
grid.Column("BeginDate", "Begin Date", style: "description"),
grid.Column("Status", "Status", style: "description"),
grid.Column("", style: "description10", format: #<a class="edit-status" id="btnEditStatus" href="">Edit</a>)
))
</td>
</tr>
</table>
JQuery Code: The parameter data is boolean value I get it from Controller class.
<script>
$(document).ready(function () {
checkAccess();
}
function checkAccess() {
// debugger;
$.ajax({
url: '/Employee/CheckAccess',
type: 'POST',
data: {},
success: function (data) {
if (data == false) {
document.getElementById("btnEditStatus").style.visibility = "hidden";
}
else if (data = true) {
document.getElementById("btnEditStatus").style.visibility = "";
}
},
error: (function (result) {
alert("Failed access! Please contact administrator.");
})
})
}
</script>
<script>
function checkAccess() {
// debugger;
$.ajax({
url: '/Application/CheckAccess',
type: 'POST',
data: {},
success: function (data) {
if (data == false) {
$('.modaltable tr').each(function (i, row) {
$(this).find('.edit-status').hide();
});
}
else if (data = true) {
// your code
}
},
error: (function (result) {
alert("Please contact IT administrator for access.");
})
})
}
</script>

Ajax displays div dialog in mvc view

Pretty new to ajax.
So I have this div:
<div id="authentication" title="Authentication" >
<b>Please Generate a new token to continue!</b>
<br /><br />
<table>
<tr>
<td>Token:</td>
<td><input type="text" id="txtToken"/></td>
</tr>
<tr>
<td></td>
<td><label id="lblError"></label></td>
</tr>
</table>
</div>
which is not being displayed on my mvc view because it is a being used as a dialogue box by Ajax code below:
$('#authentication').dialog({
autoOpen: true,
width:500,
resizable: false,
beforeclose : function() { return false; },
title: 'Authentication',
modal: true,
buttons: {
"Cancel": function () {
window.location.replace("#Url.Action("Index", "Home")");
},
"Submit": function () {
var token=$('#txtToken').val();
var dlg = $(this);
$.ajax({
type: 'POST',
data: { 'token': token},
dataType: 'json',
url: '#Url.Action("CheckNewToken", "Account")',
success: function (result) {
if(result==true)
{
window.parent.jQuery('#authentication').dialog('destroy');
}
else{
$('#lblError').html("Incorrect credentials. Please try again");
}
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
}
}
});
However when the codes goes to success and result == result, the dialog box is destroyed but the div (dialog box) is then being displayed on my view which I don't want. What am I doing wrong?
Close the dialog and then destroy. This will hide the dialog completely and then destroy its dialog features. if you just do .dialog('destroy') it will just remove the dialog functionality completely and display the element as is on the page but it wont hide.
success: function (result) {
if(result==true)
{
$('#authentication').dialog('close').dialog('destroy');
}
else{
$('#lblError').html("Incorrect credentials. Please try again");
}
},
Another thing is beforeclose : function() { return false; }, you are returning false which will prevent the close event from happening. it should be beforeClose though you can remove it safely.
if the above doesnt work another option to remove the div is by subscribing to close event:-
$('#authentication').dialog({
autoOpen: true,
width:500,
resizable: false,
title: 'Authentication',
modal: true,
close:function(){
$(this).dialog('destroy').hide();
},
buttons: {
"Cancel": function () {
},
"Submit": function () {
var token=$('#txtToken').val();
var dlg = $(this);
$('#authentication').dialog('close');
}
}
});

Update another control after successful jeditable submit

I am using jEditable to update a value on my MVC model and back to the database successfully. The updated value appears on the webpage which is exactly what I want. I also want to update another control on the page (as it is a calculated value using the updated field). What is the best way to do this? Is there a hook into a successful jEditable update that I can add some jQuery into to refresh the other control?
My jEditable call :
$(function () {
$(".editable_textarea").editable("#Url.Action("UpdateSharePrice","Home")", {
indicator: "<span style='color:#FF0000;'>Saving...</span>",
type: 'text',
submitdata: { _method: "put" },
select: true,
submit: 'OK',
cancel: 'X',
width: '40',
cssclass: "editable",
tooltip: 'click to edit...',
onblur: "submit"
});
});
Thanks
Colin.
Well, I figured it out in the end
You can use the JEditable callback method to get the parameters used to call the controller method:
$(function () {
$(".editable_textarea").editable("#Url.Action("UpdateSharePrice","Home")", {
indicator: "<span style='color:#FF0000;'>Saving...</span>",
type: 'text',
select: true,
submit: 'OK',
cancel: 'X',
width: '40',
cssclass: "editable",
tooltip: 'click to edit...',
onblur: "submit",
callback: function(value, settings)
{
var fundId = this.id;
$.ajax({
url: '#Url.Action("GetMarketValue", "Home")',
type: 'POST',
data: { id : fundId },
success: function (data) {
$('#marketValueDiv_' + fundId).html(data);
}
});
}
});
});
This parameter can then be used to do an ajax post to another action method that returns the calculated field from the model:
public ActionResult GetMarketValue(int id)
{
if (ModelState.IsValid && id > 0)
{
BaseFund targetFund = _context.Funds.Find(id);
return PartialView("GetMarketValue", targetFund);
}
else
{
return PartialView("0.00");
}
}
The success callback on the ajax call is then used to update the appropriate div html content

Resources