Showing posts with label Jquery. Show all posts
Showing posts with label Jquery. Show all posts

Thursday, October 12, 2017

Getting started with NodeJs,NPM,VsCode,Typescript and webpack

It is a quite reading if you are coming from a .net development experience to work on client side programming , below are few steps which can be used to get started with the modern web development stack .

Basically you would need a Laptop or desktop as usual :) windows or IOS license is all you need 
and then 

1)Code editor : you can use notepad++ , atom , sublime , I personally use VSCode editor (https://code.visualstudio.com/) Its completely freeeeeeeee !!
2)Git : If you need to have a repository and manage versions , you can use VSTS too . (https://git-scm.com/)
3)NodeJs :Platform used to build and test application 
4)NPM : .net developers can compare this to NugetPackage manager .this helps us to download and install the required package which will be used in the web development 
5)Typescript : You can refer this for installation , I would prefer global installation  (https://www.npmjs.com/package/typescript)
6)Task runners: Which will help to automate the development life cycle , there are many Gulp,Grunt,Webpack etc , I have used Webpack in the below sample . The help to automate compilation , bundling of the files even magnification of JS files .

Getting started : 

1)Open VS code
2)cmd (can be any terminal) -- > npm init (Package.json will be created)
3)cmd -->  npm install --save-dev typescript (Creates the local typescript files and a compile component tsc)
4)Create folder to add a  .ts file (custom coding files)
5)Create tsconfig.json and provide entries (Files to be included for transpiling)

for Webpack
1)cmd -- >npm install --save-dev ts-loader (Installs ts loader locally -this helps webpack to compile ts files)
2)cmd --> npm install --save-dev webpack (Installs webpack loacally)
3)Add "webpack.config.js" (This will be used for compiling and build )
4)run "node_modules\.bin\webpack" (provides the output of the build which is a js file which can be directly used in HTML)


Debugging
1)Add line "debugger" in any ts file
2)Open up the html in debug mode , this enters the debug mode but the code is in transpiled state , to get the code in typescript format , perform below steps
3)Open "tsConfig.json" --> add property "ComplierOptions" -->"sourceMap" :true
4)Open "Webpack.config.js --> inside exports add -->devtool: 'source-map',
5)This would load the ts file in the browser


Http-server
1)Cmd -- > Install --save-dev http-server (to install the http server locally)
Auto build through webapcak
2)webpack.config.js --> add property --> watch:true ;
3)To test or run an application in local server use cmd --> node_modules\.bin\http-server

Jquery
1)cmd --> npm install --save jquery (This will install the Jquery package globally- as this package has to be released to browser)
2)run as admin Cmd -- >npm install --save-dev @types/jquery (This will install the Jquery declaration file , this is one of the way to work with Jquery)
3)May get a error on Jquery , if so add the property "lib": ["dom","es5","es2015.iterable"]

4)cmd -- >node_modules\.bin\webpack should give you the bundled file including the Jquery library .

Get started with the project from repository
1) cmd -- > npm install  (This would install all the required package into node modules by referring the package.json)


-------------------------------------------------------------------------------------------------------------------------
tsconfig.json

{
"files": [
"./ts-src/alert.ts"
],
"compilerOptions": {
"lib": [
"dom",
"es5",
"es2015.iterable"
],
"sourceMap": true,
"outDir": "./dist1/",
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"allowJs": true
}

}

package.json


{
"name": "typescriptwithjquery",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/jquery": "^3.2.12",
"http-server": "^0.10.0",
"ts-loader": "^2.3.7",
"typescript": "^2.5.2",
"webpack": "^3.5.6"
},
"dependencies": {
"jquery": "^3.2.1"
}
}

webpack.config.js


const path = require('path');

module.exports = {
devtool: 'source-map',//only to debug
entry: './ts-src/alert.ts',
module: {
rules: [{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
}]
},
resolve: {
extensions: [".tsx", ".ts", ".js"]
},
//watch:true,
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
}
};

alert.ts

import news from './sample'
import * as $ from 'jquery'
/*
window.onload = function(){
var a1 = new news();
a1.msg();
}
*/

$(function(){
$('#btn1').on("click",function(){
var a = new news();
a.msg();
});

});

sample.ts
import * as $ from 'jquery'

export default class message{
msg(){
$("#btn1").html('Thilosh');
alert('hi');
}
}

index.html


<html>
<head>
<script src="dist/bundle.js" ></script>
</head>
<body>
<button id="btn1" >click here</button>

</body>
</html>


Note:

--save-dev  will install the packages locally in the loacl project 
-g  will install the package global 

Friday, November 27, 2015

SharePoint Migration Lookup error

There was site in SharePoint 2010 and I was planning to move it to SharePoint 2013 version by performing a Content db migration .

The site had a list which consists of lookup columns ,these were lookup to other list.But the point here was that these columns were hidden while it was created through element.xml in the solution .The list was working fine in SharePoint 2010 ,But when I migrated the 2010 site to SharePoint 2013 the link of the lookup is lost and the data is been displayed as a text field Eg: "#2;lookupValue" etc .
This was creating few problems in my solution due to mismatch in the values of the list itms .I had to fix the lookup column .Below is the resolution to this migration error



I placed the below code in Content Editor WebPart and made the field readable,This changed the column back as lookup .Later you can change the column to readonly =true


<content id="Main" contentplaceholderid="PlaceHolderMain" runat="server"></content><script language="ecmascript" type="text/ecmascript">
        var fieldCollection;
        var field;
        var list;
        function UpdateField() {
            var clientContext = SP.ClientContext.get_current();
            if (clientContext != undefined && clientContext != null) {
                var webSite = clientContext.get_web();
                this.list = webSite.get_lists().getByTitle("ListA");
                this.fieldCollection = list.get_fields();
                this.field = fieldCollection.getByTitle("Column1");
                this.field.set_readOnlyField(false);
                this.field.update();

            clientContext.load(this.fieldCollection);
            clientContext.load(this.field);
                clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));
            }
        }
        function OnLoadSuccess(sender, args) {
            alert("Field deleted successfully.");
        }
        function OnLoadFailed(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        }
</script><input type="button" id="btnUpdateField" onclick="UpdateField()" value="Change ReadOnly"/> 

Jquery function after delay


Some times functions under the method " $(document).ready(function()" doesnot get called .You can use the delay as below and call the events.

setTimeout(function()
{
$(document).ready(function(){
$("#downloads").click(function (e) {

                alert('test');

            });
});},1000);

Friday, September 20, 2013

Visual Webpart on Text Box Change event Append the Text box data with Check Box list and Drop down Selected Value

Below is the code in which on you type in something on text box and on tab event(On change) event of text box the slected values from Check Box list and drop down is appended with text and displayed in Multiline text box control


In your ascx page use below code


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>


<script type=text/javascript>
    function AutoFill() {
       
        var chkSelectedLang = new Array();
        var i = 0;
        $('#<%=chkList.ClientID %> input[type=checkbox]:checked').map(function () {
         
            chkSelectedLang[i] = this.value;
            i = i + 1;

        });
       
        //var string1 = "";
        //for (var i = 0; i < myLanguage.length; i++) {

        //    string1 += myLanguage[i] + ";";
        //}       
        //alert(string1);       
        //alert(drop);
        var publicationNo = "";
        var materialNo = $("#txtMaterialNo").val();
        var ddlSelectedLang = $("#ddlLang").val();
        publicationNo = materialNo + ddlSelectedLang + ',' + '\n';
        for (var i = 0; i < chkSelectedLang.length; i++) {
            publicationNo += materialNo + chkSelectedLang[i] + ',' + '\n';
        }
        $("#txtMultiLine").val(publicationNo);
    }
</script>




<div style="border:1px solid black">
    <asp:CheckBoxList ID="chkList" runat="server">
        <asp:ListItem Value="I">India</asp:ListItem>
        <asp:ListItem Value="U">USA</asp:ListItem>
        <asp:ListItem Value="E">England</asp:ListItem>
    </asp:CheckBoxList>

</div>

<div style="border:1px solid green">
    <asp:DropDownList ID="ddlLang" ClientIDMode="Static" runat="server">
        <asp:ListItem Value="I">India</asp:ListItem>
        <asp:ListItem Value="U">USA</asp:ListItem>
        <asp:ListItem Value="E">England</asp:ListItem>
    </asp:DropDownList>
</div>

<div style="border:1px solid red">
<span>Material No</span><asp:TextBox ID="txtMaterialNo" ClientIDMode="Static" runat="server" onChange="AutoFill()" ></asp:TextBox>
<asp:TextBox ID="txtMultiLine" ReadOnly="true" ClientIDMode="Static" runat="server" TextMode="MultiLine"></asp:TextBox>   
</div>


Also Find the demo at the below link
Thilosh-Fiddle