When a user switches tabs in the jquery ui code they can click on a button to add data to a jqgrid. The only thing that is not working is datepicker; it will launch one time and after that when switching to a new tab it will not launch again.
source document code;
<link href="/Includes/Site.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="/Includes/jqgrid/css/jquery-ui.css">
<link rel="stylesheet" type="text/css" href="/Includes/jqgrid/src/css/ui.jqgrid.css">
<script src="/Includes/jqgrid/js/jquery-2.1.4.min.js"></script>
<script src="/Includes/jqgrid/css/jquery-ui-1.11.4.min.js"></script>
<script src="/Includes/jqgrid/js/i18n/grid.locale-en.js"></script>
<script src="/Includes/jqgrid/js/jquery.jqGrid.min.js"></script>
<script src="/Includes/validation/dist/jquery.validate.min.js"></script>
<script src="/Includes/validation/dist/additional-methods.min.js"></script>
$("#tabs").tabs(
{
//each tab activation
activate:function(event, ui)
{ loadGrid(); },
beforeActivate:function(event, ui)
{
var curTab = $('#tabs .ui-tabs-active');
var curTabName = curTab.text();
var pos = curTabName.search('-');
var curID = curTabName.slice(0,pos);
var passFrmDiv = "div[id^=adddaydata" + curID + "]";
$(passFrmDiv).html("");
}
});
function loadGrid()
{
var curTab = $('#tabs .ui-tabs-active');
var curTabName = curTab.text();
var pos = curTabName.search('-');
var newID = curTabName.slice(0,pos);
//reset adddaydata
$("div[id^=adddaydata" + newID + "]").text('');
//load form
var getFrmURL = '90daywellform.php?id=' + newID + '&d=' + $.now();
var passFrmDiv = "div[id=wellform" + newID +"]";
$.get(getFrmURL, function(data)
{ $(passFrmDiv).html(data); });
//$(passFrmDiv).load('90daywellform.php?id=' + newID + ' #loadform');
//load grid
var getGrdURL = '90daygrid.php?id=' + newID + '&d=' + $.now();
var passGrdDiv = "div[id=jqgrid" + newID +"]";
$.get(getGrdURL, function(data)
{ $(passGrdDiv).html(data); });
//$(passGrdDiv).load('90daygrid.php?id=' + newID);
};
//date picker function for adding wells info
$("button[id^=addday]").click(function()
{
var curTab = $('#tabs .ui-tabs-active');
var curTabName = curTab.text();
var pos = curTabName.search('-');
var newID = curTabName.slice(0,pos); //id of the current well
//var dtTag = '#datepicker' + newID;
$.get('get90maxday.php?id=' + newID,
function(data)
{
var passMax = data;
var getFrmURL = '90dayadddayform.php?id=' + newID + '&maxday=' + passMax + '&d=' + $.now();
var passFrmDiv = "div[id^=adddaydata" + newID + "]";
$.get(getFrmURL, function(data)
{ $(passFrmDiv).html(data); });
//$().text(newID + " " + passAPI);
//$("#gridarea").load('scadagrid.php?api=' + passAPI + '&start=' + passStart + '&end=' + passEnd);
}
);
});
And here is the code from the dynamic document that is being loaded;
<?php
$id = $_GET['id'];
?>
<link href="/Includes/Site.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="/Includes/jqgrid/css/jquery-ui.css">
<script src="/Includes/jqgrid/js/jquery-2.1.4.min.js"></script>
<script src="/Includes/jqgrid/css/jquery-ui-1.11.4.min.js"></script>
<script src="/Includes/validation/dist/jquery.validate.min.js"></script>
<script src="/Includes/validation/dist/additional-methods.min.js"></script>
<tr>
<td><label for="dt<?php echo $id; ?>">Date</label></td>
<td><input id="dt<?php echo $id; ?>" name="dt<?php echo $id;?>" type="text"></td>
</tr>
var passID = $("input[id^=id]").val();
var dtPkr = "input[id=dt" + passID +"]";
$(dtPkr).datepicker();
Any assistance is greatly appreciated
You're going to want to tap into the activate event if you've got this hosted in a tab (don't see it in the code, but it's in the question?). See jQuery UI Tabs Widget documentation.
You're going to want to add, in activate or in your loadGrid() function, something to initialize the datepickers:
$(".datepicker").datepicker();
You would, of course, have to have the datepickers be of class datepicker for that to work. See jQuery UI Datepicker documentation.
Related
I have a script that reads the content of a Json string and creates a table to display the data. This works fine as long as I include the Json string in the JQuery function. What I need to do is call another php file which returns the Json string.
I have written the script that returns the Json string:
[{"ClientName":"Name","RoomName":"Room 2","RoomFromTime":"06:00:00","RoomToTime":"17:00:00"},{"ClientName":"Name","RoomName":"Room 6","RoomFromTime":"06:00:00","RoomToTime":"23:00:00"},{"ClientName":"Name","RoomName":"Room 1","RoomFromTime":"07:00:00","RoomToTime":"17:00:00"},{"ClientName":"Name","RoomName":"Room 14","RoomFromTime":"07:00:00","RoomToTime":"23:00:00"},{"ClientName":"Name","RoomName":"Room 12","RoomFromTime":"07:00:00","RoomToTime":"19:00:00"},{"ClientName":"Name","RoomName":"Room 10","RoomFromTime":"07:00:00","RoomToTime":"23:00:00"},{"ClientName":"Name","RoomName":"Room 9","RoomFromTime":"07:00:00","RoomToTime":"23:00:00"},{"ClientName":"Name","RoomName":"Room 8","RoomFromTime":"07:00:00","RoomToTime":"23:00:00"},{"ClientName":"Name","RoomName":"Room 7","RoomFromTime":"07:00:00","RoomToTime":"23:00:00"},{"ClientName":"Name","RoomName":"Room 5","RoomFromTime":"07:00:00","RoomToTime":"23:00:00"},{"ClientName":"Name","RoomName":"Room 3","RoomFromTime":"07:00:00","RoomToTime":"23:00:00"},{"ClientName":"Name","RoomName":"Room 4","RoomFromTime":"08:00:00","RoomToTime":"23:00:00"},{"ClientName":"Name","RoomName":"Room 15","RoomFromTime":"08:00:00","RoomToTime":"19:00:00"}]
My JQuery function has the following:
$(document).ready(function () {
var json = $.getJSON("get_data.php", function(data){
var tr ;
for (var i = 0; i < json.length; i++) {
tr = $('<tr/>');
tr.append("<td><div class='clientname-text'>" + json[i].ClientName + "</div></td>");
tr.append("<td><div class='roomname-text'>" + json[i].RoomName + "</div></td>");
tr.append("<td><div class='time-text'>" + json[i].RoomFromTime + " - " + json[i].RoomToTime + "</div></td>");
$('table').append(tr);
}
});
});
Using the call to the other php script does not display the returned data in the table. Now I know I have gone wrong somewhere, but can anyoe see what I am doing incorrectly.
Many thanks in advance for your time.
UPDATE:
Because getJSON by default works asynchronously, the json variable is not filled at the time the callback is run. Change the callback to:
$.getJSON("get_data.php", function(json){
...
It appears there is a wrapper around your actual jason.
Either fix it like this:
$.getJSON("get_data.php", function(json){
json = json[0].data;
...
or fix it in get_data.php.
You can use this 100% working code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.getJSON("get_data.php", function(data){
$.each(data, function(i, field){
var tr ;
tr = $('<tr/>');
tr.append("<td><div class='clientname-text'>" + field.ClientName + "</div></td>");
tr.append("<td><div class='roomname-text'>" + field.RoomName + "</div></td>");
tr.append("<td><div class='time-text'>" + field.RoomFromTime + " - " + field.RoomToTime + "</div></td>");
$('table').append(tr);
});
});
});
</script>
</head>
<body>
<table></table>
</body>
</html>
I successfully created a small jqgrid table. I'm trying to export this table to an excel or pdf file using Jquery. I am new to jquery and jqgrid. Could someone please let me know what is wrong in the code? I would really appreciate some help or suggestions.
I found the export function online. It said I have to just call this function with the grid id. Am I doing something wrong?
export.php file:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="CSS/CalibrationKit.css" rel="stylesheet" type="text/css">
<!-- ------------------------JQGRID files-------------------------------- -->
<link rel="stylesheet" type="text/css" media="screen" href="jquery/css/jquery-ui-1.7.1.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<script src="jquery/js/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="jquery/js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="jquery/js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(e) {
/*jqgrid*/
var mydata = [{
head_mean_volume: "50",
head_std_dev: "2",
head_cv: "3",
offset_factor: "4",
scaling_factor: "5"
}];
$("#projectSpreadsheet").jqGrid({
data: mydata,
datatype: "local",
colNames: ["Head Mean Volume Dispensed", "Head Standard Deviation", "Head %CV", "Whole Head Offset Factor", "Whole Head Scaling Factor"],
colModel: [{
name: 'head_mean_volume',
index: 'head_mean_volume',
editable: false,
}, {
name: 'head_std_dev',
index: 'head_std_dev',
editable: false,
}, {
name: 'head_cv',
index: 'head_cv',
editable: false,
}, {
name: 'offset_factor',
index: 'offset_factor',
editable: false,
}, {
name: 'scaling_factor',
index: 'scaling_factor',
editable: false,
}],
'cellEdit': false,
'cellsubmit' : 'clientArray',
editurl: 'clientArray'
}); /*jqGrid close */
/* createExcelFromGrid */
$('#btnSun').click(function() {
$.fn.myFunction("projectSpreadsheet");
});
$.fn.myFunction = function(gridID,filename) {
var grid = $('#' + gridID);
var rowIDList = grid.getDataIDs();
var row = grid.getRowData(rowIDList[0]);
var colNames = [];
var i = 0;
for(var cName in row) {
colNames[i++] = cName; // Capture Column Names
}
var html = "";
for(var j=0;j<rowIDList.length;j++) {
row = grid.getRowData(rowIDList[j]); // Get Each Row
for(var i = 0 ; i<colNames.length ; i++ ) {
html += row[colNames[i]] + ';'; // Create a CSV delimited with ;
}
html += '\n';
}
html += '\n';
var a = document.createElement('a');
a.id = 'ExcelDL';
a.href = 'data:application/vnd.ms-excel,' + html;
a.download = filename ? filename + ".xls" : 'DataList.xls';
document.body.appendChild(a);
a.click(); // Downloads the excel document
document.getElementById('ExcelDL').remove();
}
}); /* function close */
</script>
</head>
<body>
<table id="projectSpreadsheet" class="fixed_headers" style="width:875px"></table>
<br><br>
<button id="btnSun">Export Table data into Excel</button>
</body>
</html>
Replace your function with this one and let me know if it works for you.
function(gridID,filename) {
var html = $('#gview_' + gridID).html();
var a = document.createElement('a');
a.id = 'tempLink';
a.href = 'data:application/vnd.ms-excel,' + html;
a.download = filename + ".xls";
document.body.appendChild(a);
a.click(); // Downloads the excel document
document.getElementById('tempLink').remove();
}
Hello I am trying to parse a .json file.. it only has 1 set of data I don't think the $.each is the best approach but it's all I can find....
Here is my code:
<script type="text/javascript">
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
var url = 'http://f.cl.ly/items/1L2k221B183J1e1G411j/200.json';
/* <![CDATA[ */
$(document).ready(function(){
$.getJSON(url, function(data){
$.each(data.test, function(i,test){
content = '<h1><p class="p1"><span class="s1">' + test.name + '</span></p></h1><table class="table table-bordered"><tbody><tr><td>Section:</td><td>Chemistry</td></tr><tr><td>Synonyms:</td><td>Mg Level</td></tr><tr><td>Container:</td><td>' + test.container + '</td></tr><tr><td>Reference Ranges:</td><td>' + test.reference + '</td></tr><tr><td>Availability:</td><td>' + test.availability + '</td></tr><tr><td>Special Handling:</td><td>' + test.specialHandling + '</td></tr><tr><td>Additional Comments:</td><td>' + test.additionalComments + '/td></tr></tbody></table>';
$(content).appendTo("#main");
$("#main").fadeIn( );
});
});
});
/* ]]> */
</script>
The result kinda works... the table is there but the code loops 3 times and displays "undefined" for all veribles.
Any help would be great!
Just use the result data without a loop. If data.test is not an array, you can access the object properties directly.
something like :
<script type="text/javascript">
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
var url = 'http://f.cl.ly/items/1L2k221B183J1e1G411j/200.json';
/* <![CDATA[ */
$(document).ready(function(){
$.getJSON(url, function(data){
content = '<h1><p class="p1"><span class="s1">' + data.test.name + '</span></p></h1><table class="table table-bordered"><tbody><tr><td>Section:</td><td>Chemistry</td></tr><tr><td>Synonyms:</td><td>Mg Level</td></tr><tr><td>Container:</td><td>' + data.test.container + '</td></tr><tr><td>Reference Ranges:</td><td>' + data.test.reference + '</td></tr><tr><td>Availability:</td><td>' + data.test.availability + '</td></tr><tr><td>Special Handling:</td><td>' + data.test.specialHandling + '</td></tr><tr><td>Additional Comments:</td><td>' + data.test.additionalComments + '/td></tr></tbody></table>';
$(content).appendTo("#main");
$("#main").fadeIn( );
});
});
/* ]]> */
</script>
Try this:
<script type="text/javascript">
function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if(pair[0] == variable){return pair[1];}
}
return(false);
}
var url = 'http://f.cl.ly/items/1L2k221B183J1e1G411j/200.json';
/* <![CDATA[ */
$.getJSON(url, function(data)
{
for(var i in data.test)
{
var obj = data.test[i];
var content = '<h1><p class="p1"><span class="s1">' + obj.name + '</span></p></h1><table class="table table-bordered"><tbody><tr><td>Section:</td><td>Chemistry</td></tr><tr><td>Synonyms:</td><td>Mg Level</td></tr><tr><td>Container:</td><td>' + test.container + '</td></tr><tr><td>Reference Ranges:</td><td>' + obj.reference + '</td></tr><tr><td>Availability:</td><td>' + obj.availability + '</td></tr><tr><td>Special Handling:</td><td>' + obj.specialHandling + '</td></tr><tr><td>Additional Comments:</td><td>' + obj.additionalComments + '/td></tr></tbody></table>';
$(content).appendTo("#main");
$("#main").fadeIn( );
}
});
/* ]]> */
</script>
$.each(Object or Array, iterator) is probably what you should use. The real issue is that data.test has to be an Object or an Array, not an Object property that's not an Object or an Array.
hello friends i am working on a like module .and having some difficulties running it on ie . in all other browsers its running fine . the prob is when i click on like button nothing happens in ie . can anyone see my piece of code and help me out . IE is Such a pain .
$('.LikeThis').livequery("click",function(e){
var getID = $(this).attr('id').replace('post_id','');
var uid = $('.like_uid').val();
var type_id = $('.like_type_id').val();
$("#shlike-loader-"+getID).html('<img src="images/icons/like.gif" alt="" />');
$.post("eg?postId="+getID+ '&uid=' + uid + '&type_id=' + type_id, {
}, function(response){
$('#hiddenlikesval'+getID).remove();
$('#likepanel'+getID).show();
$('#likecontainer'+getID).append($(response).fadeIn('slow'));
var bing=$('#hiddenlikesval'+getID).val();
$('#lplbl'+getID).html(bing);
$('#likePanel'+getID).css({'display':'block','width':'404px','padding':'5px 3px 5px 3px','background':'#ECEFF5'});
$('#like-panel-'+getID).html('<input type="hidden" class="like_uid" id="like_uid'+getID+'" name="like_uid" value="'+uid+'"/><input type="hidden" class="like_type_id" id="like_type_id'+getID+'" name="like_type_id" value="'+type_id+'"/><div class="comtcontbtm unlwid"><span class="shareiconsbottom unlikeiconbtm"></span><span class="replyshare">unlike</span></div>');
$("#shlike-loader-"+getID).html('');
});
});
Are you using jQuery 1.7? If so, you should use on, livequery is deprecated. If you're using pre-1.7 you should use .delegate.
For 1.7 change your function declaration to:
$(document).on("click", ".LikeThis", function (e) {
//your stuff here
});
For pre-1.7 change it to:
$(document).delegate("click", ".LikeThis", function (e) {
//your stuff here
});
On: http://api.jquery.com/on/
Delegate: http://api.jquery.com/delegate/
$('.LikeThis').live('click', function (e) {
var getID = $(this).attr('id').replace('post_id', '');
var uid = $('.like_uid').val();
var type_id = $('.like_type_id').val();
$("#shlike-loader-" + getID).html('<img src="images/icons/like.gif" alt="" />');
$.post("modules/like/likeit.php?postId=" + getID + '&uid=' + uid + '&type_id=' + type_id, {
}, function (response) {
$('#hiddenlikesval' + getID).remove();
$('#likepanel' + getID).show();
$('#likecontainer' + getID).append($(response).fadeIn('slow'));
var bing = $('#hiddenlikesval' + getID).val();
$('#lplbl' + getID).html(bing);
$('#likePanel' + getID).css({ 'display': 'block', 'width': '404px', 'padding': '5px 3px 5px 3px', 'background': '#ECEFF5' });
$('#like-panel-' + getID).html('<input type="hidden" class="like_uid" id="like_uid' + getID + '" name="like_uid" value="' + uid + '"/><input type="hidden" class="like_type_id" id="like_type_id' + getID + '" name="like_type_id" value="' + type_id + '"/><div class="comtcontbtm unlwid"><span class="shareiconsbottom unlikeiconbtm"></span><span class="replyshare">unlike</span></div>');
$("#shlike-loader-" + getID).html('');
});
});
I have google map on my site(php,apache,mysql).
here is code:
<script type="text/javascript">
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
var point = new GLatLng(<? $default_ll ?>,<? $default_spn ?>);
map.setCenter(point, 15);
map.setUIToDefault();
map.setMapType(G_NORMAL_MAP);
var marker = new GMarker(point);
map.addOverlay(marker);
}
}
window.onload = initialize;
window.onunload = GUnload;
</script>
<div id="map_canvas" style="width:500px;height:300px;"></div>
I want to make button(outside of map div), so when user chose hes coords and clicks this button. Current location he is watching rightnow will be put in to database. Well for simplicity can you show how to assign current: ll to $new_ll and spn to $new_spn
The GMap2 map object has these methods:
var center = map.getCenter(); // returns GLatLng of center point of map
var span = map.getBounds(); // returns GLatLngBounds of current map view
Using these simply you could have a button like this:
<input type="button" onclick="javascript:alert(map.getCenter())" value="Show Coordinates"/>
More realistically, you'll define a function to be called by the button's onclick:
function showCenterAndSpan()
{
var center = map.getCenter();
var span = map.getBounds();
var ne = span.getNorthEast();
var sw = span.getSouthWest();
var coordStr = "lat=" + center.lat() + "&lng=" + center.lng() +
"&n=" + ne.lat() + "&e=" + ne.lng() + "&s=" + sw.lat() + "&w=" + sw.lng();
alert(coordStr); // Or, post this string to the server to save into the database
}