Quantcast
Channel: TechZoo - Technology Blog » ExtJS tutorial
Viewing all articles
Browse latest Browse all 3

ExtJS 4 Autocomplete ComboBox

$
0
0

ExtJS is one of the best client-side JavaScript widget frameworks which come with hundreds of build-in components and it’s also very easy to extend or customized those components. One of the most useful components is ComboBox. We can use combobox to populate groups of items on it, select one or multiple items and can associate store with it to populate it remotely.

Today I am going to show you how to customized ExtJS combo box to looks like and behave like a auto-complete textfield. We will use ArrayStore to store the companies name and ids and on entering character in ComboBox will show a list of companies sorted with that character.

Ext.onReady(function () {
  var store = Ext.create('Ext.data.ArrayStore', {
    fields: [
      {name: 'id'},
      {name: 'company'}
    ], 
    data  : [
      [1,'Ali Baba'],
      [2,'Alcoa Inc'],
      [3,'Altria Group Inc'],
      [4,'American Express Company'],
      [5,'American International Group, Inc.'],
      [6, 'AT&T Inc.'],
      [7, 'Boeing Co.'],
      [8, 'Caterpillar Inc.'],
      [9, 'Citigroup, Inc.'],
      [10, 'E.I. du Pont de Nemours and Company'],
      [11, 'Exxon Mobil Corp'],
      [12, 'General Electric Company'],
      [13, 'General Motors Corporation'],
      [14, 'Hewlett-Packard Co.'],
      [15, 'Honeywell Intl Inc'],
      [16, 'Intel Corporation'],
      [17, 'International Business Machines'],
      [18, 'Johnson & Johnson'],
      [19, 'JP Morgan & Chase & Co'],
      [20, 'McDonald\'s Corporation'],
      [21, 'Merck & Co., Inc.'],
      [22, 'Microsoft Corporation'],
      [23, 'Pfizer Inc'],
      [24, 'The Coca-Cola Company'],
      [25, 'The Home Depot, Inc.'],
      [26, 'The Procter & Gamble Company'],
      [27, 'United Technologies Corporation'],
      [28, 'Verizon Communications'],
      [29, 'Wal-Mart Stores, Inc.'],
      [30, 'Walt Disney Company (The) (Holding Company)']
    ]
  });
  Ext.create('Ext.form.Panel', {
    width: 400,
    frame: true,
    title: 'ExtJS Auto Complete ComboBox',
    padding: 10,
    fieldDefaults: {
      msgTarget : 'side',
      labelWidth: 100
    },
    defaultType: 'textfield',
    defaults: {
      anchor: '100%'
    },
    items: [{
      xtype : 'displayfield',
      value : 'Type first two characters of your company to select it.'
    },{
      fieldLabel: 'Company Name',
      xtype     : 'combobox',
      emptyText : 'Company Name',
      typeAhead : true, 
      minChars  : 2,
      store     : store,
      valueField    : 'id', 
      triggerAction : 'query', 
      hideTrigger   : true,
      displayField  : 'company'
    }],
    dockedItems: [{
      xtype: 'toolbar',
      dock: 'bottom',
      ui: 'footer',
      items: [{
        xtype: 'tbfill'
      },{
        text: 'Cancel'
      },{
        text: 'Submit Form'
      }]
    }],
    renderTo: 'output'
  });
});

Output:

Enter first two characters of your company and you will see following output.

Happy Coding :)


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles



Latest Images