Showing posts with label angularJS. Show all posts
Showing posts with label angularJS. Show all posts

angular ng grid not displaying data when there is a special character in column name

In this post am going to explain how to show data in ng-grid when there is a special character in column name.

Assume you have following columnDefs in gridOptions

columnDefs: [{
      name: "parent.name",
      displayName: 'Name',
      type:'string'

    }, {
      name: 'lastName',
      displayName: 'Surname'
    }]
  };

As you see, our first column has name as parent.name. It has dot ('.') in it's name. So if you use this columnDefs in your ng-grid it will not show data in that column as the name has special characters in it. To make it work you need to add following flag to your gridOptions

gridOptions.flatEntityAccess = true;

So your final gridOptions will look something like below

  $scope.gridOptions = {

    columnDefs: [{
      name: "parent.name",
      displayName: 'Name',
      type:'string'

    }, {
      name: 'lastName',
      displayName: 'Surname'
    }]
  };

  $scope.gridOptions.data = [{
    "parent.name": "John",
    "lastName": "Cena"
  }];

 $scope.gridOptions.flatEntityAccess = true;

Check the following plunkr for working example.

ng-grid show data when there is a special character in column name

This is how we can show data in ng-grid even if the column name is having special character in it.

For more posts on angularJs refer: AgngularJs

Read more...

ng-options with simple and complex array angularjs

In this article am going to explain how to use ng-options with simple and complex(array of objects) arrays

Assume you have following array

$scope.fruits = ['apple', 'banana', 'orange'];

If you want to show them in a dropdown using ng-options use the following code

<select name="fruit" data-ng-model="selectedFruit"  data-ng-options="fruit as fruit for fruit in fruits"></select>

What you get in the resulting html is below

<option label="apple" value="string:apple">apple</option>
<option label="banana" value="string:banana">banana</option>
<option label="orange" value="string:orange">orange</option>

when you select any option the value gets selected is same as value shown in dropdown.

Use ng-options for array of objects:


If you have an array of objects like below

$scope.fruits = [{ id: 1, name: 'apple' }, { id: 2, name: 'banana' }, { id: 3, name: 'orange' }];

Use the following html to show them in dropdown

<select name="fruit" data-ng-model="selectedFruit"  data-ng-options="fruit.id as fruit.name for fruit in fruits"></select>

If you use the above html then in the dropdown it shows name of the fruit but internally it stores the id of the fruit when you select any value from dropdown. resulting html code looks like below

<option label="apple" value="number:1">apple</option>
<option label="banana" value="number:2">banana</option>
<option label="orange" value="number:3">orange</option>

When you click on dropdown it shows names of the fruits but when you select any option internally id of the object will be selected.

Check this Plunker for working example: ng-options with simple and complex arrays

This is how ng-options works with simple and complex arrays.

For more posts on angularjs visit: AngularJS

Read more...

Reference the property name within ng-repeat

This article explains how can we reference the property of an object within the ng-repeat.

We all know that the we can render the value of a property in an object like below

<ul>
    <li>{{object.propertyName}}</li>
</ul>

 Reference the property of an object within the ng-repeat:


Assume we have an object called Employee defined as

var employee = { name:"John",
                              age:24,
                              designation:"Developer"
                            };

Now by using the following code we can show all the properties and its values of Employee

<ul>
    <li ng-repeat="(key,val) in employee">{{key}}: {{val}}</li>
</ul>

Output of the above code is

  • age: 24
  • designation: Developer
  • name: John

In this way, we can reference the property of an object within the ng-repeat.

For working example check this fiddle : reference the property name within ng-repeat

For more posts on AngularJS visit : AngularJS
Read more...

AngularJS Datetime Picker

Using angular-date-time-input directive we can format the display of a date in an input box or allow users to enter a valid date with the keyboard.


Requirements:





  • AngularJS 1.4.x or higher (1.0.x will not work)
  • moment.js 2.8.3 or higher for date parsing and formatting
  • bootstrap's glyphicons for arrows (Can be overridden in css)


    First add the datetimepiker.js and datetimepicker.templates.js javascript files (from angular-bootstrap-datetimepicker source files) in your project. 

  • Then add the 'ui.bootstrap.datetimepicker' module as a dependency to your application module:

    var myAppModule = angular.module('MyApp', ['ui.bootstrap.datetimepicker'])

    Now you can use the datetimepicker directive in your html page as below.

    <datetimepicker data-ng-model="model.date"></datetimepicker>

    Above code renders the following datetime picker in the browser.



    Show Angular datetime picker when textbox clicked


    This section explains how to show a datetime picker when user clicked on a textbox.

    First add the following textbox.

    <input type="text" data-ng-model="model.date" id="dropdown1" data-toggle="dropdown">

    Now, add the following <ul> element which has datetime picker inside it.

    <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
              <datetimepicker data-ng-model="data.dateDropDownInput" data-datetimepicker-config="{                 dropdownSelector: '#dropdown2' }"/>
    </ul>


    In the above datetimepicker directive we added data-datetimepicker-config="{ dropdownSelector: '#dropdown1' }" attribute. dropdownSelector should match the id of the textbox element.

    Now when we click on the textbox, datetimepicker will be visible. Once you select the date and time that value will be visible in that textbox.

    This way we can implement angular datetime picker to select the date and time. Please do comment for any suggestions/queries.

    To change the date format of angular datetime picker read : Change date format in angular datetime picker  


    For more posts on AngularJS please visit : AngularJS

    Read more...

    Break forEach loop in AngularJS (illegal break statement error)

    In this article i am going to explain how to break forEach loop in AngularJS

    You cannot directly use break statement inside a AngularJS forEach loop. When you try to use, it throws error illeagal break statement.

    For example consider you have following array

    $scope.users = ['John','Smith','David','Stephen'];

    if you have to check whether the given user present in the users array you have to use following angularJS code.

    var userNameToCheck = 'Smith';
    var userExists = false;
    angular.forEach($scope.users, function(userName){
        if(userName == userNameToCheck)
        {
            userExists = true;
        }
    });

    Above code checks whether the given user name present in the users array or not. If it finds the user in the array then userExists flag will be set to true. But once the user is found we need to get out of the loop in order to avoid unnecessary iterations. But if we directly write break statement inside if condition in angular.forEach loop, it throws error(illegal break statement error).
    So, in order to break out of angular.forEach after finding the user, use the following code

    var userNameToCheck = 'Smith';
    var userExists = false;
    angular.forEach($scope.users, function(userName){
        if(!userExists)
        {
            if(userName == userNameToCheck)
            {
                userExists = true;
            }
        }
    });

    In the above code we added one more if condition which checks whether the userExists flag is already set to true. If it is true that means we already found the user. So no need to check for the other users. 

    In this way we can break forEach loop in AngularJS


    For more posts on AngularJS visit AngularJS

    Read more...

    Remove week column and button from Angular ui bootstrap datepicker

    In this article i am going to explain how to remove weeks column and button from Angular ui bootstrap date picker.

    Assume you have following angular ui bootstrap date picker

    <input type="text" data-datepicker-popup data-ng-model="dateOfBirth" />
    above code renders date picker as shown below 




    To remove weeks column from angular ui bootstrap date picker use the following code

    angular.module('app', ['ui.bootstrap'])
      .config(function (datepickerConfig) {
          datepickerPopupConfig.toggleWeeksText = null;
          datepickerConfig.showWeeks = false;
        });
    above code removes the weeks column from angular ui bootstrap date picker.

    datePickerConfig.showWeeks = false removes week column from date picker and datePickerPopupConfig.toggleWeeksText = null removes the week button from date picker.

    You also need to add the following css 

    ul[datepicker-popup-wrap] > li > .btn-group > button.btn-info {
            display: none;
          }  

    Above code removes weeks column and button from all instances of date picker. If you want to remove them only for one instance of date picker you can use the following code

    <input type="text" data-datepicker-popup data-show-weeks="false" data-ng-model="dateOfBirth" />

    You can check this Plunker for above code examples.


    In this way we can remove weeks column and button from Angular ui bootstrap date picker.

    To know how to remove footer from angular ui bootstrap date picker visit remove footer from Angular ui bootstrap datepicker


    For more posts on AngularJS visit AngularJS

    Read more...

    How to remove footer from Angular ui bootstrap datepicker

    In this article i am going to show how to remove footer from Angular ui bootstrap date picker.

    Assume you have following angular ui bootstrap date picker

    <input type="text" data-datepicker-popup data-ng-model="dateOfBirth" />
    above code renders date picker as shown below 


    If you observe above angular ui date picker image you can see that it is also showing a footer which contains options to choose Today, Weeks and Clear.

    We can remove this footer from angular ui bootstrap date picker using following code

    if you want to remove footer globally you can use following AngularJS code

    yourApp.config(function (datepickerConfig, datepickerPopupConfig) {
        // datepickerConfig.showWeeks = false;
        // datepickerPopupConfig.toggleWeeksText = null;
        datepickerPopupConfig.showButtonBar = false;
    });
     Or if you want to remove footer for only one specific date picker you can do using following code

    <input type="text" data-datepicker-popup show-button-bar="false" />

    In this way you can remove footer from angular ui bootstrap datepicker



    For more posts on AngularJS visit AngularJS

    Read more...

    Show red border for invalid input fields on form submit angularjs

    In this article we are going to see how to show red color border for all invalid input elements after submitting form (using AngularJs)

    Whenever we are providing a form to user to submit some data like his contact details or purchase details etc we have to make sure that some of the fields are filled for sure before submitting. Or if the form contains special fields like Email or Website url we have to make sure that user is submitting these values in correct format.

    We can make a field as required field by adding html 5 Required attribute to that input field. And for Email field give type as type="email"  and for url field give the type as or type="url"

    Let's create a simple form where user have to submit his contact details

    <form name="addContact">
    
      <label>First Name</label>
      <input type="text" placeholder="First Name" data-ng-model="model.firstName" id="FirstName" name="FirstName" required/>
    
      <label>Last Name</label>
      <input type="text" placeholder="Last Name" data-ng-model="model.lastName" id="LastName" name="LastName" required/>
    
      <label>Email</label>
      <input type="email" placeholder="Email" data-ng-model="model.email" id="Email" name="Email" required/>
    
      <input class="btn" data-ng-click="save(model)" type="button" value="SAVE" />  
      </form>

    In the above form we have 3 fields First Name, Last Name and Email. I added html 5 required attribute to all fields so that user must enter value in all the three fields before submitting. I also added type="email" to Email field so that it will make sure that user submitting Email address in correct format.

    Now, if user submits form without entering values in any of these fields, we have to show error message saying that these fields are required and must be filled before submitting.

    For that, let's add span tags just below the input fields which contains respective error message.

    Modify the above html as

    <form name="addContact">
    
      <label>First Name</label>
      <input type="text" placeholder="First Name" data-ng-model="model.firstName" id="FirstName" name="FirstName" required/>
      <span class="text-error" data-ng-show="addContact.submitted && addContact.FirstName.$error.required">first Name is required</span>
      <label>Last Name</label>
      <input type="text" placeholder="Last Name" data-ng-model="model.lastName" id="LastName" name="LastName" required/>
      <span class="text-error" data-ng-show="addContact.submitted && addContact.LastName.$error.required">Last Name is required</span>
      <label>Email</label>
      <input type="email" placeholder="Email" data-ng-model="model.email" id="Email" name="Email" required/>
      <span class="text-error" data-ng-show="addContact.submitted && addContact.Email.$error.required">Email address is required</span>  <span class="text-error" data-ng-show="addContact.submitted && addContact.Email.$error.email">Email address is not valid</span>
      <input class="btn" data-ng-click="save(model)" type="button" value="SAVE" />  
      </form>

    Now in your Angular Controller add the following code

    $scope.save= function (model) {
        if ($scope.addContact.$valid) {      
          //form is valid
        }
        else {
            //if form is not valid set $scope.addContact.submitted to true     
            $scope.addContact.submitted=true;    
        }};
    });
    In Save function we are checking whether the form is valid or not using $scope.addContact.$valid. If all fields are filled, then the form will get submitted and if all the fields are not filled then error message will be shown under the fields. (to show the error messages in red color i added class 'text-error' to all span tags. so in css add color:red to using class)

    If you check the span elements we added data-ng-show="addContact.submitted && addContact.FirstName.$error.required". This is because these span elements must be shown only after submitting form. we are making addContact.submitted to true within the save function. And addContact.FirstName.$error.required will be true if the value is null. So as both values are true the span elements will be shown.

    If you observe the above html code you can see two span tags for Email field. One is to check whether the Email field is empty and other is to check whether the value entered in Email field is in correct format or not.

     In this way, we can show error message for invalid fields after submitting form.

    What if you want to show red border for all invalid fields on form submit using AngularJs ?


    This can be achieved by adding a class to all input fields after submitting form. This can be done using ng-class attribute(which adds/removes a class based on a condition)

    For example change the First Name field to

    <input type="text" ng-class="{submitted:addContact.submitted}" placeholder="First Name" data-ng-model="model.firstName" id="FirstName" name="FirstName" required/>
    In the above html code i added ng-class attribute to First Name field. So, class named "submitted" gets applied to that field when addContact.sumbitted value is true. As we are making this property true after submitting the form (see save function code above) the class gets applied to First Name field after submitting form.

    So, now if the form get submitted without entering any value in required fields then "submitted" class will be applied to those elements. So, now we can show red border for all those elements using the following css

    input.submitted.ng-invalid
    {
      border:1px solid #f00;
    }
    above css changes the border color of all invalid fields after submitting form. If you observe the above css it has two classes 'submitted' and 'ng-invalid' where we are manually adding class 'submitted' to input elements and one more class 'ng-invalid' is automatically added by AngularJs if the field is invalid.

    We can also have a class at form level. i.e., we can add ng-class attribute to form instead of adding to each element in form.

    Exapmle:
    <form name="addRelation" ng-class="{submitted:addContact.submitted}">
    Above statement adds the class('submitted') to form. So, now you can use following css to show red color border to all invalid input fields

    form.submitted .ng-invalid
    {
        border:1px solid #f00;
    }
    In this way we can show red color border on input fields after submitting form using Angular JS.

    For more posts on AngularJs visit : AngularJs

    Read more...