Back to Materialize

Chips Content

jade/page-contents/chips_content.html

1.0.03.8 KB
Original Source

Chips can be used to represent small blocks of information. They are most commonly used either for contacts or for tags.

Jane Doe

Tag close

Contacts

To create a contact chip just add an img inside.

<div class="chip">
    
    Jane Doe
  </div>

Tags

To create a tag chip just add a close icon inside with the class close.

<div class="chip">
    Tag
    <i class="close material-icons">close</i>
  </div>

Javascript Plugin

To add tags, just enter your tag text and press enter. You can delete them by clicking on the close icon or by using your delete button.

Set initial tags.

Use placeholders and override hint texts.

Use autocomplete with chips.

<!-- Default with no input (automatically generated) -->
  <div class="chips"></div>
  <div class="chips chips-initial"></div>
  <div class="chips chips-placeholder"></div>
  <div class="chips chips-autocomplete"></div>

  <!-- Customizable input -->
  <div class="chips">
    <input class="custom-class">
  </div>

Initialization

document.addEventListener('DOMContentLoaded', function() {
    var elems = document.querySelectorAll('.chips');
    var instances = M.Chips.init(elems, options);
  });

  // Or with jQuery

  $('.chips').chips();
  $('.chips-initial').chips({
    data: [{
      tag: 'Apple',
    }, {
      tag: 'Microsoft',
    }, {
      tag: 'Google',
    }],
  });
  $('.chips-placeholder').chips({
    placeholder: 'Enter a tag',
    secondaryPlaceholder: '+Tag',
  });
  $('.chips-autocomplete').chips({
    autocompleteOptions: {
      data: {
        'Apple': null,
        'Microsoft': null,
        'Google': null
      },
      limit: Infinity,
      minLength: 1
    }
  });

Chip data object

var chip = {
    tag: 'chip content',
    image: '', //optional
  };

Options

NameTypeDefaultDescription
dataArray[]Set the chip data (look at the Chip data object)
placeholderString''Set first placeholder when there are no tags.
secondaryPlaceholderString''Set second placeholder when adding additional tags.
autocompleteOptionsObject{}Set autocomplete options.
limitIntegerInfinitySet chips limit.
onChipAddFunctionnullCallback for chip add.
onChipSelectFunctionnullCallback for chip select.
onChipDeleteFunctionnullCallback for chip delete.

Methods

Use these methods to interact with chips.

Because jQuery is no longer a dependency, all the methods are called on the plugin instance. You can get the plugin instance like this:

var instance = M.Chips.getInstance(elem);

/* jQuery Method Calls
You can still use the old jQuery plugin method calls.
But you won't be able to access instance properties.

$('.chips').chips('methodName');
$('.chips').chips('methodName', paramName);
*/
.addChip();

Add chip to input.

Arguments

Chip: Chip data object.

instance.addChip({
    tag: 'chip content',
    image: '', // optional
  });
.deleteChip();

Delete nth chip.

Arguments

Integer: Index of chip.

instance.deleteChip(3); // Delete 3rd chip.
.selectChip();

Select nth chip.

Arguments

Integer: Index of chip.

instance.selectChip(2); // Select 2nd chip

Properties

NameTypeDescription
elElementThe DOM element the plugin was initialized with.
optionsObjectThe options the instance was initialized with.
chipsDataArrayArray of the current chips data.
hasAutocompleteBooleanIf the chips has autocomplete enabled.
autocompleteObjectAutocomplete instance, if any.