You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Yii2框架下ui-sortable表格内Imperavi编辑器点击无法获焦求助

Fixing Imperavi Editor Focus Issue in Yii2 SortableGridView

I’ve run into similar conflicts between sortable components and rich text editors before, so let’s break down what’s happening and how to fix this cleanly:

The Root Cause

The SortableGridView (from yii2-sortable-behavior) binds mousedown, mouseup, and click events to elements in sortable rows to handle drag-and-drop functionality. These events propagate down to the Imperavi editor’s .redactor-box container, interfering with the editor’s ability to capture focus and process user input. When you removed those events manually in Chrome DevTools, you eliminated the conflict directly.

Solutions (Ordered by Elegance & Reliability)

1. Exclude Editor Elements from Sortable Interaction

The cleanest fix is to tell the Sortable component to ignore clicks on the Imperavi editor entirely. Most sortable libraries (including the one powering yii2-sortable-behavior) have a cancel option that lets you specify selectors for elements that shouldn’t trigger sorting.

Update your SortableGridView widget configuration to add this option:

<?= SortableGridView::widget([
    'dataProvider' => $dataProvider,
    'orderUrl' => ['order'],
    'options' => ['class' => 'table'],
    // Add this to exclude editor containers from sortable events
    'sortableOptions' => [
        'cancel' => '.redactor-box, .redactor-editor',
    ],
    'columns' => [
        // ... your existing column definitions ...
    ],
]); ?>

This way, the Sortable component won’t bind its interfering events to the editor’s containers, letting the editor handle focus and input normally.

2. Stop Event Propagation on the Editor Container

If adjusting the Sortable options doesn’t work, you can prevent the editor’s mouse events from bubbling up to the Sortable row. Add this JavaScript to your view:

$(document).on('mousedown mouseup click', '.redactor-box', function(e) {
    // Block the event from reaching the Sortable's row handlers
    e.stopPropagation();
});

This ensures that when you interact with the editor, the Sortable’s drag-and-drop logic doesn’t fire, so the editor can capture focus without interference.

3. Manually Remove Conflicting Events (Last Resort)

If the above methods fail, you can replicate what you did in DevTools by removing the conflicting events after the page loads. Note this is less reliable (the Sortable component might re-bind events later), but it works as a quick fix:

$(document).ready(function() {
    // Add a small delay to ensure both Sortable and Imperavi are fully initialized
    setTimeout(function() {
        $('.redactor-box').off('mousedown mouseup click');
    }, 100);
});

Why These Work

All three approaches target the core conflict: the Sortable component’s mouse events were overriding the editor’s ability to capture focus. By either excluding the editor from Sortable’s scope, stopping event propagation, or removing the conflicting events, you let the Imperavi editor handle user input as intended.

内容的提问来源于stack exchange,提问作者white-imp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:23:34