PHP code example of silverware / select2

1. Go to this page and download the library: Download silverware/select2 library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

silverware / select2 example snippets


use SilverWare\Select2\Forms\Select2Field;

$field = Select2Field::create(
  'MySelect2Field',
  'Select2 Field',
  [
    1 => 'An',
    2 => 'Array',
    3 => 'of',
    4 => 'Options'
  ]
);

$field->setConfig('maximum-input-length', 20);

$field->setConfig('maximum-input-length', 20);  // this will work
$field->setConfig('maximumInputLength', 20);    // this will NOT work

use SilverWare\Select2\Forms\Select2AjaxField;

$field = Select2AjaxField::create(
  'MySelect2AjaxField',
  'Select2 Ajax Field'
);

$field->setDataClass(SiteTree::class);  // by default, the field searches for SiteTree records

$field->setIDField('ID');       // the name of the field which identifies the record
$field->setTextField('Title');  // the name of the field to use for the option text

$field->setSearchFields([
  'Title'  // an array of fields to search based on the entered term
]);

$field->setSortBy([
  'Title' => 'ASC'  // an array which defines the sort order of the results
]);

$field->setLimit(256);  // the maximum number of records to return

$field->setExclude([
  'Title:ExactMatch' => 'Hide This Title'
]);

$field->setFormatResult('<span>Found: <em>$Title</em></span>');
$field->setFormatSelection('<span>Selected: <strong>$Title</strong></span>');

$field->setAjaxConfig('cache', false);
$field->setAjaxConfig('delay', 500);

$field->setAjaxConfig('data-type', 'json');  // this will work
$field->setAjaxConfig('dataType', 'json');   // this will NOT work