Tuesday, 19 March 2013

Sorting data using JavaScript

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?

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

We are moving

We are moving this blog to our new blog site: https://ciysys.com/blog/nodejs.htm