Inserting table into Trirand JQGrid Edit Dialog that was creating with JQuery - asp.net

I have a JQuery and AJAX function that creates a table with 2 list boxes and buttons to add/remove from one list box to another. I want to insert this table into the Edit Dialog box of the JQGrid. So that when the add or edit button is clicked the table displays and the user can add items and the corresponding cell is edited.
Java script and AJAX:
function create_listbox() {
var html = '<select id="ddlRoles" size="7" multiple></select>';
var selected = '<select id="ddlSelectedRoles" size="7" multiple></select>';
var table = '<table id="table1" border="3"></table>';
var tr = '<tr id="tr1"></tr>';
var td1 = '<td id="td1"></td>';
var td2 = '<td id="td2"></td>';
var td3 = '<td id="td3"></td>';
var addButton = '<button id="add" onclick="addRole()">Add Role</button><br>';
var removeButton = '<button onclick="removeRole()">Remove Role</button><br>';
$('#whatever').append(table);
$('#table1').append(tr);
$('#tr1').append(td1);
$('#tr1').append(td2);
$('#tr1').append(td3);
$('#td1').append(html);
$('#td2').append(addButton);
$('#td2').append(removeButton);
$('#td3').append(selected);
$.ajax({
url: "MyApplication.asmx/GetRoles",
data: "{ }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
// alert(data.d);
for (var i = 0; i < data.d.length; i++) {
$('#ddlRoles').append("<option value=\"" + data.d[i].RoleID + "\">" + data.d[i].RoleName + "</option>")
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus + ":" + errorThrown);
}
});
}
I know that I need an EditTypeCustomElement and EditTypeGetCustomValue but I am completely stumped as to how to accomplish this.
I've tried returning $table.get(0) with the create_listbox function which adds a table but it doesn't add the child elements. Any help?

You need a EditTypeCustomElement (below) JQuery and Ajax:
function create_listbox(value, options) {
var htmlRolesDdl = '';
var htmlRolesSelectedDdl = '';
$.ajax({
url: "MyApp.asmx/GetRoles",
data: "{ }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
success: function (data) {
var roles = new Array();
if (value.toString().match(',') == null) roles.push(value.toString());
else roles = value.toString().split(",")
for (var i = 0; i < data.d.length; i++) {
var isMatch = false;
for (var j = 0; j < roles.length; j++) {
if (data.d[i].RoleName == roles[j].toString()) {
htmlRolesSelectedDdl += "<option id=\"option" + data.d[i].RoleID + "\"" + " value=\"" + data.d[i].RoleID + "\">" + data.d[i].RoleName + "</option>";
//$selected.append("<option id=\"option" + data.d[i].RoleID + "\"" + " value=\"" + data.d[i].RoleID + "\">" + data.d[i].RoleName + "</option>")
isMatch = true;
}
}
if (!isMatch) {
htmlRolesDdl += "<option id=\"option" + data.d[i].RoleID + "\"" + " value=\"" + data.d[i].RoleID + "\">" + data.d[i].RoleName + "</option>"
//$listbox.append("<option id=\"option" + data.d[i].RoleID + "\"" + " value=\"" + data.d[i].RoleID + "\">" + data.d[i].RoleName + "</option>")
}
}
},
async: false,
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus + ":" + errorThrown);
}
});
var html = '<table id="UserRole" border="0">';
html += '<tr>';
html += '<td>';
html += '<select id="ddlRoles" size="7" multiple width="155" style="width:155px;">';
html += htmlRolesDdl;
html += '</select>';
html += '</td>';
html += '<td>';
html += '<button onclick="addRole()" style="width:100px;"> Add >></button><br>';
html += '<button onclick="removeRole()" style="width:100px;"><< Remove</button><br>';
html += '</td>';
html += '<td>';
html += '<select class="ListBoxStyle" id="ddlSelectedRoles" size="7" width="155" multiple style="width:155px;">';
html += htmlRolesSelectedDdl;
html += '</select>';
html += '</td>';
html += '</tr>';
html += '<tr>';
html += '<td colspan="3" align="center" style="padding-top:5px;padding-bottom:10px;">';
html += '(Press and Hold "Ctrl" to select multiple roles)';
html += '</td>';
html += '</tr>';
html += '</table>';
return $(html);
}
and a EditTypeGetCustomValue (below) JQuery and AJAX:
function listbox_value(elem, type, stringvalue) {
if (type == 'get') {
var roles = new Array();
$("#ddlSelectedRoles > option").each(function () {
roles.push($(this).val().toString());
});
return roles.toString();
} else if (type == 'set') {
var roles = new Array();
$("#ddlSelectedRoles > option").each(function () {
roles.push($(this).val().toString());
});
$("#<%= hdnSelectedRoles.ClientID %>").val(roles);
return roles.toString();
}
}

Related

getting always the default link with ajax

I need to change the above link from
Voucher to special action..
The new link is getting from 'Slink ' in the DB
So how can I change it so the page will be the link provided in 'Slink' and not the default link
$.ajax({
url: '/Newsletter/GetUserOrder?id=' + id,
type: "POST",
dataType: 'json',
data: id,
success: function (response)
{
if (response === "")
{
location.href = '/Newsletter/CardDetails'+ id;
}
else
{
var Bname='';
var itemsQuantity;
var name = '';
var Link="פירוט הטבה"
$.each(response, function (indexInArray, valueOfElement)
{
Bname += '<div class="card col-md-5">' + '<b>' + valueOfElement.shortNameVar + '</b>' + '<br/> ' + valueOfElement.BusnessName + '</div>' + '<br/>' + '<div class="chip">' + "כמות" + valueOfElement.MemberOrderQuntity + '</div>' +
'<b>' + '<div class="flx-aut text-center">' + '' + Link + '' + '<div>';
});

Display all videos from JSON (ASP.NET MVC)

I have code for AJAX to get Links from table
Here is code
<script>
$('#display').click(function() {
var vacancyId = $("#vacancy").val();
var model = {
vacancyId: vacancyId
};
$.ajax({
url: '#Url.Action("Links", "Questions")',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(model),
type: 'POST',
dataType: 'json',
processData: false,
success: function(data) {
var question2 = data[0];
var videoHTML = '<video width="320" height="240" style="margin-left: 130px;margin-top: 20px;" controls>';
videoHTML += '<source src="' + document.location.origin + "/uploads/" + question2.Linkes + ".webm" + '" type="video/webm">';
videoHTML += '</video>';
$(".videolist").append(videoHTML);
}
});
});
</script>
Now I display only first video in array, but It can be about 10, I need to display all ten videos, how I need to rewrite code?
Your success function should be like this.
Post your Json sample data for accurate solution.
success: function(data) {
var question2 = data;
for (var i = 0; i <= question2.length-1; i++) {
var videoHTML = '<video width="320" height="240" style="margin-left: 130px;margin-top: 20px;" controls>';
videoHTML += '<source src="' + document.location.origin + "/uploads/" + question2[i].Linkes + ".webm" + '" type="video/webm">';
videoHTML += '</video>';
$(".videolist").append(videoHTML);
}
}

Google Places JS API Show Reviews

JS API, not web service:
I'm having trouble displaying the 0-5 reviews included with the Place Details Responses:
https://developers.google.com/maps/documentation/javascript/places#place_details_responses
So far I have been able to display these items:
details.name
details.rating
details.open_now
details.formatted_address
details.types
details.formatted_phone_number
But when I try to show details.reviews it just displays:
[object Object],[object Object],[object Object],[object Object],[object Object]
Has anyone had any success displaying the reviews?
function createMarker(place) {
var image = 'img/marker.png';
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
icon: image,
position: place.geometry.location
});
var request = { reference: place.reference };
service.getDetails(request, function(details, status) {
google.maps.event.addListener(marker, 'click', function() {
if (status == google.maps.places.PlacesServiceStatus.OK) {
// Replace empty spaces in navigation link with + symbols
var navLink = details.formatted_address;
navLink = navLink.replace(/\s/g, "+");
$('.navLink').html(navLink);
// Match Rating bar width to rating number
var ratingWidth = (details.rating*20)+"px";
$('.rating-bar > span').css('width', "'"+ratingWidth+"'");
var contentStr = '<h5 class="info-window-title">'+details.name+'</h5><ul class="info-window">';
if (!!details.rating) contentStr += '<li>Rating: <div class="rating-bar"><span style=width:'+ratingWidth+'></span></div><strong>'+details.rating+'</strong></li>';
if (!!details.open_now) contentStr += '<li class="open-now">'+details.open_now+'</li>';
contentStr += '<li>'+details.formatted_address+'</li>';
contentStr += '<li class=gray>'+details.types+'</li>';
// Check for platform to send appropriate app link
if ((navigator.platform.indexOf("iPhone") != -1)) {
contentStr += '<li class="link"><a class=navLink href=http://maps.apple.com/?daddr=Current+Location&saddr='+navLink+'><i class="fa fa-automobile"></i> Get Directions</a></li>';
} else {
contentStr += '<li class="link"><a class=navLink href=https://www.google.com/maps/dir/Current+Location/'+navLink+'><i class="fa fa-automobile"></i> Get Directions</a></li>';
}
if (!!details.formatted_phone_number) contentStr += '<li class="link"><i class="fa fa-phone"></i> '+details.formatted_phone_number+'</li>';
if (!!details.reviews) contentStr += '<li>'+details.reviews+'</li>';
contentStr += '</ul>';
infowindow.setContent(contentStr);
infowindow.open(map,marker);
} else {
var contentStr = "<h5>No Result, status="+status+"</h5>";
infowindow.setContent(contentStr);
infowindow.open(map,marker);
}
});
});
}
details.reviews doesn't return a string(what you get in the output is the string-representation of this array), it's an array which contains up to 5 review-objects. you must iterate over the items of this array and prepare the output on your own.
A sample-function which prepares the reviews:
(function (rs /*reviews-array*/ , fx /*review-parser*/ ) {
var list = document.createElement('ul');
rs.forEach(function (r) {
list.appendChild(fx(r));
});
return '<ul>' + list.innerHTML + '</ul>';
//remove the previous line when you want to return a DOMNode
return list;
}
(details.reviews,
function (r /*single review*/ ) {
console.log(r.aspects)
var item = document.createElement('li'),
review = item.appendChild(document.createElement('ul'))
props = {
author_name: 'author',
rating: 'rating',
text: 'text'
};
item.appendChild(document.createElement('h6'));
item.lastChild.appendChild(document.createElement('a'));
item.lastChild.lastChild
.appendChild(document.createTextNode(r.author_name));
if (r.author_url) {
item.lastChild.lastChild.setAttribute('href', r.author_url);
}
item.lastChild.appendChild(document.createTextNode('(' + r.rating +
')'));
if (r.aspects && r.aspects.length) {
item.appendChild(document.createElement('ul'));
r.aspects.forEach(function (a) {
item.lastChild.appendChild(document.createElement(
'li'));
item.lastChild.lastChild
.appendChild(document.createTextNode(a.type +
':' + a.rating))
});
}
item.appendChild(document.createElement('p'));
item.lastChild.appendChild(document.createTextNode(r.text));
return item;
}
))
Demo:
var map, infowindow, service;
function initialize() {
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(-33.8665433, 151.1956316),
zoom: 15
});
var request = {
placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
};
infowindow = new google.maps.InfoWindow();
service = new google.maps.places.PlacesService(map);
service.getDetails(request, function(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
createMarker(place)
}
});
}
function createMarker(place) {
var image = 'img/marker.png';
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
//icon: image,
position: place.geometry.location
});
var request = {
reference: place.reference
};
service.getDetails(request, function(details, status) {
google.maps.event.addListener(marker, 'click', function() {
if (status == google.maps.places.PlacesServiceStatus.OK) {
// Replace empty spaces in navigation link with + symbols
var navLink = details.formatted_address;
navLink = navLink.replace(/\s/g, "+");
$('.navLink').html(navLink);
// Match Rating bar width to rating number
var ratingWidth = (details.rating * 20) + "px";
$('.rating-bar > span').css('width', "'" + ratingWidth + "'");
var contentStr = '<h5 class="info-window-title">' + details.name + '</h5><ul class="info-window">';
if (!!details.rating) contentStr += '<li>Rating: <div class="rating-bar"><span style=width:' + ratingWidth + '></span></div><strong>' + details.rating + '</strong></li>';
if (!!details.open_now) contentStr += '<li class="open-now">' + details.open_now + '</li>';
contentStr += '<li>' + details.formatted_address + '</li>';
contentStr += '<li class=gray>' + details.types + '</li>';
// Check for platform to send appropriate app link
if ((navigator.platform.indexOf("iPhone") != -1)) {
contentStr += '<li class="link"><a class=navLink href=http://maps.apple.com/?daddr=Current+Location&saddr=' + navLink + '><i class="fa fa-automobile"></i> Get Directions</a></li>';
} else {
contentStr += '<li class="link"><a class=navLink href=https://www.google.com/maps/dir/Current+Location/' + navLink + '><i class="fa fa-automobile"></i> Get Directions</a></li>';
}
if (!!details.formatted_phone_number) contentStr += '<li class="link"><i class="fa fa-phone"></i> ' + details.formatted_phone_number + '</li>';
if (!!details.reviews && details.reviews.length) {
contentStr += '<li>Reviews' +
(function(rs /*reviews-array*/ , fx /*review-parser*/ ) {
var list = document.createElement('ul');
rs.forEach(function(r) {
list.appendChild(fx(r));
});
return '<ul>' + list.innerHTML + '</ul>';
//remove the previous line when you want to return a DOMNode
return list;
}
(details.reviews,
function(r /*single review*/ ) {
console.log(r.aspects)
var item = document.createElement('li'),
review = item.appendChild(document.createElement('ul'))
props = {
author_name: 'author',
rating: 'rating',
text: 'text'
};
item.appendChild(document.createElement('h6'));
item.lastChild.appendChild(document.createElement('a'));
item.lastChild.lastChild
.appendChild(document.createTextNode(r.author_name));
if (r.author_url) {
item.lastChild.lastChild.setAttribute('href', r.author_url);
}
item.lastChild.appendChild(document.createTextNode('(' + r.rating +
')'));
if (r.aspects && r.aspects.length) {
item.appendChild(document.createElement('ul'));
r.aspects.forEach(function(a) {
item.lastChild.appendChild(document.createElement(
'li'));
item.lastChild.lastChild
.appendChild(document.createTextNode(a.type +
':' + a.rating))
});
}
item.appendChild(document.createElement('p'));
item.lastChild.appendChild(document.createTextNode(r.text));
return item;
}
))
+ '</li>';
}
contentStr += '</ul>';
console.log(contentStr)
infowindow.setContent(contentStr);
infowindow.open(map, marker);
} else {
var contentStr = "<h5>No Result, status=" + status + "</h5>";
infowindow.setContent(contentStr);
infowindow.open(map, marker);
}
});
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places"></script>
<div id="map-canvas"></div>

MVC 4 - 500 error on JSON call

I have started using VS2012 (Yes about time). I have a MVC 4 app.
Im making a JSON call to get data from DB to render in html.
But Im getting a 500 Internal Server error I can see in Fiddler.
Im not sure how to debug this because I cant find where to
see the C# exception if that is what the issue is.
So Im calling getRecipe in javascript.
public JsonResult GetRecipe(int rid)
{
var recipe = _rep.GetRecipe(rid);
return Json(recipe, JsonRequestBehavior.AllowGet);
}
My script is
var dataService = new function () {
var serviceBase = '/Recipes/'
getRecipesList = function (callback) {
$.getJSON(serviceBase + 'GetRecipesList', {}, function (data) {
callback(data);
});
};
getRecipe = function (rid, callback) {
$.getJSON(serviceBase + 'GetRecipe', {rid:rid}, function (data) {
callback(data);
});
};
return {
getRecipesList: getRecipesList,
getRecipe: getRecipe
};
} ();
var renderer = new function () {
renderList = function () {
dataService.getRecipesList(renderListDiv);
},
renderListDiv = function (recipes) {
var listDiv = $('#listdiv');
listDiv.html("");
$(recipes).each(function (index) {
var table = '<table>';
table += ('<tr><td><a class="recipeLink" href="#recipeList-' + this.RecipeID + '">' + this.RecipeTitle + '</a></td></tr>');
table += '</table>';
listDiv.append(table);
});
},
selectedRecipe = function (anchor) {
var href = $(this).attr("href");
var rid = href.split("-")[1];
dataService.getRecipe(rid, renderRecipe);
};
renderRecipe = function (recipe) {
var recipeDiv = $('#recipediv');
var html = '<h1>' + recipe.RecipeTitle + ' (' + recipe.RecipeID + ')</h1>';
html += '<h4>Preparation Time: ' + getTimeString(recipe.PrepTime) + '</h4>';
html += '<h4>Cooking Time: ' + getTimeString(recipe.CookTime) + '</h4>';
var count = recipe.Ings.length;
var colmax = Math.ceil(count / 2);
var colleft = 0;
var colright = colmax;
html += '</br><table id="ingredientstable">';
for (colleft = 0; colleft < colmax; colleft++) {
html += '<tr>';
html += '<td>' + recipe.Ingredients[colleft].Units + ' ' + recipe.Ingredients[colleft].Measure + ' ' + recipe.Ingredients[colleft].IngredientName + '</td>';
if(colright < count)
html += '<td>' + recipe.Ingredients[colright].Units + ' ' + recipe.Ingredients[colright].Measure + ' ' + recipe.Ingredients[colright].IngredientName + '</td>';
colright += 1;
html += '</tr>';
}
html += '</table>';
html += '<h4>Method</h4>';
html += '<p>' + recipe.Method + '</p>';
recipeDiv.html(html);
numingredients = 0;
editorForm.loadEditor(recipe);
};
clearRecipe = function () {
$('#recipediv').html("");
};
getTimeString = function (time) {
if (time < 60)
return time + ' min';
return time / 60 + ' hours'
};
changeFont = function () {
$('body').css("font-family", "Comic Sans MS");
};
return {
renderList: renderList,
selectedRecipe: selectedRecipe,
changeFont: changeFont,
clearRecipe: clearRecipe,
};
}();
The browser's developer tools might be a good place to get some additional information. In Chrome you can press F12 to bring up the dev tools window. Click on the network tab and then fire off your ajax call. You will see the failed request highlighted in red. If you click on it you will see the error that has been returned from mvc.
I have provided a screenshot in this question: Ajax call with a 'failed to load resource : the server responded with a status of 500'

How to call a webmethod in the jquery multislect

when i am usung the jquery multiselect close function i am calling a webmethod in the code behind the method is not hitting
my script is:
$(document).ready(function () {
$('.department').multiselect({
show: ["bounce", 5], hide: ["blind", 1],
close: function () {
debugger;
var values = new Array();
$(this).multiselect("getChecked").each(function (index, item) {
values.push($(item).val());
});
$("input[id*=selectedValues]").val(values.join(","));
document.getElementById("<%=hdnDepartment.ClientID %>").value = values;
if (document.getElementById("<%=hdnDepartment.ClientID %>").value != "") {
//$("#<%=Button1.ClientID %>")[0].click();
$.ajax({
type: "POST",
url: "wfrmLeave_PermisssionReport.aspx/Populate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
}
});
}
}
})
.multiselectfilter();
});
my method is:
[System.Web.Services.WebMethod]
public void Populate()
{
strQuery = "select * from tblhrims_employeedetail where nvrresigned='No'";
if (ddlBusinessUnit.SelectedItem.ToString() != "Select")
{
strQuery += "and nvrbusinessunit ='" + ddlBusinessUnit.SelectedValue + "' ";
}
if (hdnDepartment.Value != "")
{
string strDepartment = hdnDepartment.Value;
string[] strarrDepartment = strDepartment.Split(',');
strDepartment = "";
for (int i = 0; i < strarrDepartment.Length; i++)
{
strDepartment += "'" + strarrDepartment[i] + "'" + ",";
}
strDepartment = strDepartment.TrimEnd(',');
strQuery += "and nvrdepartment in(" + strDepartment + ")";
}
populateDropDown(strQuery, ddlEmployeeName, "nvrempname", "nvrempcode");
}
i hope this helps you http://trentgardner.net/net/asp-net-webmethods-with-jquery-and-ajax/

Resources