Sunday 28 December 2014

Formatting date in Linq-to-Entities query causes exception

invoices.Select(i => new 
{
    FormattedDate = (     EntityFunctions.Right(String.Concat(" ", SqlFunctions.StringConvert((double?) SqlFunctions.DatePart("dd", i.DocumentDate))), 2)
                        + "/"
                        + EntityFunctions.Right(String.Concat(" ",SqlFunctions.StringConvert((double?) SqlFunctions.DatePart("mm", i.DocumentDate))), 2)
                        + "/"
                        + EntityFunctions.Right(SqlFunctions.StringConvert((double?) SqlFunctions.DatePart("yyyy", i.DocumentDate)), 4)
                       ).Replace(" ", "0")
}

Friday 27 June 2014

Convert returned JSON Object Properties to (lower first) camelCase

I have JSON returned from an API like so:
Contacts: [{ GivenName: "Matt", FamilyName:"Berry" }]
To keep this consitent with my code style (camelCase - lower case first letter) I want to transform the array to produce the following:
 contacts: [{ givenName: "Matt", familyName:"Berry" }]
Whats the easiest/best way to do this? create a new Contact object and iterate over all the conatacts in the returned array?


I needed a generic method that accepted an array or object. This is what I'm using (I borrowed KyorCode's firstToLower() implementation):


function convertKeysToCamelCase(obj) {
    if (!obj || typeof obj !== "object") return null;

    if (obj instanceof Array) {
        return $.map(obj, function(value) {
            return convertKeysToCamelCase(value);
        });
    }

    var newObj = {};
    $.each(obj, function(key, value) {
        key = key.charAt(0).toLowerCase() + key.slice(1);
        newObj[key] = value;
    });

    return newObj;
};
Example calls:
var contact = { GivenName: "Matt", FamilyName:"Berry" };

console.log(convertKeysToCamelCase(contact));
// logs: Object { givenName="Matt", familyName="Berry"}

console.log(convertKeysToCamelCase([contact]));
// logs: [Object { givenName="Matt", familyName="Berry"}]

console.log(convertKeysToCamelCase("string"));
// logs: null

console.log(contact);
// logs: Object { GivenName="Matt", FamilyName="Berry"}
 

Thursday 22 May 2014

Give custom validation message in HTML5

Custom Message in HTML5

Generally if you are just giving required in input it will give its own validation message in html5. But if you want to give your custom message then do this.

<input id="UserID" type="text" required="required" oninvalid="this.setCustomValidity('Witinnovation')">

Thursday 17 April 2014

Get added and deleted items from array

This one is only for int array Here i am going to show how to get value of add and deleted items from defined array.


        var oldbrd = $scope.selectedDiagnosis.sort();
        var newbrd = $scope.RefferalView.Diagnosis.sort();

        var additem = [];
        var deleteitem = [];
        //var index;

        angular.forEach(oldbrd, function (value) {
            if (newbrd.indexOf(value) == -1) {
                deleteitem.push(value);
            }
        });

        angular.forEach(newbrd, function (value) {
            if (oldbrd.indexOf(value) == -1) {
                additem.push(value);
            }
        });

        $scope.AddedDiagnosisCode = additem;
        $scope.DeletedDiagnosisCode = deleteitem;
        alert('added =>: ' + additem + "_________" + 'deleted =>: ' + deleteitem);