Tuesday, 24 November 2015

Structured-Filter - a jQuery UI Widget for building structured queries


Introduction

Structured-Filter is a generic Web UI for building structured search or filter queries. With it you can build structured queries like "Contacts where Firstname starts with 'A' and Birthday after 1/1/1980 and State in (CA, NY, FL)"…
It is a full jQuery UI widget, supporting various configurations and themes.
The project and a live demo are available at GitHub.

Background

Most enterprise web applications used to have an "advanced search" displayed as a form containing a list of fields with operators and values like the screenshot below (or this live demo).

Today, the same functionality is displayed in a more dynamic way and usually called "filter" rather than "advanced search".

This newer UX pattern needs more clicks to build queries but it takes less real-estate and doesn't force users to navigate to a different page.

Model

The widget is configured with a list of fields to use in the search conditions.

Fields

Each field must have an ID, a type and a label.
  • id – unique identifier for the field.
  • label – displayed field name.
  • type – data type. Possible types of field: text, number, boolean, date, time, list.
Example:
fields = [
    { type:"text", id:"lastname", label:"Lastname"},
    { type:"text", id:"firstname", label:"Firstname"},
    { type:"boolean", id:"active", label:"Is active"},
    { type:"number", id:"age", label:"Age"},
    { type:"date", id:"bday", label:"Birthday"},
 {type:"list", id:"category", label:"Category",
  list:[
   {id:'1', label:"Family"},
   {id:'2', label:"Friends"},
   {id:'3', label:"Business"},
   {id:'4', label:"Acquaintances"},
   {id:'5', label:"Other"}
  ]
 }
];

Conditions

Queries are expressed as a set of conditions.
Each condition is defined by:
  • a field
  • an operator
  • one or several values
For each field the possible operators are determined by it's type.
boolean:
  • Yes (1)
  • No (0)
date:
  • on (eq)
  • not on (ne)
  • after (gt)
  • before (lt)
  • between (bw)
  • is empty (null)
  • is not empty (nn)
list:
  • any of (in)
  • equal (eq)
number:
  • = (eq)
  • != (ne)
  • > (gt)
  • < (lt)
  • is empty (null)
  • is not empty (nn)
text:
  • equals (eq)
  • not equal (ne)
  • starts with (sw)
  • contains (ct)
  • doesn't contain (nct)
  • finishes with (fw)
  • is empty (null)
  • is not empty (nn)
time:
  • at (eq)
  • not at (ne)
  • after (gt)
  • before (lt)
  • between (bw)
  • is empty (null)
  • is not empty (nn)

Using the code

First, load jQuery, jQuery-UI and the widget code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"
 type="text/javascript" charset="utf-8"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js" 
type="text/javascript" charset="utf-8"></script>
<script src="js/structured-filter.js" type="text/javascript" 
charset="utf-8"></script>
The widget requires a jQuery UI theme to be present, as well as its own included base CSS file (structured-filter.css). Here, we use the "ui-lightness" theme as an example:
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/
libs/jqueryui/1.11.4/themes/ui-lightness/jquery-ui.css">
<link href="css/structured-filter.css" rel="stylesheet" type="text/css">
Now, let's attach it to an existing <DIV> tag:
<script type="text/javascript">
    $(document).ready(function() {
        $("#myFilter").structFilter({
            fields: [
                {type:"text", id:"lastname", label:"Lastname"},
                {type:"text", id:"firstname", label:"Firstname"},
                {type:"boolean", id:"active", label:"Is active"},
                {type:"number", id:"age", label:"Age"},
                {type:"date", id:"bday", label:"Birthday"},
                {type:"lov", id:"category", label:"Category",
                    list:[
                        {id:'1', label:"Family"},
                        {id:'2', label:"Friends"},
                        {id:'3', label:"Business"},
                        {id:'4', label:"Acquaintances"},
                        {id:'5', label:"Other"}
                    ]
                }
            ]
        });
    });
</script>

<div style="width:100px;" id="myFilter"></div>
This will change the <DIV> into the widget.
Note: Structured-Filter doesn't require the full jQueryUI. You can use a custom build one with only the following: jquery.ui.core.js, jquery.ui.widget.js, jquery.ui.button.js and jquery.ui.datepicker.js.

Options

Structured-Filter provides several options to customize its behaviour:

buttonLabels (Boolean)

The labels of buttons used to manipulate filters. This options applies to the 3 buttons, "New filter", "Add filter"/"Update filter" and "Cancel" which use icons if the option is set to false.
$("#myFilter").structFilter({
    buttonLabels: true
});
Defaults to *false*.

dateFormat (String)

The format for parsed and displayed dates. This attribute is one of the regionalisation attributes.
Common formats are: Default – "mm/dd/yy", ISO 8601 – "yy-mm-dd", Short – "d M, y", Medium – "d MM, y", Full – "DD, d MM, yy". For a full list of the possible formats see the [jQuery formatDate function](http://docs.jquery.com/UI/Datepicker/formatDate).
$("#myFilter").structFilter({
    dateFormat: "d M, y"
});
Defaults to *"mm/dd/yy"*.

fields (array)

The list of fields (as an array of objects with id, label and type) to participate in the query definition.
Possible types are: text, boolean, number, date, time, and list.
$("#myFilter").structFilter({
    fields: [
        {type:"text", id:"lastname", label:"Lastname"},
        {type:"text", id:"firstname", label:"Firstname"},
        {type:"boolean", id:"active", label:"Is active"},
        {type:"number", id:"age", label:"Age"},
        {type:"date", id:"bday", label:"Birthday"},
        {type:"list", id:"category", label:"Category",
            list:[
                {id:'1', label:"Family"},
                {id:'2', label:"Friends"},
                {id:'3', label:"Business"},
                {id:'4', label:"Acquaintances"},
                {id:'5', label:"Other"}
            ]
        }
    ]
});
Defaults to *[ ]*.

highlight (Boolean)

A highlight animation performed on the last added or modified filter.
$("#myFilter").structFilter({
    highlight: false
});
Defaults to *true*.

submitButton (Boolean)

Shows or hides the "Submit" button.
$("#myFilter").structFilter({
    submitReady: true
});
Defaults to *false*.

submitReady (Boolean)

Provides hidden fields with the conditions' values to be submitted with the form (as an alternative to an AJAX call).
$("#myFilter").structFilter({
    submitReady: true
});
Defaults to *false*.

Methods

addCondition(data)

Adds a new filter condition.
$("#myFilter").structFilter("addCondition", {
    field:{
        label: 'Lastname',
        value: 'lastname'
    },
    operator:{
        label: 'starts with',
        value: 'sw'
    },
    value:{
        label: '"a"',
        value: 'a'
    }
});

clear()

Removes all search filters.
$("#myFilter").structFilter("clear");

length()

Gets the number of filters.
$("#myFilter").structFilter("length");

removeCondition(index)

Removes the condition of the specified index.
$("#myFilter").structFilter("removeCondition", 0);

val([data])

Gets or sets the filter definition (as an array of filters).
$("#myFilter").structFilter("val");

$("#myFilter").structFilter("val", data);
Sample value:
[
    {
        "field":{
            "label": "Lastname",
            "value": "Lastname"
        },
        "operator":{
            "label": "starts with",
            "value": "sw"
        },
        "value":{
            "label": "\"jo\"",
            "value": "jo"
        }
    }
]

valText()

Gets the filter definition (as a readable text string).
$("#myFilter").structFilter("valText");
Sample value:
Lastname starts with "jo"

valUrl()

Gets the filter definition (as a URL string).
$("#myFilter").structFilter("valUrl");
Sample value:
filters=1&field-0=Lastname&operator-0=sw&value-0=jo&label=Lastname%20
starts%20with%20%22jo%22%0A

Events

change.search

This event is triggered when the list of search conditions is modified.
$("#myFilter").on("change.search", function(event){
    // do something
});

submit.search

This event is triggered when the submit button is clicked.
$("#myFilter").on("submit.search", function(event){
    // do something
});

Theming

Structured-Filter is as easily themeable as any jQuery UI widget, using one of the jQuery UI themes or your own custom theme made with Themeroller.
ui-lightness:

ui-darkness:

start:

redmond:

le-frog:

Beside jQuery UI themes, I also have an implementation for Bootstrap (and Backbone) as part of Evolutility metadata-driven set of Backbone views.

 

Ref: http://www.codeproject.com/Articles/821396/Structured-Filter-a-jQuery-UI-Widget-for-building

Wednesday, 21 October 2015

Read XML using jQuery Ajax

Below is the sample XML file that we will be using for the demo.The XML file is having list of Contacts with Name and PhoneNumber as 2 XML node with every Contact. 

XML File :- 


<?xml version="1.0" encoding="utf-8"?>
<ContactList>
<Contact>
<Name>Jitendra Gangwar</Name>
<PhoneNumber>1234567890</PhoneNumber>
</Contact>
<Contact>
<Name>Suraj Sharma</Name>
<PhoneNumber>1234567890</PhoneNumber>
</Contact>
<Contact>
<Name>Arun Kumar</Name>
<PhoneNumber>9876543210</PhoneNumber>
</Contact>
<Contact>
<Name>Dhirendra Kumar</Name>
<PhoneNumber>9512121210</PhoneNumber>
</Contact>

</ContactList>

Now we are going to process the XML using jQuery
  • Call the ajax method to process the xml file.
  • Set HTTP request type to "GET" and also provide the name of XML file in url.
  • Set the datatype to "xml".
  • We also need to define a callback functions, which gets called when request is successful or if some error occurred.
  • So when success callback is called then loop through the xml content.
  • Get the node value for "Title" and "Publisher" and append it to the div.
  • Define error callback to handle the error.
jQuery code:-

<script type="text/javascript" language="javascript">
$(document).ready(function(){
$("#dvContent").append("<ul></ul>");
$.ajax({
type: "GET",
url: "ContactList.xml",
dataType: "xml",
success: function(xml){
$(xml).find('Contact').each(function(){
var sName = $(this).find('Name').text();
var sPhoneNumber = $(this).find('PhoneNumber').text();
$("<li></li>").html(sName + ", " + sPhoneNumber).appendTo("#dvContent ul");
});
},
error: function() {
alert("An error occurred while processing XML file.");
}
});
});
</script>

Complete Source Code : - 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Process XML using jQuery</title>
<script src="Scripts/jquery-2.1.4.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$("#dvContent").append("<ul></ul>");
$.ajax({
type: "GET",
url: "ContactList.xml",
dataType: "xml",
success: function(xml){
$(xml).find('Contact').each(function(){
var sName = $(this).find('Name').text();
var sPhoneNumber = $(this).find('PhoneNumber').text();
$("<li></li>").html(sName + ", " + sPhoneNumber).appendTo("#dvContent ul");
});
},
error: function() {
alert("An error occurred while processing XML file.");
}
});
});
</script>
<style type="text/css">
body
{
font-family : Arial;
font-size : 10pt;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="dvContent">
</div>
</form>
</body>
</html>

OutPut : -

Monday, 18 May 2015

Introduction to angular.js

In this article we start with angular.js let’s discuss some of the basic concepts.


Single page application 

Single page application is a web based application that contains a single web pageand that is dynamically updated as the user interacts with the application. The purposeof single page application is to provide user a desktop application like experience.


What is Angular.js? 

Angular.js is a JavaScript framework for creating rich and interactive single pageapplication. Angular.js is maintained by Google.


How to include angular.js in our project


There are 2 ways to include angular.js on your webpage.

   1. Download from angularjs.org

     a. Go to angularjs.org and click on ‘Download’ button .

   2. Include angular.js from the cloud You can add following script tag in head section of your Sourcen file

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>


Directives 

Directives allow you to add behavior to HTML element.

   1. ng-app – This directive is used to initialize the angular.js application. Thisdirective also defines the scope of angular.js application. For example if we have a divtag and inside the tag a directive ng-app is defined then all the angular.js elementswill work under div tag only. So if you want extend the scope to the entire page then Isuggest you to add ng-app in body or html tag.

   2. ng-model - binds the value of HTML controls (input, select, textarea) toapplication data.

   3. ng-bind - binds application data to the HTML view. View is user interface that auser can see and interact.


Create your first angular.js application 

In the preceding section you saw that how to include angular.js in your page. In thissection we will create a simple angular.js application

In the HTML page add following code

<div ng-app="">
        <p>
            Name:
            <input type="text" ng-model="name"></p>
        <p ng-bind="name">
        </p>

    </div>


In the preceding code we have defined ng-app directive in a div tag. Then we have used ng-model and ng-bind to bind a paragraph and a textbox.

Complete code in Source File -

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div ng-app="">
        <p>
            Name:
            <input type="text" ng-model="name"></p>
        <p ng-bind="name">
        </p>
    </div>
    </form>
</body>

</html>



Output 


Angular.js checks if the value in paragraph is same as value in textbox. If the value isnot same then angular.js updates the text in paragraph with the text written in textbox.