Tuesday, April 21, 2015

Jquery iterate through json array with for loop

var arr = [{ "id": "10", "class": "child-of-9" }, { "id": "11", "classd": "child-of-10" }];

for (var i = 0; i < arr.length; i++) {

var obj = arr[i];



console.log(obj);

for (var key in obj) {

var attrName = key;

var attrValue = obj[key];

console.log('attrName -' + attrName + '- attrValue -' + attrValue);



}

}


Jquery iterate through json array with foreach loop


            var json = [
                   { "id": "1", "tagName": "apple" },
                   { "id": "2", "tagName": "orange" },
                   { "id": "3", "tagName": "banana" },
                   { "id": "4", "tagName": "watermelon" },
                   { "id": "5", "tagName": "pineapple" }
            ];

            $.each(json, function (index, value) {

                console.log('value.id - ' + value.id + 'value.tagName -' + value.tagName);
             
            });

jquery calculate percentage of array element and push percentage into other array

 var array = [20, 30, 40, 50, 60];
            var newarray = [];
            $.each(array, function (index, value) {
                newarray.push(value / 100);
            });

            console.log(newarray);

jquery calculate sum of array elements

  var sum = 0;
            var array = [1, 2, 3, 4, 5];

            $.each(array, function (index, value) {
                sum = sum + value;
            });

            alert(sum);

Friday, April 17, 2015

sql Adding an identity to an existing column

You can't alter the existing columns for identity.
You have 2 options,
  1. Create a new table with identity & drop the existing table
  2. Create a new column with identity & drop the existing column
Approach 1. (New table) Here you can retain the existing data values on the newly created identity column.
CREATE TABLE dbo.Tmp_Names
    (
      Id int NOT NULL
             IDENTITY(1, 1),
      Name varchar(50) NULL
    )
ON  [PRIMARY]
go

SET IDENTITY_INSERT dbo.Tmp_Names ON
go

IF EXISTS ( SELECT  *
            FROM    dbo.Names ) 
    INSERT  INTO dbo.Tmp_Names ( Id, Name )
            SELECT  Id,
                    Name
            FROM    dbo.Names TABLOCKX
go

SET IDENTITY_INSERT dbo.Tmp_Names OFF
go

DROP TABLE dbo.Names
go

Exec sp_rename 'Tmp_Names', 'Names'
Approach 2 (New column) You can’t retain the existing data values on the newly created identity column, The identity column will hold the sequence of number.
Alter Table Names
Add Id_new Int Identity(1, 1)
Go

Alter Table Names Drop Column ID
Go

Exec sp_rename 'Names.Id_new', 'ID', 'Column'

asp.net dropdownlist set first value using jquery

$('#<%=ddl.ClientID%>').prop('selectedIndex', 0);

html tooltip hover

<input border='0' type='image' style='border-width:0px;' title='Edit' id='imgEditUser'>


---title='Edit'