Monday, November 18, 2019

Block or Disable Cut, Copy and Paste operation Using jQuery

It is required to block the cut, copy and paste operations on some of the textbox. For example, it will be better if we do not allow users to copy paste the data entered in Email and Confirm Email field in order to make the user to type themselves.

The below jQuery code will help us to do the same using preventDefault() method

Move List Items Up and Down Using jQuery

Sometimes, we require moving/scrolling the list items in the select control up and down to sort manually. This little code snippet will help us to do that using jQuery library.

Thursday, November 14, 2019

Convert JSON data to CSV

JSON Data


Output as CSV

Wednesday, October 23, 2019

Sorting by Key in An Array of object with Jquery

var data = [{ TagId: 1, TagName: "C#", }, 
{ TagId: 2, TagName: "Single Page Application", }, 
{ TagId: 3, TagName: "Visual Studio", }, 
{ TagId: 4, TagName: "Fakes", }]

var posts = [];
    posts = sortByKeyDesc(data, "TagId");


function sortByKeyDesc(array, key) {
        return array.sort(function (a, b) {
            var x = a[key]; var y = b[key];
            return ((x > y) ? -1 : ((x < y) ? 1 : 0));
        });
    }

function sortByKeyAsc(array, key) {
        return array.sort(function (a, b) {
            var x = a[key]; var y = b[key];
            return ((x < y) ? -1 : ((x > y) ? 1 : 0));
        });
    }

Tuesday, October 22, 2019

8 Must Know JavaScript Array Methods

Working with arrays in JavaScript used to be a pain with barely any support for complex array operations. Fast forward to today, though, and there are tons of amazing JavaScript array methods available to us. In this video I will be covering the 8 most important array methods in JavaScript.


const items =[
{ name: 'Bike', price:100},
{ name: 'TV', price:200},
{ name: 'Album', price:10},
{ name: 'Book', price:5},
{ name: 'Phone', price:500},
{ name: 'Computer', price:1000},
{ name: 'Keyboard', price:25}
]

// create another array with filtered items
const filteredItems = items.filter((item) => {
return item.price <= 100
})

console.log(filteredItems);



// get element and create another array 
const itemNames = items.map((item) => {
     return item.name
});

console.log(itemNames)


//find element in array
const foundItem = items.find((item) =>{
return item.name === 'Album'
})
console.log(foundItem)


//forEach 
items.forEach((item) =>{
console.log(item.price)
})


// some -- return true if found
const hasInExpensiveItem = items.some((item) =>{
   return item.price <= 100
})
console.log(hasInExpensiveItem)

// calculate with variable initiate as 0
const total = items.reduce((currentTotal, item){
    return item.price + currentTotal
},0) // 0 is the initial value of CurrentTotal

console.log(total)

// include -- if item present in array return true.. this work fine with simple array...

const items =[1, 2, 3, 4, 5]
const includesTwo = items.includes(2)
console.log (includesTwo) // return True..

const includesTwo = items.includes(6)
console.log (includesTwo) // return false..


// distinct from array


var distinct = (value, index, self) =>{
   return self.indexOf(value) === index;
}

var years = [2016, 2017, 2017, 2020, 2016, 2019]
var distinctYears = years.filter(distinct)

output : [2016, 2017, 2020, 2019]



Thanks to Web Dev Simplified and codeburst

Friday, October 18, 2019

PrintQueue.Purge Method

Code help to Purge Network/Local Print .

Dim ps = New LocalPrintServer(PrintSystemDesiredAccess.AdministrateServer)
Dim pq = New PrintQueue(ps, ps.DefaultPrintQueue.FullName, 

PrintSystemDesiredAccess.AdministratePrinter)

If pq.NumberOfJobs > 0 Then
pq.Purge()
End If

Tuesday, August 20, 2019

Convert JSON to CSV Format

This is common thing now a days we have all data in JSON and when we have to sent it as a csv file we need to convert. Following code found stackoverflow.com, and credit goes to developer. I am putting into my blog for easy access and reference to my blog reader.

//logData is the JSON object having json data, rest of the code do the job.

const items = logData
const replacer = (key, value) => value === null ? '' : value 
const header = Object.keys(items[0])
let csv = items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
csv.unshift(header.join(','))
csv = csv.join('\r\n')

console.log(csv)