Tuesday, June 7, 2011

How to check an array item exist or not in the json array based on the item property value in javascript

In my current project i am working more with jquery and json.In this project i had a requirement to find the json object exist or not in the json array based on the item property value.I had googled a bit and did not find the answer.

Then i wrote a prototype method to find the json object exist or not.

Array.prototype.containsJsonObj = function(name,value) {
var isExists=false;
$.each(this,function(){
if(this[name]==value){
isExists=true;
return false;
}});
return isExists;
};


Below is the json array

var jsonArray=[{"firstName":"John","lastName":"Doe","age": 23 },
{ "firstName":"Mary","lastName" :"Smith","age": 32 }]

we can check whether the array item exist or not as shown below

if(jsonArray.containsJsonObj("firstName","John"))
{
//if it exists enter into this loop
}
else
{
//if it does not exist enter into this loop
}

How to get checkboxlist selected item values on client side using javascript

Sometimes, you may need to find out server side checkboxlist selected item values on client side.By default checkboxlist does not send item value to the client.This problem can be solved by assigning values to each item of checkboxlist in page load event as shown below

protected void Page_Load(object sender, EventArgs e)
{
foreach (ListItem li in cblListItems.Items)
li.Attributes.Add("mainValue", li.Value);
}

Then call the below javascript function on click of checkboxlist.

function GetCheckBoxListValues(chkBoxID)
{
var chkBox = document.getElementById(chkBoxID);
var options = chkBox.getElementsByTagName('input');
var listOfSpans = chkBox.getElementsByTagName('span');
for (var i = 0; i < options.length; i++)
{
if(options[i].checked)
{
alert(listOfSpans[i].attributes["mainvalue"].value);
}
}
}

How To Sort ListItems In JavaScript

First Declare ListItemArray Varaible:-

var myOptions = [];

//Fill ListItem Array With Some Data

// copy options into an array
function SortListItems(optionText,optionValue) //where optionText and optionValue are array of texts and values
{ myOptions.clear();//clear array if any data present

// copy options into an array
for (var i=0; i
{ myOptions[i] ={ optText:optionText[i], optValue:optionValue[i]}; }

// sort array myOptions.sort(SortFuncAsc);}

// sort functionfunction SortFuncAsc(record1, record2){ var value1 = record1.optText.toLowerCase(); var value2 = record2.optText.toLowerCase();; if (value1 > value2) return(1); if (value1 < value2) return(-1); return(0);
}

Bandwidth detection with javascript

In one of my current projects, I came across a scenario to play a video based on the users downloading bandwidth. The detection should be done with javascript. There is a technique, to load an image of known size and calculating the bandwidth on basis of time taken to load that image. Though this technique is not 100% reliable, this will give an approximate estimate of bandwidth.

Here is the javascript code to find users bandwidth:

var userBandwidth = 0;
var startTime;
var endTime;
var imgSize = 39842;
var loadTimeInSec;

function GetUserBandwidth() {
var testImage = new Image();
testImage.src = "bwtest.jpg";
startTime = (new Date()).getTime();
testImage.onload = CreateDelegate(testImage, DoneWithTest);
}

function DoneWithTest() {
endTime = (new Date()).getTime();
loadTimeInSec = (endTime - startTime) / 1000;
userBandwidth = (imgSize / loadTimeInSec) / 1024;
}

Here we are loading an image of size 38Kb and added a delegate on image loaded event. In the call back function, we calculate end time, with that we can calculate the bandwidth of the user.

Check my previous post to add delegate in javascript.

Delegate in Javascript

Before going to the topic, lets know brief about “Delegate”:
A delegate is like a function pointer, which stores reference of a method. It specifies a method to call and optionally an object to call the method on. They are used, among other things, to implement callbacks and event listeners.

We can create and define delegates in Javascript to perform some specific tasks on an object. The following is the code to define delegate.


function CreateDelegate(contextObject, delegateMethod) {
return function() {
return delegateMethod.apply(contextObject, arguments);
}
}


Let us take a simple example and use the delegate in that. Here is the scenario, there is a large image to be loaded on a web page and we need to display the height and width of that image after loading. Delegate method will be very handy in this situation.


var image = new Image();
image.src = "img.jpg";
image.name = image.id = "img1";
image.onload = CreateDelegate(image, ImageLoaded);

function ImageLoaded() {
alert(this.width + " X " + this.height);
}


We are creating a delegate on image loaded event, which will call method ‘ImageLoaded’. This delegate automatically pass the object on which the delegate is defined, to the method. We can refer the actual image object using ‘this’ in the method.

Monday, June 6, 2011

One of the most usefull link for learing Silver Light


http://samples.msdn.microsoft.com/Silverlight/SampleBrowser/#/?sref=animation_ovw_intro

Update data in one table with data from another table

This blog post illustrates how to update more than one column in a table with values from columns in another table and explains how to do it in the three RDBMS that we support.


Table Structures and values:


TableA has four columns: a, b, c, d (a is the primary key column)

TableB has five columns: a1, b1, c1, d1, e1 (a1 and b1 together constitute the primary key for this table)


The foreign key relationship between the two tables is based on A.a = B.a1


The data in these 2 tables is as follows:

I.    TableA

a    b    c    d

1    x    y    z

2    a    b    c

3    t    x    z


II.    TableB

a1    b1    c1    d1    e1

1    x1    y1    z1    40

2    a1    b1    c1    50


The requirement is to write a SQL to update columns b, c and d in TableA from the columns b1, c1 and d1 from TableB where-ever the join condition satisfies and e1 > 40 in TABLEB.


Oracle:


UPDATE TABLEA

SET (b, c, d) = (SELECT b1, c1, d1 from TABLEB WHERE TABLEB.a1 = TABLEA.a and TABLEB.e1 > 40)

WHERE EXISTS (SELECT 1 from TABLEB WHERE TABLEB.a1 = TABLEA.a and TABLEB.e1 > 40)

/


Results after the update:


a    b    c    d

————————————

1     x          y           z

2     a1        b1         c1

3     t           x           z


SQL Server:


UPDATE TABLEA

SET     b = TABLEB.b1,

c = TABLEB.c1,

d = TABLEB.d1

FROM TABLEA, TABLEB

WHERE TABLEA.a = TABLEB.a1

AND TABLEB.e1 > 40

GO


Note: This is an extension in SQL Server i.e. the FROM clause – it does make it simple to understand and is a nice feature.


Results after the update:


a    b    c    d

————————————

1     x          y           z

2     a1        b1         c1

3     t           x           z


DB2 LUW:


–Same as Oracle–


UPDATE TABLEA

SET (b, c, d) = (SELECT b1, c1, d1 from TABLEB WHERE TABLEB.a1 = TABLEA.a and TABLEB.e1 > 40)

WHERE EXISTS (SELECT 1 from TABLEB WHERE TABLEB.a1 = TABLEA.a and TABLEB.e1 > 40);


Results after the update:


a    b    c    d

————————————

1     x          y           z

2     a1        b1         c1

3     t           x           z


NOTE:


It is very important to make sure that your where clause for the update statement is correct since that is what identifies the records that the update statement is going to qualify and do the update upon.  If it is incorrect, then you can get wrong results.  The reason I am mentioning this is because I have seen people write wrong where clauses and then wondering what went wrong because they specified the correct condition in the SET clause.


In the above example, if the Where condition was omitted, the other record’s columns would be updated to NULL value and this will be the final result set:


a    b    c    d

————————————

1     Null      Null      Null

2     a1        b1         c1

3     Null     Null      Null