cancel tab closing in p:accordionPanel

本秂侑毒 提交于 2019-12-11 13:23:14

问题


I have a p:accordionPanel and inside of each tab of the panel there is some info that the user can manipulate, what i need to do is if the user close the tab show a confirm dialog (before the tab get closed) whit something like "are you sure you wanna close the tab? if you do your changes will be lost". here is what i tried

<p:ajax event="tabClose" onstart="return myFunction()" 
listener" {myBean.myMethod}" process="@this" />

function myFunction() {
    var answer = confirm("are you sure you wanna close the tab? if you do your changes will be lost");
    if(answer){
        //some logic 
        return true;
    }else{
        //some logic
        return false;
    }
}

The problem is that if i choose cancel on the confirm dialog the tab get close anyway. Shouldn't the tab closing be canceled by the onStart="return false"? is there a way to achieve what i'm trying to do?


回答1:


Finally I solved my problem, apparently the onStart="return false" does not prevent the tab from change its statatus but the onTabChange attribute of the p:accordionPanel does, the only problem is that for some reason the onTabchange event of the accordion don't get execute when the tab is been closed just when the tab is been opened so i have to override the acordionPanel unselect function of primefaces to call the onTabChange event

PrimeFaces.widget.AccordionPanel.prototype.unselect = (function(index) {
  var cached_function = PrimeFaces.widget.AccordionPanel.prototype.unselect;

  return function() {
    var panel = this.panels.eq(index);

    if(this.cfg.onTabChange) {
        var result = this.cfg.onTabChange.call(this, panel);
        if(result === false)
            return false;
    }

    var result = cached_function.apply(this, arguments); 

    return result;
  };
})();

then i only had to placed myFunction(); on the onTabChange event of the accordiong panel (onTabChange="return myFunction()")and it works.



来源:https://stackoverflow.com/questions/37423527/cancel-tab-closing-in-paccordionpanel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!