Page 1 of 1

Sheet contextMenu disapear after sheet update

Posted: 26 Jul 2018, 12:38
by bug007
Hi,
version 6.2.7
I would like to try use contextMenu like showcase, this work fine on the fisrt load, but when I update data on the sheet, contextMenu is be lost !
How can I create it and always stay ? It should be great to don't have to recreated at each refresh ?
Thanks for your help

Re: Sheet contextMenu disapear after sheet update

Posted: 26 Jul 2018, 13:27
by Melloware
I think there is 1 of 2 ways to solve it.

1. Put in in a JS function and call it after your Ajax Update like...

Code: Select all

<p:ajax update="mySheet" oncomplete="addContextMenuJs();" />
2. Instead of using $hot.updateSettings doing it in the Sheet Extender funciton. The Extender should get called on every update of the sheet when it re-initializes.

Re: Sheet contextMenu disapear after sheet update

Posted: 26 Jul 2018, 13:59
by Melloware
Now that I think about it you may have to use Solution #1 because in #2 the sheet doesn't exist yet the Extender is before the sheet is created so there is no way to get at the $hot value.

Re: Sheet contextMenu disapear after sheet update

Posted: 26 Jul 2018, 15:13
by bug007
Thanks Melloware, it's exact, on the extender I can only use standard declaration like :

Code: Select all

this.cfg.contextMenu = ['row_above', 'row_below', '---------', 'undo', 'redo']; 
but not your.

Than I've implemented the 1 solution.
For your information, I saw an error with contextMenu if the sheet is empty.

Now, I look the better way for insert (after or before) a "Template" line with default value by backing bean to integrate it on the same time into the database table .... Any experience ?

Re: Sheet contextMenu disapear after sheet update

Posted: 26 Jul 2018, 15:35
by Melloware
Yeah the context menu you might have to get smarter with the JS code. I grabbed their example almost exactly from the HandsonTable docs...

https://docs.handsontable.com/5.0.0/dem ... -menu.html

Re: Sheet contextMenu disapear after sheet update

Posted: 21 Aug 2018, 18:17
by bug007
Hi, with the solution to add contxet menu after each change, I found a handsontable issue => event hooks calling more time than once !
Similar problem of https://github.com/handsontable/handson ... ssues/3343

Than I look how is it posssible to re add context menu and also hook.
Here my patch exemple solution for remove row :

Code: Select all

$(document).ready(function () {
                            addContextMenu();
                        });

                        function addContextMenu() {
                            var $hot = PF('listSheet').ht;
                            $hot.removeHook('afterRemoveRow', cbRemoveRow); // this is very important !
                            $hot.updateSettings({
                                contextMenu: {                                    
                                 items: {
                                        "remove_row": {
                                            name: 'Delete line(s)',
                                            disabled: function () {
                                                // if first row, disable this option
                                                try {
                                                    return $hot.getSelected()[0] === 0;
                                                } catch (error) {
                                                    return true;
                                                } 
                                            }
                                       }
                                }
                            });
                            $hot.addHook('afterRemoveRow', cbRemoveRow);
                        }
                        ;
                        
                        function cbRemoveRow (index, amount){
                                deleteRows([{name: 'index', value: index}, {name: 'amount', value: amount}]);
                            };
Hope this can help someone !

Re: Sheet contextMenu disapear after sheet update

Posted: 21 Aug 2018, 19:10
by Melloware
Excellent!