Sorting in Javascript is quite easy.
For example, I have an array call "my_object_array" where "my_object" is an object that contains "customer_code" and "customer_name" properties. To sort the data by customer_code, just run the following code:
var f = 'customer_code';
my_object_array.sort(function (a, b) {
return a[f] - b[f];
});
To sort the data in descending order, you just need to swap the "a" and "b".
my_object_array.sort(function (a, b) {
return b[f] - a[f];
});
Cool huh?
Tuesday, 19 March 2013
Friday, 1 March 2013
Submitting data in JSON format to the website
If your web page contains quite a number of fields to be posted to the web server, you may submit the data in JSON format (through JQuery). In this way, you may get rid of writing lots of query parameter in the URL.
Below is the sample code posted by someone.
Sample code
http://stackoverflow.com/questions/5570747/jquery-posting-json
JSON Object
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify
Below is the sample code posted by someone.
Sample code
http://stackoverflow.com/questions/5570747/jquery-posting-json
JSON Object
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify
Monday, 21 January 2013
Javascript parameter is optional
In Javascript, the parameter in the function is optional. If the caller did not pass in the parameter value (as shown in test1 function), then, the value of "a" is "undefined". So, each time before you are using the incoming parameter value, make sure that you check it before using it.
You may copy the following tags into any html file and then debug it in Visual Studio.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function check(a) {
var i = 0;
if (a) {
i = a;
}
else {
i = -1;
}
}
function test1() {
check();
}
function test2() {
check(12345);
}
function test3() {
check(null);
}
</script>
</head>
<body>
<input type="button" name="name" value="test1" onclick="return test1();" />
<input type="button" name="name" value="test2" onclick="return test2();" />
<input type="button" name="name" value="test3" onclick="return test3();" />
</body>
</html>
You may copy the following tags into any html file and then debug it in Visual Studio.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function check(a) {
var i = 0;
if (a) {
i = a;
}
else {
i = -1;
}
}
function test1() {
check();
}
function test2() {
check(12345);
}
function test3() {
check(null);
}
</script>
</head>
<body>
<input type="button" name="name" value="test1" onclick="return test1();" />
<input type="button" name="name" value="test2" onclick="return test2();" />
<input type="button" name="name" value="test3" onclick="return test3();" />
</body>
</html>
Friday, 11 January 2013
Clearing the contents in an element
Execute the following using JQuery, it will clear all the contents (including tags) within "div1":
$('#div1').remove().end();
$('#div1').remove().end();
Wednesday, 2 January 2013
Get the input by ID
Let's say you have 3 buttons which starts with "f1" and below is the HTML tags:
<input type="button" id="f1.1" value="test1" />
<input type="button" id="f1.2" value="test2" />
<input type="button" id="f1.3" value="test3" />
When you run the following JQuery code, you will get different result:
var v1 = $('#f1.1'); => this returns zero match.
var v2 = $('input[id^="f1"]'); => this returns 3 matches.
var v3 = $('input[id="f1.2"]'); => this returns 1 match (i.e., the second button).
If you are using "#" in the selector, the result is either 0 or 1 (it's always stop after it found the first one).
For more details, check out the following reference:
http://api.jquery.com/category/selectors/
<input type="button" id="f1.1" value="test1" />
<input type="button" id="f1.2" value="test2" />
<input type="button" id="f1.3" value="test3" />
When you run the following JQuery code, you will get different result:
var v1 = $('#f1.1'); => this returns zero match.
var v2 = $('input[id^="f1"]'); => this returns 3 matches.
var v3 = $('input[id="f1.2"]'); => this returns 1 match (i.e., the second button).
If you are using "#" in the selector, the result is either 0 or 1 (it's always stop after it found the first one).
For more details, check out the following reference:
http://api.jquery.com/category/selectors/
Tuesday, 25 December 2012
Detecting double click in an element
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 report. After I googled for the solution, I found the following solution in stackoverflow.com
Below is the code:
// Someone has created the solution as a delegate to ease the implementation.
$.singleDoubleClick = function (singleClk, doubleClk) {
return (function () {
var alreadyclicked = false;
var alreadyclickedTimeout;
return function (e) {
if (alreadyclicked) {
// double
alreadyclicked = false;
alreadyclickedTimeout && clearTimeout(alreadyclickedTimeout);
doubleClk && doubleClk(e);
} else {
// single
alreadyclicked = true;
alreadyclickedTimeout = setTimeout(function () {
alreadyclicked = false;
singleClk && singleClk(e);
}, 300);
}
}
})();
}
// call this in the document ready:
$('.profile').bind('click', $.singleDoubleClick(confirmSelection, createReport));
Below is the code:
// Someone has created the solution as a delegate to ease the implementation.
$.singleDoubleClick = function (singleClk, doubleClk) {
return (function () {
var alreadyclicked = false;
var alreadyclickedTimeout;
return function (e) {
if (alreadyclicked) {
// double
alreadyclicked = false;
alreadyclickedTimeout && clearTimeout(alreadyclickedTimeout);
doubleClk && doubleClk(e);
} else {
// single
alreadyclicked = true;
alreadyclickedTimeout = setTimeout(function () {
alreadyclicked = false;
singleClk && singleClk(e);
}, 300);
}
}
})();
}
// call this in the document ready:
$('.profile').bind('click', $.singleDoubleClick(confirmSelection, createReport));
Friday, 23 November 2012
CSS progress spinner generator
This is a cool site which help you to generate a progress spinner only the fly:
http://cssload.net/
http://cssload.net/
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...