hypejunction / elgg_tokeninput
Elgg 的自动补全分词
Requires
- bower-asset/jquery-tokeninput: ~1.7.0
Requires (Dev)
- composer/installers: ~1.0
README
Elgg 的核心自动补全和用户选择器的替代品
截图
功能
- 用户友好的自动补全和用户选择器输入
- 与实体和元标签协同工作
- 开发者友好
- 单选和多选
- 自由标记
- 标记拖放排序
备注
I/O
JS 插件的默认行为是将用户输入合并成一个以逗号分隔的字符串。PHP 插件钩子 'action', 'all' 将尝试拆分这些值并将它们反馈回动作进行进一步处理。但是,这只适用于基本表单输入名称,例如 name="field_name"
。如果您正在处理更复杂的表单,例如 name="field_name[element_name]"
,您需要在您的动作中添加一些自定义逻辑。
AJAX 请求
要初始化成功 AJAX 请求上的标记输入,请使用 $('.elgg-input-tokeninput').trigger('initialize');
结果和标记格式
服务器端
使用 'tokeninput:entity:export', $entity_type
插件钩子来修改 JSON 输出。您可以添加 'html_result' 和 'html_token' 参数来自定义输出。
客户端
使用 'results:formatter', 'tokeninput'
和 'results:formatter', 'tokeninput'
钩子。
示例
示例 1
创建一个输入,允许用户搜索和选择多个文件附加到实体
向表单添加输入
echo elgg_view('input/tokeninput', array( 'value' => $current_attachment_guids, // An array of values (guids or entities) to pre-populate the input with 'name' => 'attachment_guids', 'callback' => 'my_search_files_callback', 'query' => array('simpletype' => 'image'), 'multiple' => true ));
添加回调函数
function my_search_files_callback($query, $options = array()) { $user = elgg_get_logged_in_user_entity(); $simpletype = get_input('simpletype'); $query = sanitize_string($query); // replace mysql vars with escaped strings $q = str_replace(array('_', '%'), array('\_', '\%'), $query); $dbprefix = elgg_get_config('dbprefix'); $options['types'] = array('object'); $options['subtypes'] = array('file'); $options['joins'][] = "JOIN {$dbprefix}objects_entity oe ON oe.guid = e.guid"; $options['wheres'][] = "oe.title LIKE '%$q%'"; $options['wheres'][] = "e.owner_guid = $user->guid"; if ($simpletype) { $options['metadata_name_value_pairs'] = array( 'name' => 'simpletype', 'value' => $simpletype ); } return elgg_get_entities_from_metadata($options); }
在您的动作文件中
$attachment_guids = get_input('attachment_guids'); if (is_string($attachment_guids)) { $attachment_guids = explode(',', $attachment_guids); } if (is_array($attachment_guids)) { foreach ($attachment_guids as $attachment_guid) { make_attachment($entity->guid, $attachment_guid); } }
升级
到 4.x
-
elgg.tokeninput
库已被删除。elgg_load_library('elgg.tokeninput')
调用会导致 WSOD,只需将其删除即可 -
tokeninput/init
AMD 模块已被删除。Tokeninput 在页面加载和 AJAX 请求时按需引导 -
define('ELGG_TOKENINPUT_PAGEHANDLER', 'tokeninput');
已被删除。如果您需要修改页面处理器标识符,请注销页面处理器并注册一个新的处理器
归属/致谢
- jquery.tokeninput http://loopj.com/jquery-tokeninput/