You are viewing limited content. For full access, please sign in.

Question

Question

Form Lookup

asked on May 10, 2020

Hello, 

I'm trying to find the best way to lookup data for each calendar date. I need to lookup the data by matching the district, school year, and date combination. I initially did this by populating a collection, but I do not want to see a row for each date. This will eventually be in a calendar format. I have the data in the SQL database, but am trying to get out of creating a lookup rule for each date. I have hidden lookup fields to match the month, date, year, but this seems very inefficient. Is there javascript that could make this more efficient? Thanks! 

 

 

 

0 0

Answer

SELECTED ANSWER
replied on May 12, 2020

It depends on which method you want to use. Since you're already storing the day of the week in the SQL table, it may be easier to go with option 2, but option 1's offset amount for the first day would also be easy. It also depends on what you do with the data in Forms and Workflow.

For example, if you need to email someone a specific day's value, option 1 would likely be better. However, if you want to email a week's total, there would be fewer date calculations with option 2. Personally, since you're storing the day of the week as well, I'd go with option 1; it has more flexibility and will be easier for you to do.

Let me break down what I did for option 1. If you would rather 2, let me know and I can write that up too. FYI both of them are using tables, not collections.

 

Option 1 - One Row Per Day

For the table's settings, I have a single column with no header text (variable is Hours) at 50% width, and the row label as {n} so it shows the day number, also at 50% width.

You'll then want to have a lookup rule that finds all of the Workday values from the SQL table (in order, including blanks) and fill the Hours column with them. The day number will populate automatically.

You will also need the a separate field for the 1st of the month's day of the week, aka the offset. If you can, I'd recommend a quick stored procedure that simply grabs the day of the week from the "Day" column in your SQL table and substitutes it for a numerical value, Sunday = 1, Monday = 2, etc.

For the formatting, apply the "month-table" CSS class to the tables

and use the following CSS:

.month-table td.col0 {
  padding-right: 4px;
}
.month-table td {
  display: inline-block;
  width: 50%;
  text-align: right;
}
.month-table tr {
  float: left;
  width: 14%;
}
.month-table .cf-xlarge {
  width: 100%;
}

The following JavaScript will apply the first day's offset:

var monthTableIDs = [
  [?,?],	//Jan
  [?,?],	//Feb
  [?,?],	//Mar
  [?,?],	//Apr
  [?,?],	//May
  [?,?],	//Jun
  [?,?],	//Jul
  [?,?],	//Aug
  [?,?],	//Sep
  [?,?],	//Oct
  [?,?],	//Nov
  [?,?]	//Dec
];
$(document).ready(function() {
  for (var i of monthTableIDs) {
    if (i[0] !== null && i[1] !== null)
      changeMonthPadding(i[0], i[1]);
  }
});
$(document).on("lookupcomplete", function() {
  for (var i of monthTableIDs) {
    if (i[0] !== null && i[1] !== null)
      changeMonthPadding(i[0], i[1]);
  }
});

function changeMonthPadding(sourceID, tableID) { //source and table are numbers --> 4 means q4
  var firstday = $("#q" + sourceID + " input").val();
  var tr = $("#q" + tableID + " tbody tr:first-child()");
  $(tr).css("width", (14*firstday) + "%");
  $(tr).find("td").css("width", Math.floor((100/firstday/2)*10)/10 + "%");
  $(tr).find("td.col0").css("width", Math.ceil((1-(1/firstday/2))*100*10)/10 + "%");
};

You will need to add the IDs of your tables and offset fields into the monthTableIDs variable. Just replace the first question mark of a row with the table ID (if it's q15, that's the number 15), and the second question mark with the corresponding field with the offset number.

So "[?,?], //Jan" becomes "[15,22], //Jan" if your Jan table is q15 and the Jan offset field is q22.

 

0 0

Replies

replied on May 11, 2020

Hi Tiffany,

If you add another column in the Forms table for the date, you should be able to remove the DayNumber matching in the lookup rule and return it along with Workday into the two columns.

I'm not sure how your SQL table is laid out, but if you have missing rows when doing this then you could create a Store Procedure in SQL that returns the above with extra 'empty' Workday values for missing dates.

0 0
replied on May 11, 2020

I had it link by the date initially, but then thought I don't want to have to redo this again next year. I currently have to have a field with each date so I can format this in a calendar view. When I tried with a table or collection, I could populate the entire year's worth of data easily, but then couldn't format like a calendar because it would create a new row of data for each day. Any suggestions? 

0 0
replied on May 11, 2020 Show version history

But isn't that what you're doing now? Based on the screenshot it looks like you have a table for each month. If you leave in the month in the lookup rule, you'll still have it separated by month, and then next year you only have to change the year field.

What type of formatting are you doing to make it look like a calendar? I'm not sure why the row label works better for you than a different column; they are treated very similarly in the HTML & CSS.

0 0
replied on May 11, 2020

I am trying to get it to look like a calendar. I think that's why I'm struggling. I can get it to pull the data in a populate the entire table with one rule. But to format, I need a variable for each date.  Here is an example of what happens when I have it populate both the date and the hours for each date. I can't get it to format like a calendar. 

 

0 0
replied on May 11, 2020

Can you show me what the old method looks like when you have it looking how you want it?

0 0
replied on May 11, 2020

I've attached a sample. They would basically put an 'x' on the days they planned on working then a secretary would manually put that into an electronic format (that doesn't look like a calendar). 

calendar.png
calendar.png (1.01 MB)
0 0
replied on May 12, 2020

I was under the impression you had the calendar formatting working with your initial method of a lookup for every date. In that case:

 

You'd be able to wrap the table to display 7 rows per line, but to show the proper offsets (ie if Jul 31 is a Wed, Aug 1 would need to be Thurs) you'd need to make it messy with Forms/SQL. It also looks quite messy on mobile.

 

Another option is to give the table 14 cells per line (7 days of the week × 2) to have the days of the week, and then fill it with an SQL stored procedure which would account for the offsets and total day count in the month.

The two cells per day would be the day number and # of hours, and you could have SQL return -1 for the day and then hide both cells in Forms when that's the case.

 

0 0
replied on May 12, 2020

This is exactly what I want it to look like. Do you have a it set up as a variable for each box? 

0 0
replied on May 12, 2020

Do you have it in a table or a collection? I have this in a collection. The dates and hours are pulling from the SQL table. How did you get it to have 7 rows per line? 

0 0
replied on May 12, 2020

Sorry for another post. This is what my SQL table looks like. 

0 0
SELECTED ANSWER
replied on May 12, 2020

It depends on which method you want to use. Since you're already storing the day of the week in the SQL table, it may be easier to go with option 2, but option 1's offset amount for the first day would also be easy. It also depends on what you do with the data in Forms and Workflow.

For example, if you need to email someone a specific day's value, option 1 would likely be better. However, if you want to email a week's total, there would be fewer date calculations with option 2. Personally, since you're storing the day of the week as well, I'd go with option 1; it has more flexibility and will be easier for you to do.

Let me break down what I did for option 1. If you would rather 2, let me know and I can write that up too. FYI both of them are using tables, not collections.

 

Option 1 - One Row Per Day

For the table's settings, I have a single column with no header text (variable is Hours) at 50% width, and the row label as {n} so it shows the day number, also at 50% width.

You'll then want to have a lookup rule that finds all of the Workday values from the SQL table (in order, including blanks) and fill the Hours column with them. The day number will populate automatically.

You will also need the a separate field for the 1st of the month's day of the week, aka the offset. If you can, I'd recommend a quick stored procedure that simply grabs the day of the week from the "Day" column in your SQL table and substitutes it for a numerical value, Sunday = 1, Monday = 2, etc.

For the formatting, apply the "month-table" CSS class to the tables

and use the following CSS:

.month-table td.col0 {
  padding-right: 4px;
}
.month-table td {
  display: inline-block;
  width: 50%;
  text-align: right;
}
.month-table tr {
  float: left;
  width: 14%;
}
.month-table .cf-xlarge {
  width: 100%;
}

The following JavaScript will apply the first day's offset:

var monthTableIDs = [
  [?,?],	//Jan
  [?,?],	//Feb
  [?,?],	//Mar
  [?,?],	//Apr
  [?,?],	//May
  [?,?],	//Jun
  [?,?],	//Jul
  [?,?],	//Aug
  [?,?],	//Sep
  [?,?],	//Oct
  [?,?],	//Nov
  [?,?]	//Dec
];
$(document).ready(function() {
  for (var i of monthTableIDs) {
    if (i[0] !== null && i[1] !== null)
      changeMonthPadding(i[0], i[1]);
  }
});
$(document).on("lookupcomplete", function() {
  for (var i of monthTableIDs) {
    if (i[0] !== null && i[1] !== null)
      changeMonthPadding(i[0], i[1]);
  }
});

function changeMonthPadding(sourceID, tableID) { //source and table are numbers --> 4 means q4
  var firstday = $("#q" + sourceID + " input").val();
  var tr = $("#q" + tableID + " tbody tr:first-child()");
  $(tr).css("width", (14*firstday) + "%");
  $(tr).find("td").css("width", Math.floor((100/firstday/2)*10)/10 + "%");
  $(tr).find("td.col0").css("width", Math.ceil((1-(1/firstday/2))*100*10)/10 + "%");
};

You will need to add the IDs of your tables and offset fields into the monthTableIDs variable. Just replace the first question mark of a row with the table ID (if it's q15, that's the number 15), and the second question mark with the corresponding field with the offset number.

So "[?,?], //Jan" becomes "[15,22], //Jan" if your Jan table is q15 and the Jan offset field is q22.

 

0 0
replied on May 12, 2020

Thank you so much. I owe you! 

 

I'm pretty close, just stuck on this part of the JavaScript. 

If my first day of August is #q90 and my August table is #q89, would I change all of the SourceID to 90 and tableID to 89?

function changeMonthPadding(sourceID, tableID) { //source and table are numbers --> 4 means q4
29
  var firstday = $("#q" + sourceID + " input").val();
30
  var tr = $("#q" + tableID + " tbody tr:first-child()");
31
  $(tr).css("width", (14*firstday) + "%");
32
  $(tr).find("td").css("width", Math.floor((100/firstday/2)*10)/10 + "%");
33
  $(tr).find("td.col0").css("width", Math.ceil((1-(1/firstday/2))*100*10)/10 + "%");
34
};

 

0 0
replied on May 12, 2020

Hi! I think I have it all set except the padding based on the first day. Can you tell what I am missing? Am I using the correct "q"? I'm guessing that I would need to do the function ChangeMonthPadding for each table/source, correct? 

 

0 0
replied on May 13, 2020 Show version history

Don't change the function, it needs to be exactly as I wrote it. The only thing you needed to add to the JavaScript are the ID numbers in the first variable, monthTableIDs (ie lines 2 to 13).

If your first day of August field is q90 and the August table is q89, line 9 should be:

  [89,90],    //Aug

 

0 0
replied on May 13, 2020

Got it! Last question. I needed to change my month-table padding to 35px otherwise it was just not next to the input field. Do you know why my 1 is left aligned and not next to the input field? Thank you so much!! 

0 0
replied on May 13, 2020

Can I see all your CSS? And did you apply the 'month-table' class to each table?

0 0
replied on May 13, 2020


#q6 label {width: 150px;}
#q6 .cf-medium {width: 200px;}
#q6 .cf-field {width: 200px;}

#q7 label {width: 150px;}
#q7 .cf-medium {width: 200px;}
#q7 .cf-field {width: 200px;}

#q6, #q7 {display:inline-block;}

#q56 label {width: 150px;}
#q56 .cf-medium {width: 200px;}
#q56 .cf-field {width: 200px;}

#q13 label {width: 150px;}
#q13 .cf-medium {width: 200px;}
#q13 .cf-field {width: 200px;}
#q56, #q13 {display:inline-block;}

#q14 label {width: 150px;}
#q14 .cf-medium {width: 200px;}
#q14 .cf-field {width: 200px;}

#q138 label {width: 150px;}
#q138 .cf-medium {width: 200px;}
#q138 .cf-field {width: 200px;}
#q14, #q138 {display:inline-block;}




.month-table .parsley-errors-list {display : none;} 
.month-table td.col0 {
  padding-left: 34px;padding-top: 5px;
 }
.month-table td {
  display: inline-block;
  width: 50%;
  text-align: right;height : 30px; line-height: 30px;

}
.month-table tr {
  float: left;
  width: 14%;height : 30px; line-height: 30px;
}
.month-table .cf-xlarge {
  width: 100%;
}

Here is my CSS. I made some formatting changes to get rid of some white space. It's getting very close. I also need to see why my SQL views are not sorting by the date. It looks like they are when I run the view, but it's not pulling into the form that way. I think I know how to fix that issue. 

 

0 0
replied on May 13, 2020

Try adding the text-align where you have the 34px padding / replacing it:

.month-table td.col0 {
  text-align: right;
  padding-top: 5px;
}

 

And FYI, this line you have:

.month-table .parsley-errors-list {display : none;}

Will hide the error that displays when it's the wrong data type, like "Must be a number." if it's a number field.

0 0
replied on May 13, 2020

This is what happens when I change that part of the CSS. It moves the label farther left. 

0 0
replied on May 13, 2020

I think it has something to do with the text alignment. I can change other formats  (padding, size, etc.) for that class and notice the changes, but the text alignment is still left aligned. 

0 0
replied on May 13, 2020

Yes, for some reason the text-align isn't applying. Try this:

.month-table td.col0 {
  text-align: right !important;
  padding-top: 5px;
}

There may be another CSS rule from the theme or something that is overwriting it.

1 0
replied on May 13, 2020

I found a workaround. I had to add this for each individually. I wouldn't even let me to #q100, #q102. Thank you so much. I really appreciate it! 

#q100  td.col0 {
  text-align: right;padding-right; 4px;
}

 

1 0
You are not allowed to follow up in this post.

Sign in to reply to this post.