jQuery Autocomplete

Autocomplete  widget allows to search and select results from a pre-populated list and the source can be from database, array etc. Once you start typing the widget starts searching for results to matched results.

One of the issues which I find with autocomplete is that it gives the user an option to select from the list or type in anything. I have working with few projects which wants only selection from list and not any free text.

To solve this issue, we can use change event with autocomplete which will check if you have selected the value and if you have not then it will delete the value. The change event is being triggered when the value is being changed in the textbox or if the field is being blurred. After selecting the option from the list it will be a good option to disable the tying from the textbox and add a link next to the text box to inform the user to activate tying if they want to make changes. This way the textbox will be under control and typing can be prevented.

Sample Code

$(document).ready(function () {
 
 $("#category").autocomplete({
 source: function (request, response) {
 $.ajax({
 url: "http://en.wikipedia.org/w/api.php",
 dataType: "jsonp",
 data: {
 'action': "opensearch",
 'format': "json",
 'search': request.term
 },
 success: function (data) {
 response(data[1]);
 }
 });
 },
 change: function (event, ui) {
 if (ui.item == null || ui.item == undefined) {
 $("#category").val("");
 $("#category").attr("disabled", false);
 } else {
 $("#category").attr("disabled", true);
 }
 }
 });

$('#changeCategory').click(function (e) {
 e.preventDefault();
 $("#category").attr("disabled", false);
 });
});

Resources:
Reading materials link
Working fiddle link

Happy Coding

ReactJS – JSX

What is JSX

JSX is an XML-STYLED syntax for use in javascripts. JSX is  a preprocessor step that adds XML syntax to JavaScript.

Just like XML, JSX tags have a tag name, attributes, and children. If an attribute value is enclosed in quotes, the value is a string. Otherwise, wrap the value in braces and the value is the enclosed JavaScript expression.

Rules:

  • JSX is XML and not to be mistaken with HTML
  • JSX avoids Javascript keywords such as ‘CLASS’ uses classname instead
  • LOWER-CASE for html tabs
  • CAPITAL-CASE for react components
  • CAMEL-CASE for html attributes
  • Style is mostly a json object

Compilers

  • This is the link to compile the html to JSX.
  • Babel link for the compiler JSX to ReactJS

 

 

 

ReactJS: Framework

React Framework

What is ReactJS

ReactJS is a javascript framework from Facebook (javascript library) which is narrow in scope but tightly focused. It is concerned with rendering the view with data state(dataset) and viewer state(UI). It is a two way binding with model and view and not using the controller. It turns the javascript into reactive descriptive language.

In short javascript is about props, state and events.

Benefits of using ReactJS

It uses virtual DOM which is in memory DOM, very fast and has many states. It also uses Javascript XML(JSX).

Features

  • Rendering an Element

ReactDom.render() – renders element to the DOM

React Element the element to render
DOM Element the target DOM element to render into
Callback optional callback function

Example (fiddle)

ReactDOM.render(
  <h1>Hello, world</h1>,
  document.getElementById('container'),
  function(){
     window.alert('done');
  }
)

<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>
  • Rendering a Component

Returns element via render method and reusable components. In MyComponent capital M is important, if M is not capital than reactjs will not function properly. React component returns the react element. render() is a required method.

Example (fiddle)

var MyComponent = React.createClass({
  render: function(){
  	return (
    	<p>Testing the MyComponent feature</p>
    );
  }
});

ReactDOM.render(
<MyComponent/>,
document.getElementById('container')
);

Source: Link

  • Component Props

Collection of values associated with a component (Key value pair). It allows dynamic rendering of data when re-using components.

Example

var Hello = React.createClass({
    render: function() {
        return
Hello {this.props.name}

; } }); ReactDOM.render( <Hello name=“World” />, document.getElementById(‘container’) );

  • Component State

Sometimes components may have state that represent a mutatable properties that may affect the rendered output, eg pressing a button (True or False). State is optional in a component.

2 types of states:

  • GETINITIALSTATE – set initial values
  • SETSTATE – update and render from virtual DOM

Example

var ClickCounter = React.createClass({
	getInitialState: function(){
		return { 
			count:0
		}
	},

	increment: function(){
		this.setState({
			count:this.state.count + 1
		});
	},
    render: function() {
        return (
        	<button> Clicked {this.state.count} times! </button>
        );
    }
});
 
ReactDOM.render(
	<ClickCounter />,
    document.getElementById('container')
);
  • Component Events

Events tell when to change the state of the components.

Useful Materials

React Website

Blog

Happy coding!!

….Sometime simple tip makes someone’s coding life easier…………

Weight Concern

Ways to reduce weight:

  1. Dieting
  2. Stomach fat burn
  3. Soup

Dieting

Swedish Diet

Meat and dairy lovers can try this if they looking forward to lose some weight.

Stomach Fat Burn

Burn Belly Fat

I haven’t tried this but I am going to try this for sure.

Melt Belly Fat

Review:

I have used parsely before and it does help in reducing weight. Actually I am drinking parley everything instead of plain water. The taste is not so good but once you get used to it, it helps a lot.

Yoga Poses to reduce fat

It is worth trying this.

Soup

Cabbage Soup

Review:

Has anyone tried this, doesn’t sound impressive but I  usually make soup with chicken or lamb. I will definitely try once because I am i na mission to lose around 10 kilos by the end of this year. This articles just motives me that ti is possible to achieve my goal.

Happy days!!

Mysql Tips #3: Drop and CONCAT

Drop Index

One common mistake which is being made on dropping index is that we forget the index name and try to drop the column name.

drop index departments_name_unique on departments;

CONCAT()

Incase if you want to join the fields

SELECT  DATE, expiry, CONCAT( user.b_fname, " ", user.b_lname ) 
FROM user

Joins

Different types of joins: inner joins, left joins, right joins union/outer joins. This link explains in great detail.

 

Happy coding!!

….Sometime simple tip makes someone’s coding life easier…………

Mysql Tips #2 : Using Group

Using group in MySQL:

Group by is being used to group field into a single row to perform count, sum avg etc.

SELECT *
FROM `test_results` 
WHERE parent_code =11
GROUP BY username

Using Group by with MAX

SELECT MAX( points ) , username
FROM `test_results` 
WHERE parent_code =11
GROUP BY username

Similar can be used for min, avg, count…etc

Using Group by with Having

select gid
 from `gd`
 group by gid
 having count(*) > 10
order by lastupdated desc

Happy coding!!

….Sometime simple tip makes someone’s coding life easier…………