定制 dakujem/selectoo 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dakujem/selectoo

Composer 安装命令:

composer require dakujem/selectoo

包简介

SelectBox & MultiSelectBox hybrid input with lazy & asynchronous options loading capabilities to be used with Select2, Selectize, Chosen and similar UI libraries.

README 文档

README

PHP from Packagist

SelectBox & MultiSelectBox hybrid input with lazy & asynchronous options loading capabilities to be used with Select2, Selectize, Chosen and similar UI libraries.

The aim of Selectoo is to provide a flexible tool for creating reusable select inputs.

Features

  • can work in single-choice or multi-choice modes
    • single-choice mode is roughly equal to SelectBox
    • multi-choice mode is roughly equal to MultiSelectBox
  • on-demand (lazy) loading of options using a callback
    • options can be loaded asynchronously using AJAX
    • options are only loaded when really needed
  • can be skinned with UI libraries or custom scripts (see below)
  • dependent / cascading inputs

Usage / Use cases

Reusable inputs with specific setup:

  • select2-skinned (or other library) input with special configuration options and other application logic
  • autocomplete with ajax
  • tagging
  • custom ui script for altering the DOM upon selection
  • cascading / dependent inputs

See included examples for more information.

Configuring Selectoo input

Create a (multi)select box with fixed options

$items = ['a', 'b', 'c', 'd',];
$input = new Selectoo($label, $items, $multiselect);

or a (multi)select box with options loaded on demand (lazy loading).

$input = new Selectoo($label, [$repository, 'fetchAll'], $multiselect);

An engine can optionally be attached

$input->setEngine(new Select2Engine);

and configured.

$input->getEngine()

	->placeholder('Select a user', true)
	->width('width: 100%', true)
	->closeOnSelect(false, true)

;

The Select2Engine is configured using magic options (or setOption method). The names of the magic methods (or option names) represent the Select2 configuration options.

Lazy options loading & AJAX

Lazy loading is used when the Selectoo instance is given "item callback" callable instead of an array of items. The callable is supposed to return a list of items.

The item callback also works as a validator for values. It receives raw value as the first argument (and the input instance as the second). The callback should return the array of possible values (these are the items) in form exactly the same as the items set to SelectBox or MultiSelectBox inputs. The raw value is then compared to the returned items and filtered. This approach ensures the validity of the data.

It is important to set this callback in case you work with remote item loading (ajax/api loading of options).

See the example ajax presenter, complete with input factory and user repository examples.

Ajax configuration can be very simple:

(new Select2Engine())
	->ajax('{url: "' . $link . '", dataType: "json" }')
;

Supported and intended UI libraries

Selectoo uses "script engines" to generate JavaScript UI scripts along with HTML input element. Any script engine can be attached to the Selectoo instance by calling $selectoo->setEngine($engine).

  • select2/select2
    • use the Select2Engine instances
  • selectize/selectize.js
    • to be implemented
  • harvesthq/chosen
    • to be implemented

Custom engines can easily be created implementing the ScriptEngineInterface interface.

Factories

For best reusability I strongly encourage using factories for inputs with more logic, typically these involve fetching data from storage or external resources along with JS/UI configuration, handling events and value validation.

See simple examples:

Script management

The scripts generated by engines can be post-processed and/or collected by a management routine. This approach can be used to gather and place the scripts at the end of the page and to solve problems when writing scripts inside the page and loading jQuery at the end of the HTML file.

See script-management example for more information.

Dependent selections / cascading inputs

Example is being prepared.

What is needed, in general:

  • UI script that manages on change events
  • Select2 configuration that sends the value of master inputs in the URL of the AJAX request
  • repository filtering based upon the values
  • API endpoint that receives the master values and makes repository request, using values from the URL
  • "item callback" that also makes repository request, using values from Form

Note that when the item callback is called the values from the Form object can be used to check for master values

By "master values" above I mean the values of the inputs the Selectoo instance depends on

By following these steps, dependent Selectoo inputs can be created and values validated.

Examples

Please see the examples included in the repository to get a better understanding of when Selectoo comes handy.

Notable differences compared to Nette SelectBox & MultiSelectBox

  • disabling a Selectoo input does not modify/reset its value, so it can be re-enabled without the loss of the information
  • disabled input is handled differently - raw value is always loaded even when input (or items) is disabled. The disabled values are filtered when calling getValue method.

Requirements

Selectoo PHP Nette forms
version 2.0 7.1+ 3.*
version 1.0 7.0+ 2.4.*

You should also install Select2 version 4 to use the Select2Engine, see Select2 documentation.

You may install any other select input "skin" and implement your own engine for it within minutes.

Selectoo can also be used without such a "skin".

dakujem/selectoo 适用场景与选型建议

dakujem/selectoo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.96k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「javascript」 「autocomplete」 「Forms」 「nette」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dakujem/selectoo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dakujem/selectoo 我们能提供哪些服务?
定制开发 / 二次开发

基于 dakujem/selectoo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 26.96k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: Unlicense
  • 更新时间: 2018-01-03