Vertical scrollbar causing column width different between header and cell in spreadsheet. This is a common issue when the HTML page that display on a computer. Whereas, it won't have issue if it is on Android or iOS because the scrollbar is floating on top of the content.
To explain this issue, let says we have a spreadsheet (or "table" view) built with DIV which looks like below:
<style>
.header {
display:flex;
flex-direction:row;
}
.item {
display:flex;
flex-direction:row;
}
.c1 {
width:50%;
}
.item-list {
overflow: hidden;
overflow-y: scroll;
height: 80px;
}
</style>
<div class="my-table">
<div class="header">
<div class="c1">Date</div>
<div class="c1">Ref #</div>
</div>
<div class="item-list">
<div class="item">
<div class="c1">1 Sep 2019</div>
<div class="c1">0001</div>
</div>
<div class="item">
<div class="c1">1 Sep 2019</div>
<div class="c1">0002</div>
</div>
<div class="item">
<div class="c1">1 Sep 2019</div>
<div class="c1">0003</div>
</div>
<div class="item">
<div class="c1">1 Sep 2019</div>
<div class="c1">0004</div>
</div>
<div class="item">
<div class="c1">1 Sep 2019</div>
<div class="c1">0005</div>
</div>
</div>
</div>
With the CSS for item-list, a vertical scrollbar will appear once the number of items increased.
The problem with this CSS is that the date and reference number column in the header will have a different width if you compared it with the column in the items.
To solve this, you need to use this style:
(1) you have to use "sticky:
.header{
position: sticky;
top: 0;
left: 0;
background-color: white;
}
(2) Move the overflow attribute for "item-list" to "my-table":
.my-table {
position:relative;
overflow: hidden;
overflow-y: scroll;
height: 80px;
}
And that solves the column width issue when there is a vertical scrollbar.
Friday, 13 September 2019
Monday, 27 May 2019
Server-sent event
Server sent event supports the server push from server to browser only.
In Javascript
var sse = null;
function runNow() {
if (!sse) {
sse = new EventSource('/sse_stream');
sse.addEventListener('message',
function (e) {
document.getElementById('lbl1').innerText = e.data;
});
}
}
function stop() {
if (sse) {
sse.close();
sse = null;
}
}
Reference:
https://www.html5rocks.com/en/tutorials/eventsource/basics/
https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
In Javascript
var sse = null;
function runNow() {
if (!sse) {
sse = new EventSource('/sse_stream');
sse.addEventListener('message',
function (e) {
document.getElementById('lbl1').innerText = e.data;
});
}
}
function stop() {
if (sse) {
sse.close();
sse = null;
}
}
Reference:
https://www.html5rocks.com/en/tutorials/eventsource/basics/
https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
Web worker
Summary of web worker
The JavaScript to that create a web worker
This script will be added to the HTML page like a normal JS file.
var w;
var lbl1;
function onStart() {
if (w) {
return;
}
w = new Worker('demo_worker.js');
lbl1 = $('#lbl1');
w.onmessage = function (e) {
if (e.data.t == 'number') {
lbl1.text('running value=' + e.data.i);
}
else {
console.log(e.data)
}
}
}
function onStop() {
if (w != null) {
w.terminate();
w = null;
}
}
function onSayHelo() {
if (w) {
w.postMessage('helo from tab...' + (new Date()));
}
}
The web worker Javascript file "demo_worker.js"
var i = 0;
function timedCount() {
i = i + 1;
postMessage({
t: 'number',
i: '<b>'+ i + '</b>',
ts: new Date()
});
setTimeout("timedCount()", 1000);
}
function testAjax() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function (e) {
if (xhr.readyState == 4 && xhr.status == 200) {
// upon loaded succesfully, pass it to the HTML page.
postMessage({
t: 'data',
data: xhr.response
});
}
}
xhr.open('get', 'test-data.txt');
xhr.send();
}
// message from the HTML page.
self.onmessage = function (e) {
console.log('worker...', e.data)
}
timedCount();
setTimeout(testAjax, 3000)
- Every web worker is a thread on it's own!! Instantiate too many worker instances might degrade your computer performance.
- The worker will not be able to access the HTML page.
- All data exchange/communication must be done through postMessage().
- With web worker, the communication between the HTML/JS and the worker will be more complex.
- Good thing is that process in the web worker won't affect the HTML page update/redraw.
The JavaScript to that create a web worker
This script will be added to the HTML page like a normal JS file.
var w;
var lbl1;
function onStart() {
if (w) {
return;
}
w = new Worker('demo_worker.js');
lbl1 = $('#lbl1');
w.onmessage = function (e) {
if (e.data.t == 'number') {
lbl1.text('running value=' + e.data.i);
}
else {
console.log(e.data)
}
}
}
function onStop() {
if (w != null) {
w.terminate();
w = null;
}
}
function onSayHelo() {
if (w) {
w.postMessage('helo from tab...' + (new Date()));
}
}
The web worker Javascript file "demo_worker.js"
- This file will run in it's own thread.
- All data exchange must be done through postMessage().
- The worker is able to execute AJAX call.
- The worker will not be able to access the HTML page.
var i = 0;
function timedCount() {
i = i + 1;
postMessage({
t: 'number',
i: '<b>'+ i + '</b>',
ts: new Date()
});
setTimeout("timedCount()", 1000);
}
function testAjax() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function (e) {
if (xhr.readyState == 4 && xhr.status == 200) {
// upon loaded succesfully, pass it to the HTML page.
postMessage({
t: 'data',
data: xhr.response
});
}
}
xhr.open('get', 'test-data.txt');
xhr.send();
}
// message from the HTML page.
self.onmessage = function (e) {
console.log('worker...', e.data)
}
timedCount();
setTimeout(testAjax, 3000)
Monday, 29 October 2018
Print the call stack in console
Here's the code in printing the call stack in console:
var stack = new Error().stack;
console.log(stack);
var stack = new Error().stack;
console.log(stack);
Tuesday, 21 November 2017
CSS fill pattern
To create the fill pattern image, check out the following website:
http://www.patternify.com/
/*dots*/
.inactive_cell2 {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAYAAACp8Z5+AAAAEklEQVQImWNgYGD4z0AswK4SAFXuAf8EPy+xAAAAAElFTkSuQmCC");
background-repeat: repeat;
}
To create strips without using any image data,
https://css-tricks.com/stripes-css/
/*stripes*/
.inactive_cell {
background: repeating-linear-gradient( 48deg, #fefefe, #e0e0e1 5px, #eaeaea 5px, #eaeaea 10px );
}
http://www.patternify.com/
/*dots*/
.inactive_cell2 {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAYAAACp8Z5+AAAAEklEQVQImWNgYGD4z0AswK4SAFXuAf8EPy+xAAAAAElFTkSuQmCC");
background-repeat: repeat;
}
To create strips without using any image data,
https://css-tricks.com/stripes-css/
/*stripes*/
.inactive_cell {
background: repeating-linear-gradient( 48deg, #fefefe, #e0e0e1 5px, #eaeaea 5px, #eaeaea 10px );
}
Friday, 17 November 2017
Simple pie chart & doughnut chart with SVG + CSS
You can draw a simple pie chart & doughnut chart with SVG + CSS. This is quite simple and straight forward. But the limitation is that it is able to show 2 sections only.
To change the value of the section, look for stroke-dasharray and change the first value.
To change the value of the section, look for stroke-dasharray and change the first value.
.pie2 {
fill: yellow;
stroke: green;
/* max=50 (radius x 2) */
stroke-width: 50;
/* 158 = (radius x 2) x 3.142 = 25 x 2 x 3.142 */
stroke-dasharray: 40 158;
}
.chart2 {
background-color: yellow;
border-radius: 50%;
-webkit-transform: rotate(-90deg);
transform: rotate(-90deg);
}
<svg width="100" height="100" class="chart2">
<circle r="25" cx="50" cy="50" class="pie2"/>
</svg>
To draw a doughnut chart,
.doghnut {
fill: yellow;
stroke: green;
stroke-width: 20;
stroke-dasharray: 180 251;
}
.doghnut_chart {
background-color: yellow;
border-radius: 50%;
-webkit-transform: rotate(-90deg);
transform: rotate(-90deg);
}
<svg width="100" height="100" class="doghnut_chart">
<circle r="40" cx="50" cy="50" class="doghnut"/>
</svg>
Wednesday, 15 November 2017
Javascript that prevents double click on submit button
This is what happening:
var _prev_save_click_ts = null;
function beforeSubmit() {
if (_prev_save_click_ts != null) {
var ts = new Date();
if (ts - _prev_save_click_ts < 500) {
// user is double clicking.
return false;
}
}
_prev_save_click_ts = new Date();
return true;
}
- The user loves "double clicking" on any button even though single click will trigger the action.
- The Internet connection slows down and the user thought that he did not click on the correct button. So, he decided to click it again even though the waiting time is lesser than 1 second (?!).
var _prev_save_click_ts = null;
function beforeSubmit() {
if (_prev_save_click_ts != null) {
var ts = new Date();
if (ts - _prev_save_click_ts < 500) {
// user is double clicking.
return false;
}
}
_prev_save_click_ts = new Date();
return true;
}
Subscribe to:
Posts (Atom)
We are moving
We are moving this blog to our new blog site: https://ciysys.com/blog/nodejs.htm
-
Overview Usually, we start developing a new system with designing database and then writing codes. If you are not a fan of ORM, you will h...
-
I'm working on an attendance audit screen which allows the user to single click to confirm the selection and double click to create a re...
-
You cannot achieve the fixed row and column with HTML + CSS. You need the Javascript as well. Below is the HTML, CSS and Javasript th...