tacoberu/nette-form-selectboxremote 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

tacoberu/nette-form-selectboxremote

Composer 安装命令:

composer require tacoberu/nette-form-selectboxremote

包简介

Select, which load options from remote.

README 文档

README

Select, which retrieves data remotely. It's available for large amounts of records that need to be filtered and loaded gradually in pages. The package offers an instant solution for javascript handling via Select2, or TomSelect.

Just as in a regular selectbox an array of values is passed, here a model providing the values is passed instead. The universal CallbackQueryModel can serve as the model, resolving everything via callbacks. The package also allows you to create your own custom implementation.

All communication with the backend goes through this model in the very same systematic way — there's no need to create any special AJAX endpoint or presenter action. Loading and filtering data is handled internally by the control through its own signal, just like any other signal in Nette components. Adding addSelectRemote() is therefore, from a usage point of view, exactly the same as adding a regular, non-remote selectbox — the only difference is that a model is passed instead of an array of values.

Installation

composer require tacoberu/nette-form-selectboxremote

Version and requirements

Branch PHP Nette
v2.0 >= 8.1 ^3.2
v1.2 >= 7.4 ^3.1

Usage

Register extension

After registering the extension, the addSelectRemote() and addMultiSelectRemote() shortcuts are available on the container:

extensions:
	- Taco\Nette\Forms\Controls\SelectBoxRemoteExtension

Javascript

The package offers two ready-made handlers, both written in TypeScript and compiled into a JS module that's part of the package (vendor/tacoberu/nette-form-selectboxremote/assets/). The file is also available directly in the repository and needs to be exposed from your public folder (symlink or copy) and imported as an ES module. Filtering for a given field is available after enabling it with the data-class="filterable" attribute on the PHP side:

$form->addSelectRemote('category2', 'Category 2:', $model)
	->controlPrototype->data('class', 'filterable');

Select2

assets/remoteselect2.tsassets/remoteselect2.js, the export initSelect2Impl(el, overrides?) is available.

	<link href="https://cdn.jsdelivr.net/npm/select2@4.0.10/dist/css/select2.min.css" rel="stylesheet" />
	<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
	<script src="https://cdn.jsdelivr.net/npm/select2@4.0.10/dist/js/select2.min.js"></script>

	<script type="module">
		import { initSelect2Impl } from "/js/remoteselect2.js";
		document.querySelectorAll('select')
			.forEach(initSelect2Impl);
	</script>

jQuery is only needed as a dependency of Select2 itself (it's only available as a jQuery plugin) — the initSelect2Impl function itself does not depend on jQuery, it reads attributes through the native element.dataset. Pagination is available thanks to Select2's built-in ajax/pagination.more mechanism.

TomSelect

assets/TomSelectAdapter.tsassets/TomSelectAdapter.js, the export initTomSelectImpl(el, overrides?) is available. Unlike Select2, TomSelect is available as a pure vanilla JS solution, with no dependency on jQuery.

	<link href="https://cdn.jsdelivr.net/npm/tom-select@2.6.1/dist/css/tom-select.css" rel="stylesheet" />
	<script src="https://cdn.jsdelivr.net/npm/tom-select@2.6.1/dist/js/tom-select.complete.min.js"></script>

	<script type="module">
		import { initTomSelectImpl } from "/js/TomSelectAdapter.js";
		document.querySelectorAll('select')
			.forEach(initTomSelectImpl);
	</script>

Pagination is available thanks to the official virtual_scroll plugin (included in tom-select.complete.min.js) and is switched on automatically for fields with data-type="remoteselect".

Further configuration (overrides)

Both functions, initSelect2Impl and initTomSelectImpl, offer a second, optional parameter — an object that gets merged with the automatically derived settings. This makes any other feature of the given library available, for example reordering items in a multiselect (the drag_drop plugin for TomSelect) or the ability to add a new record (tags: true for Select2, create: true for TomSelect):

document.querySelectorAll('select').forEach((el) => {
	const overrides = el.multiple ? { plugins: ['drag_drop'], create: true } : {};
	initTomSelectImpl(el, overrides);
});

For TomSelect, plugins from the overrides are merged with virtual_scroll, which cannot be disabled — without it, pagination over AJAX would not be available.

Decorating items

The model can return arbitrary extra fields per item on top of id/label (e.g. flag, icon, description) — the library passes them through unchanged all the way to the JS layer. Rendering them (an icon, a flag, a description under the label, ...) is then up to your own template callback, enabled through the same overrides parameter:

// Select2: templateResult/templateSelection receive the whole item object.
initSelect2Impl(el, {
	templateResult: (data) => `${data.flag ?? ''} ${data.label}`,
});

// TomSelect: render.option/render.item receive the item object and an escape() helper.
initTomSelectImpl(el, {
	render: { option: (data, escape) => `<div>${escape(data.flag ?? '')} ${escape(data.label)}</div>` },
});

The example application demonstrates this on the countries field (flag + country name) and the accounts field (icon on the left, label and description stacked below it) — see DashboardPresenter::getCountrySelectModel() / getAccountSelectModel() and the corresponding <script type="module"> in select2.latte / tomselect.latte.

Usage in PHP

$form = new Nette\Forms\Form;

// CallbackQueryModel is buildin implementation of generic QueryModel.
$categorySelectQueryModel = new CallbackQueryModel(function($term, $page, $pageSize) use ($data) {
	$results = [];
	foreach ($data as $x) {
		if ($term && stripos($x->label, $term) === False) {
			continue;
		}
		$results[] = (object) [
			'id' => $x->id,
			'label' => $x->label,
		];
	}
	$total = count($results);
	$offset = ($page - 1) * $pageSize;
	return (object) [
		'total' => $total,
		'items' => array_slice($results, $offset, $pageSize),
	];
}, function($id) use ($data) {
	foreach ($data as $x) {
		if ($x->id === $id) {
			return (array) $x;
		}
	}
	return NULL;
});

// ...

$form->addSelectRemote('category', 'Category:', $categorySelectQueryModel);
$form->addMultiSelectRemote('tags', 'Tags:', $tagsSelectQueryModel);

This is what the result looks like for Select2

This is what the result looks like for TomSelect

Example application

examples/ contains a complete sample Nette application with two pages — /dashboard/select2 and /dashboard/tomselect — demonstrating the same PHP form handled by both JS libraries.

composer install
cp .env-example .env
php -S localhost:8001 examples/document_root/router.php

The application is then available at the address from .env (APP_URL).

Building assets (TypeScript)

npm run build:assets

The command compiles every *.ts file in assets/ (currently remoteselect2.ts and TomSelectAdapter.ts) into the matching .js file next to it.

E2E tests (Playwright)

npm install
npx playwright install chromium  # first time only

npm run test:e2e        # run the tests
npm run test:e2e:ui     # interactive UI

Two test suites are available: tests/e2e/remote-select.spec.ts for the Select2 variant and remote-select-tomselect.spec.ts for the same scenarios (loading, loading more, filtering) with TomSelect. The address of the tested application is set in .env via APP_URL, output is stored in temp/.

tacoberu/nette-form-selectboxremote 适用场景与选型建议

tacoberu/nette-form-selectboxremote 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 tacoberu/nette-form-selectboxremote 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: JavaScript

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-24