Primefaces排序仅在过滤后起作用

时间:2017-01-19 10:38:18

标签: jsf primefaces

我是JSF开发和Primefaces的新手。我的问题是我在数据表中列出了数据,但是当点击箭头时数据没有被排序。如果您过滤数据表,然后单击箭头一切正常。 任何提示?

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:body>
    <h:link value="INSERT NEW Customer" outcome="/kunde/insert" />
    <h:form id="form">
        <p:growl id="msgs" showDetail="true" />
        <p:dataTable widgetVar="kundeTable" var="kunde" value="#
{kundeController.findAll()}" lazy="false">
            <f:facet name="header">
                <p:outputPanel>
                    <h:outputText value="Search all fields" />
                    <h:inputText id="globalFilter" 
onkeyup="PF('kundeTable').filter()" />
                </p:outputPanel>
            </f:facet>

            <p:column sortBy="#{kunde.id}" filterBy="#{kunde.id}">
                <f:facet name="header">Id</f:facet>
                <h:outputText value="#{kunde.id}" />
            </p:column>
            <p:column sortBy="#{kunde.name}" filterBy="#{kunde.name}" >
                <f:facet name="header">Name</f:facet>
                <h:outputText value="#{kunde.name}" />
            </p:column>

            <p:column sortBy="#{kunde.strasse}" filterBy="#{kunde.strasse}" >
                <f:facet name="header">Strasse</f:facet>
                <h:outputText value="#{kunde.strasse}" />
            </p:column>

            <p:column sortBy="#{kunde.plz}" filterBy="#{kunde.plz}" >
                <f:facet name="header">Plz</f:facet>
                <h:outputText value="#{kunde.plz}" />
            </p:column>

            <p:column sortBy="#{kunde.ort}" filterBy="#{kunde.ort}" >
                <f:facet name="header">Ort</f:facet>
                <h:outputText value="#{kunde.ort}" />
            </p:column>

            <p:column>
                <f:facet name="header">DELETE</f:facet>
                <h:commandLink value="Löschen" action="#{kundeController.delete(kunde)}" onclick="return confirm('Wollen Sie wirklich diese Person löschen?')" />
            </p:column>
            <p:column>
                <f:facet name="header">UPDATE</f:facet>
                <h:commandLink value="Bearbeiten" action="#{kundeController.update(kunde)}" />
            </p:column>
        </p:dataTable>
    </h:form>
</h:body>

2 个答案:

答案 0 :(得分:0)

在大多数情况下,问题是使用源/二进制格式1.8 ... 你可以通过在pom.xml中更改它来解决这个问题:

let BATCH_SIZE = 500
Array.prototype.chunk = function (groupsize) {
    var sets = [];
    var chunks = this.length / groupsize;

    for (var i = 0, j = 0; i < chunks; i++ , j += groupsize) {
        sets[i] = this.slice(j, j + groupsize);
    }

    return sets;
}

function upsertDiscountedProducts(products) {

    //Take the input array of products and divide it into chunks of BATCH_SIZE

    let chunks = products.chunk(BATCH_SIZE), current = 0

    console.log('Number of chunks ', chunks.length)

    let bulk = models.Product.collection.initializeUnorderedBulkOp();

    //Get the current time as timestamp
    let timestamp = new Date(),

        //Keep track of the number of items being looped
        pendingCount = 0,
        inserted = 0,
        upserted = 0,
        matched = 0,
        modified = 0,
        removed = 0,

        //If atleast one upsert was performed
        upsertHappened = false;

    //Call the load function to get started
    load()
    function load() {

        //If we have a chunk to process
        if (current < chunks.length) {
            console.log('Current value ', current)

            for (let i = 0; i < chunks[current].length; i++) {
                //For each item set the updated timestamp to the current time
                let item = chunks[current][i]

                //Set the updated timestamp on each item
                item.updatedAt = timestamp;

                bulk.find({ _id: item._id })
                    .upsert()
                    .updateOne({
                        "$set": item,

                        //If the item is being newly inserted, set a created timestamp on it
                        "$setOnInsert": {
                            "createdAt": timestamp
                        }
                    })
            }

            //Execute the bulk operation for the current chunk
            bulk.execute((error, result) => {
                if (error) {
                    console.error('Error while inserting products' + JSON.stringify(error))
                    next()
                }
                else {

                    //Atleast one upsert has happened
                    upsertHappened = true;
                    inserted += result.nInserted
                    upserted += result.nUpserted
                    matched += result.nMatched
                    modified += result.nModified
                    removed += result.nRemoved

                    //Move to the next chunk
                    next()
                }
            })



        }
        else {
            console.log("Calling finish")
            finish()
        }

    }

    function next() {
        current++;

        //Reassign bulk to a new object and call load once again on the new object after incrementing chunk
        bulk = models.Product.collection.initializeUnorderedBulkOp();
        setTimeout(load, 0)
    }

    function finish() {

        console.log('Inserted ', inserted + ' Upserted ', upserted, ' Matched ', matched, ' Modified ', modified, ' Removed ', removed)

        //If atleast one chunk was inserted, remove all items with a 0% discount or not updated in the latest upsert
        if (upsertHappened) {
            console.log("Calling remove")
            remove()
        }


    }

    /**
     * Remove all the items that were not updated in the recent upsert or those items with a discount of 0
     */
    function remove() {

        models.Product.remove(
            {
                "$or":
                [{
                    "updatedAt": { "$lt": timestamp }
                },
                {
                    "discount": { "$eq": 0 }
                }]
            }, (error, obj) => {
                if (error) {
                    console.log('Error while removing', JSON.stringify(error))
                }
                else {
                    if (obj.result.n === 0) {
                        console.log('Nothing was removed')
                    } else {
                        console.log('Removed ' + obj.result.n + ' documents')
                    }
                }
            }
        )
    }
}

用这个:

<configuration>
     <source>1.8</source>
     <target>1.8</target>
     <compilerArguments>
          <endorseddirs>${endorsed.dir}</endorseddirs>
     </compilerArguments>
</configuration>

然后只需清理并构建项目并试一试;)

此致

答案 1 :(得分:0)

我遇到了同样的问题。作为一种解决方法,您可以在页面加载后调用表的过滤器函数:

            <p:remoteCommand autoRun="true" oncomplete="PF('kundeTable').filter();" />

将其放在</p:dataTable>结束标记之后。