I see I can use custom action handlers for adminhtml grid actions. This is DOM node for action link (HTML):
<a class="action-menu-item"
data-bind="attr: {href: $action().href},
text: $action().label,
click: $col.getActionHandler($action())"
data-repeat-index="0"
href="http://.../index.php/admin/customer/index/edit/id/1/">Edit</a>
I have created additional action in adminhtml grid with callback data (PHP code):
$actions['alert'] = [
'label' => __('Alert'),
'callback' => 'function() {alert("Action!")}'
];
This action is converted into JSON and is loaded to the grid in this format, where "callback" property is a string (JSON):
"alert": {
"label": "Alert",
"callback": "function() {alert(\"Action!\")}"
}
This is Magento parser for action's props (module-ui/view/base/web/js/grid/columns/actions.js
). This parser process JSON data and should call action's callback in case of callback is object/array/function (see _getCallback
):
getActionHandler: function (action) {
var index = action.index,
rowIndex = action.rowIndex;
if (this.isHandlerRequired(index, rowIndex)) {
return this.applyAction.bind(this, index, rowIndex);
}
},
applyAction: function (actionIndex, rowIndex) {
debugger
var action = this.getAction(rowIndex, actionIndex),
callback = this._getCallback(action);
action.confirm ?
this._confirm(action, callback) :
callback();
return this;
},
_getCallback: function (action) {
var args = [action.index, action.recordId, action],
callback = action.callback;
if (utils.isObject(callback)) {
args.unshift(callback.target);
callback = registry.async(callback.provider);
} else if (_.isArray(callback)) {
return this._getCallbacks(action);
} else if (!_.isFunction(callback)) {
callback = this.defaultCallback.bind(this);
}
return function () {
callback.apply(callback, args);
};
},
But callback
property is a string, so defaultCallback
is used.
How can I use my own action handler in adminhtml grid actions?