I'm creating a module that uses a dynamic-rows component similar to the one used for advanced pricing in the product add/edit form.
I need to add some custom javascript code when the new row is added to the list of existing rows (Add button is clicked).
A simple alert('Good Job!') should do it. I can take it from there.
I don't want to bore you with my custom code because that's not important for now.
So I am asking...
How can I add a js alert when pressing the add button?
I don't mind if your recommendations suggest a core edit. I know how to adapt it to my own module (I think).
I just need a place to start.
[Edit].
I realized I didn't add any code.
The code for the advanced pricing dynamic rows can be found here
1 Answer 1
First, we decided to use events that can be activated on a button hit. However, nothing happened when we added a child. We checked this by adding the event console.log() class to the static:
pub/static/adminhtml/Magento/backend/en_US/Magento_Ui/js/lib/core/events.js
/**
* Calls callback when name event is triggered.
* @param {String} events
* @param {Function} callback
* @return {Object} reference to this
*/
on: function (events, callback, ns) {
var iterator;
if (arguments.length < 2) {
ns = callback;
}
console.log('events: ');
console.log(events);
iterator = addHandler.bind(null, this, ns);
_.isObject(events) ?
_.each(events, iterator) :
iterator(callback, events);
return this;
},
and added a log to the processingAddChild dynamic-rows method:
pub/static/adminhtml/Magento/backend/en_US/Magento_Ui/js/dynamic-rows/dynamic-rows-grid.js
processingAddChild: function (ctx, index, prop) {
console.log('processingAddChild fire');
if (this._elems.length > this.pageSize) {
return false;
}
this.showSpinner(true);
this.addChild(ctx, index, prop);
console.log('processingAddChild END\'S');
},
The result: enter image description here
Some additional options that you may think of:
- Replace this button for your own with a js-component expanding this button. Hence, you'll get full control over it.
- Use simple events like click for home-elements (probably, not the best decision).
- Replace a js-component for a dynamic row for your own and add the
code you need at the beginning of the
processingAddChildmethod processing and then call the parent method(this._super()).
The first and third options look like the best solution if you need to add your own elements and not to modify the existing Magento code.
-
2Duh! Of course that was the solution. Extending the dynamic rows component. Thank you. Just for the record, I decided to change the function
addChildinstead ofprocessingAddChildbecause I need to do something right after a child row is added. This also solves my next question of how I can add code when a row is deleted. Thank you again.Marius– Marius2016年10月03日 11:17:05 +00:00Commented Oct 3, 2016 at 11:17 -
1Hi @marius, can you provide some instructions on how you extended the dynamic rows component?kolucciy– kolucciy2017年01月08日 15:30:23 +00:00Commented Jan 8, 2017 at 15:30
-
Is there any option to manage dynamic rows in product adding section with store view scope?Ranganathan– Ranganathan2018年11月21日 12:39:12 +00:00Commented Nov 21, 2018 at 12:39
-
@Marius, how can we limit dynamic rows to 10 rows maximum? just need to show error message if rows are more than 10Jafar Pinjar– Jafar Pinjar2021年01月27日 16:57:05 +00:00Commented Jan 27, 2021 at 16:57