Jquery UI Datepicker disabling wrong days - jquery-ui

I have run in to issue that days that have to be disabled are shifted to the next day.
The idea is that day that does not exist in our booking object or have a value less than 1 should be disabled on calendar.
here is simplified version of my script and demonstration on jsfiddle:
var bookings = {
"2012-09-01": 24,
"2012-09-03": 31,
"2012-09-05": 27,
"2012-09-06": 9,
"2012-09-07": 18,
"2012-09-08": 0,
"2012-09-10": 20,
"2012-09-12": 19,
"2012-09-13": 0,
"2012-09-14": 9,
"2012-09-15": 24,
"2012-09-17": 19,
"2012-09-19": 28,
"2012-09-20": 15,
"2012-09-21": 12,
"2012-09-22": 25,
"2012-09-24": 19,
"2012-09-26": 0,
"2012-09-27": 0,
"2012-09-28": 0,
"2012-09-29": 0
};
function MyEvent(date)
{
bookings = bookings || {};
this.date = date.toISOString().substr(0, 10);
this.display = (typeof bookings[this.date] == 'number' && bookings[this.date] > 0);
return this;
}
MyEvent.prototype.toArray = function () {
return [this.display, null, null];
};
$(function ()
{
$('#eventCalendar').datepicker({
dateFormat: "yy-mm-dd",
firstDay: 1,
defaultDate: "2012-09-24",
beforeShowDay: function (date)
{
return new MyEvent(date).toArray();
}
}
);
});
Can some one suggest me what am I doing wrong or is it a bug?

It was a little struggle, but is not a bug.
There is a problem using:
date.toISOString()
the operation can return a different date (for example next day) from the original date passed from the plug in, because the function ignores timezone offsets
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/toISOString
so your condition will not work well.
Here is a working fiddle the is not using that function: http://jsfiddle.net/nBejK/2/

Related

JSPDF Autotable not exporting defined table footer

I have an html table defined based on dynamic data. The table contains a thead, tfoot and tbody. The tfoot is mapped to specific values within my json. However, when using JSPDF Autotable and exporting to PDF the footer is not rendered. I have seen information but no code examples of the showfoot option and have tried it after the doc.autotable, and even after the style options but to no avail. The footer is not exported. I'm sure it super simple - but I can't seem to figure it out. Note: I do not want JSDPF Autotable to 'create' a footer - it is defined, it is part of my table - I simply want it rendered on the pdf. I found an old stackoverflow from 2016 where this was mentioned - Simon B. commented it would be added - the topic was closed - but I couldn't find a code example anywhere.
Here is my jspdf autotable code where I have tried to 'show my footer' - but to no avail. Any assistance appreciated.
<script>
function generate() {
//Creation of PDF document
let doc = new jsPDF('l', 'pt');
const totalPagesExp = '{total_pages_count_string}';
var elem = document.getElementById('${pm.info.requestId}');
var data = doc.autoTableHtmlToJson(elem);
doc.autoTable(data.columns, data.rows, {
headStyles: {
cellWidth: 'wrap',
fontSize: 10,
lineWidth: 0,
lineColor: [0, 0, 0],
textColor: [0, 0, 0],
fillColor: [255,255,255]
},
bodyStyles: {
cellWidth: 'wrap',
fontSize: 8,
lineWidth: 0,
lineColor: [0, 0, 0],
textColor: [0, 0, 0],
fillColor: [255,255,255]
},
footStyles: {
cellWidth: 'wrap',
fontSize: 10,
lineWidth: 0,
lineColor: [0, 0, 0],
textColor: [0, 0, 0],
fillColor: [211,211,211]
},
//Formatting of pages
didDrawPage: function (data) {
//Summa logo on top of the page
doc.addImage('${pm.variables.get("summa")}', 'PNG', 20, 20, 145, 42.63);
//Font sizes of report information
doc.setFontSize(8);
//Report information: portfolio name, knowledge time and report time
doc.text(35, 75, '${pm.variables.get("portfolioName")}');
doc.text(35, 85, '${pm.variables.get("reportTime")}');
doc.text(35, 95, '${pm.variables.get("knowledgeTime")}');
//Page numbers
var str = "Page " + doc.internal.getNumberOfPages()
if (typeof doc.putTotalPages === 'function') {
str = str + " of " + totalPagesExp;
};
//Page size
var pageSize = doc.internal.pageSize;
var pageHeight = pageSize.height ? pageSize.height : pageSize.getHeight();
doc.text('Theme "plain"', 14, 16);
},
margin: {
top: 100
},
});
//Number of pages
if (typeof doc.putTotalPages === 'function') {
doc.putTotalPages(totalPagesExp);
},
//--------------------------------------------------------------------------------------------------START
//Change name of report if desired
doc.save('${pm.info.requestName}${pm.variables.get("reportTime")}.pdf');
//--------------------------------------------------------------------------------------------------END
}
Not sure if you already managed to export footer totals to the jspdf. I got it to working, with a small adjustment.
Replace the var elem = document.getElementById('${pm.info.requestId}'); to var elem = document.getElementById('NAME');
And replace the doc.autoTable(data.columns, data.rows, { to doc.autoTable({html: '#NAME',

jquery ui - disable today and highlight next 3 days

So far I can disable Today's date, but I'm coming up short trying to highlight the next 3 days
$( "#someDiv" ).datepicker({
beforeShowDay: function( date ){
//disable Sundays;
return [date.getDay() != 0, '']
},
/* today is disabled */
minDate: 1
});
... or is there a way to render individual day cells with date info as data attributes or something like that?
In your return, add a condition that will check for the date range you want and add a class to those dates.
Here is a jsFiddle with the full example. I'm sure this can be improved upon though.
The code and CSS to add a background to the dates when the condition is true (style it how you like):
.highlightDay .ui-state-default {
background: #484;
color: #FFF;
}
$(document).ready(function() {
$("#datepicker").datepicker({
beforeShowDay: function(date) {
var newDate = addDays(new Date(), 0);
var thirdDay = addDays(new Date(), 3);
return [date.getDay() != 6,
// This can probably be improved
date >= newDate && date <= thirdDay ? "highlightDay" : ""];
},
minDate: 1
});
});
function addDays(theDate, days) {
return new Date(theDate.getTime() + days*24*60*60*1000);
}

Highcharts, how can I start xAxis on an arbitrary time

I have a line chart with a datetime xAxis. I need to show ticks every 10 minutes, for that I have set tickInterval to 10*60*1000, my problem is that I need to show ticks every 10 minutes since the first date, for example, if my first point is displayed at 10:33, I need to show ticks at 10:33, 10:43, 10:53, etc, but what I have are ticks at 10:30, 10:40, 10:50 and so on, is there any way to do this?
Thanks!
It's not that straightforward because Highcharts automatically determines the labels to use when the x-axis is of the type 'datetime':
"In a datetime axis, the numbers are given in milliseconds, and tick marks are placed on appropriate values like full hours or days"
To set labels like '10:33' you need to create your own categories. Luckily these can simply be derived from your data and the desired time interval.
Here's a working example: http://jsfiddle.net/Rt7ZV/
We just take the given start date, interval and number of points and build an array of the categories to be used as the x-axis labels.
function getTimes(numTimes, interval) {
var ms = (new Date(2012, 02, 30, 10, 33)).getTime();
var times = [];
var startDate = new Date(ms);
times.push(startDate.getHours() + ":" + startDate.getMinutes());
for (var i = 1; i< numTimes; i++)
{
ms += interval;
var nextTime = (new Date()).setTime(ms);
var nextDate = new Date(nextTime);
times.push(nextDate.getHours() + ":" + pad(nextDate.getMinutes(), 2));
}
return times;
}
function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
var data = [1, 2, 3, 4, 5, 3, 2, 5, 7, 6, 4];
var interval = 10*60*1000
var timeCategories = getTimes(data.length, interval);
$(function () {
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
zoomType: 'x',
spacingRight: 20
},
title: {
text: 'Time series'
},
xAxis: {
categories: timeCategories,
title: {
text: null
},
startOnTick: false
},
yAxis: {
title: {
text: 'Exchange rate'
},
startOnTick: false,
showFirstLabel: true
},
tooltip: {
shared: true
},
legend: {
enabled: false
},
series: [{
type: 'line',
name: 'time series',
data: [
1, 2, 3, 4, 5, 3, 2, 5, 7, 6, 4
]
}]
});
});
});
I found the tickPositions property on xAxis, which isn't documented on highcharts, only on highstock, but seems to work fine on both. With this property you can specify which values you want to hace a tick for, and work perfectly for my problem.

Jquery UI Datepicker with EOM and MOM

I need implement a Jquery UI Calendar that when a users click any date, it select automatically to the next MOM or EOM.
For example, if the user clicks on April 9th, it must go to 'April 15th',
or if clicks on 22 February, it must go to 'Feb 28' or 'Feb 29'.
For clarification:
MOM is "Middle of Month, every time is 15th", from 1 to 15 of any month.
EOM is "End of Month, it's the last day of the month, jan 31, feb 28 or 29, mar 31, apr 30, may 31, jun 30, july 31, aug 31, sep 30, oct 31, nov 30, dec 31...
Thanks
I would do this as a standard jquery onchange handler for the text box. The reason is that a user can bypass the date-picker and type in a date manually. You want the date to automatically be changed no matter if the date is selected, typed in, or pasted in.
Use the onSelect event from datepicker. Be aware that if you have maxDate option set and its before EOM or MOM the value selected will be maxDate.
UPDATE: Close after select. Use onClose instead.
UPDATE2: jsFiddle using latest jQuery UI
onClose: function(dateText, inst) {
var day = inst.currentDay,
month = inst.currentMonth,
year = inst.currentYear;
if (day <= 15) {
day = 15;
}
else {
day = 0;
if (month>10){
month = 0;
year++;
}
else
month++;
}
var d = new Date(year, month, day);
$(this).datepicker("setDate", d);
}
The code that works with IE6/IE7/IE8/IE9 and FF, Chrome, etc.
<script type="text/javascript">
$(function() {
$("#payperiodcalendar input").datepicker({
dateFormat: 'dd-M-yy',
onSelect: function(dateText, inst) {
var selectedDay = inst.currentDay,
selectedMonth = inst.currentMonth,
selectedYear = inst.currentYear;
if (selectedDay <= 15) {
var now = new Date(selectedYear, selectedMonth, 15);
} else {
var now = new Date(selectedYear, selectedMonth, LastDayOfMonth(selectedYear, selectedMonth));
}
$(this).attr("value", now.format("dd-MMM-yyyy"));
}
});
function LastDayOfMonth(Year, Month) {
Month++;
return (new Date((new Date(Year, Month, 1)) - 1)).getDate();
}
});
</script>

Difficult couchdb query

I have the following query:
view.reduce.group_level(5).keys
which returns:
[["1f9c79a33f399a7937d880c5f31e8dbc", 2011, 12, 29, 13], ["1f9c79a33f399a7937d880c5f31e8dbc", 2011, 12, 29, 14], ["c38332ffc275b6c70bcf06ffc39ddbdd", 2011, 12, 29, 13], ["c38332ffc275b6c70bcf06ffc39ddbdd", 2011, 12, 29, 14]]
The first key is an id and the other keys are year, month, day, hour
I would like all the rows between 2010 and 2013. So I want to ignore the first key.
The problem is that i need to set the first parameter to get the results but i want to get all the results for all the keys.
for example: view.reduce.group_level(5).startkey(["every_possible_key", 2010]).endkey(['every_possible_key", 2013, {}])
If i leave the first key blank than i get nothing. If i give it "\u9999" than i get everything and it ignores the 2nd key.
Somebody knows what I am doing wrong?
Thanks a lot.
map:
function(d) {
if (d['type'] == 'State' && d['driver_id'] && d['name'] && d['created_at']) {
var dt = new Date(d.created_at);
emit([d.driver_id, dt.getFullYear(), dt.getMonth() + 1, dt.getDate(), dt.getHours()], d.name);
}
}
reduce:
function(k,v,r) {
var result = {
'hire': 0, 'hired': 0, 'arrived': 0, 'pick up': 0, 'drop off': 0,
'missed': 0, 'rider cancel': 0, 'driver cancel': 0, 'no show': 0,
'avail': 0, 'unavail': 0, 'other': 0
};
if (r) {
var row = null;
for (i in v) {
row = v[i];
for (j in row) {
result[j] += row[j];
}
}
} else {
for (i in v) {
if (result[v[i]] != null) {
result[v[i]] += 1;
} else {
result['other'] += 1;
}
}
}
return result;
}
What you're "doing wrong" is to use a key you don't need in your query as the first key of your view.
If you need it for another query, create another view.

Resources