Xpages attach event to partial refresh of pager in Data View

守給你的承諾、 提交于 2019-12-12 04:02:26

问题


In a previous post I asked how to add a bootstrap class to a Data View. The answer was to add the class to the "table.dataview" in a script block. After the table is created the class is applied and all is well.

But when I use a pager the formatting disappears. I am using a partial refresh on the pager to only refresh the data table but doing so means that the bootstrap class does not get applied to the table.

I believe I need to add an event handler that will attach to the refresh action of the dataView to add the class. However I cannot get the event handler to work.

My code for the event handler is below.

<xp:eventHandler refreshMode="partial" submit="true"
            id="applyCSS" refreshId="dataView1" event="parialRefresh"
            value="what" loaded="false">
            <xp:this.binding><![CDATA[#{javascript:"pager1"}]]></xp:this.binding>
            <xp:this.action><![CDATA[#{javascript:("table.dataview").addClass("table-striped table-hover table-bordered table-condensed")}]]></xp:this.action>
        </xp:eventHandler>

Oliver, the rendered=false was simply a typo - I was testing something and needed to temporarily suppress that.

Oliver and Paul,

Last night I was able to get the partial refresh to work.

I ran across this post by Mark Roden which explained how to do it. There were two different ways to accomplish this, one less and one more efficient. The code I used is below.

<xp:scriptBlock id="scriptBlock3">
            <xp:this.value><![CDATA[$('.dataView1PanelWrapper').on("DOMNodeInserted", function(){
    $("table.dataview").addClass("table-striped table-hover table-bordered table-condensed")
})]]></xp:this.value>
        </xp:scriptBlock>

However, and isn't there almost always a however in Xpages, I have some sortable columns in the view and clicking on the sort brings up the same problem! I lose the class assignment!

So now I would have to intercept that event too, right?

Concerned where this will end. Don't like the idea of DOM Manipulation, and only want to do it if I have too.

I started by using a simple view. It worked great, but for some reason the spacing was messed up in the pagers. I found that by moving the pagers out of the view itself, I was able to get the alignment issue fixed. I think it would be better just to use a view, as I can assign the class directly and won't have to do all this manipulation. It is however very good to know how to do this for the future.

Is that what you would suggest?

================================================== I have tried Paul Withers suggestion using an output script. This works on the initial page load, but not on any other changes to the data view - when the pager fires, or sorting or any of that. I am close, but no cigar yet. Any suggestions?

<xp:scriptBlock id="scriptBlock5" loaded="false">
        <xp:this.value><![CDATA[dojo.require("dojo.behavior");

Behavior = {
    ".dataview": {
        found: function(node) {

            dojo.addClass(node,".table-striped table-hover table-bordered table-condensed");
            //node.addClass("table-striped table-hover table-bordered table-condensed");
        }
    }
}

dojo.ready(function() {
    dojo.behavior.add(Behavior);
    dojo.behavior.apply();
});

//Make sure that future pagers are also straightened out
dojo.subscribe("partialrefresh-complete", null, function(method, form, refreshId) {
    dojo.behavior.apply();
});]]></xp:this.value>
    </xp:scriptBlock>

回答1:


Move your existing xp:scriptBlock with the working code inside a facet of the xe:dataView. Then the styling will get applied on initial load and on all partial refreshes.




回答2:


You should call your CSJS stuff to add the class in the onComplete property of the event handler - hard to find, just highlight the event handler object in source code or outline and then open "All properties" to find the "onComplete" property. This event allows CSJS to be called.

BTW: why is the loaded property = false? The event will never we rendered.




回答3:


dojo.behavior, dojo.ready and dojo.subscribe should allow you to manage this. dojo.behavior allows you to define a particular behaviour for a particular collection of elements which will be retrieved via a Dojo query. dojo.ready will (I believe) run the code when the page initially loads and dojo.subscribe("partialrefresh-complete", null, function(method, form, refreshId) {...} will run the code aftedr a partial refresh.

Here's sample code I used for converting a DataView's category column images to Font Awesome icons, so the Behavior = {...} bit will need amending.

Behavior = {
    // Convert xp:pagers to Bootstrap
    ".catColumn a img": {
        found: function(img_node) {
            var imgSrc = dojo.getNodeProp(img_node, "alt").toLowerCase();
            if (imgSrc.indexOf("collapse") >= 0) {
                dojo.place('<i class="fa fa-minus-square"></i>&nbsp;', img_node, 'replace');
            } else {
                dojo.place('<i class="fa fa-plus-square"></i>&nbsp;', img_node, 'replace');
            }
        }
    }
}

dojo.ready(function() {
    dojo.behavior.add(Behavior);
    dojo.behavior.apply();
});

//Make sure that future pagers are also straightened out
dojo.subscribe("partialrefresh-complete", null, function(method, form, refreshId) {
    dojo.behavior.apply();
});


来源:https://stackoverflow.com/questions/34615735/xpages-attach-event-to-partial-refresh-of-pager-in-data-view

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