在Vaadin中调用FileDownloader时,请使用过滤后的dataProvider内容 [英] Use filtered dataProvider contents when FileDownloader is called in Vaadin

查看:104
本文介绍了在Vaadin中调用FileDownloader时,请使用过滤后的dataProvider内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将过滤器应用于DataProvider之后,我正在尝试下载csv文件.

I'm trying to download a csv file after applying filters to the DataProvider.

由于某种原因,过滤后的结果显示在网格中,但是下载的csv文件仍然包含所有数据.

For some reason the filtered results are shown in the Grid, but the downloaded csv file still contains all data.

@AutoView
class FinancialTransactionsView : VerticalLayout(), View {    
    private val grid: Grid<FinancialTransaction>
    private val yearField: ComboBox<Int>

    private val dataProvider = DataProvider.ofCollection(FinancialTransaction.findAll())
    private val fileDownloader: FileDownloader

    init {
        label("Financial Transactions") {
            styleName = ValoTheme.LABEL_H1
        }

        yearField = comboBox("Select Year") {
            setItems(listOf(2016, 2017, 2018))

            addSelectionListener {
                // Filter the data based on the selected year
                if (it.value != it.oldValue) setDataProvider()
            }
        }

        // Create FileDownloader and initialize with all contents in the DataProvider
        fileDownloader = FileDownloader(createCsvResource())

        val downloadButton = button("Download csv") {
            styleName = ValoTheme.BUTTON_PRIMARY

            onLeftClick {
                // The idea here is to assign values from the filtered DataProvider to the FileDownloader
                fileDownloader.fileDownloadResource = createCsvResource()
            }
        }

        fileDownloader.extend(downloadButton)
        fileDownloader.fileDownloadResource = createCsvResource()

        grid = grid(dataProvider = dataProvider) {

            expandRatio = 1f
            setSizeFull()
            addColumnFor(FinancialTransaction::companyId)
            addColumnFor(FinancialTransaction::fiscalYear)
            addColumnFor(FinancialTransaction::fiscalPeriod)
            addColumnFor(FinancialTransaction::currency)
            addColumnFor(FinancialTransaction::finalizedDebitAmountInCurrency)
            addColumnFor(FinancialTransaction::finalizedCreditAmountInCurrency)

            appendHeaderRow().generateFilterComponents(this, FinancialTransaction::class)
        }
    }

    private fun createCsvResource(): StreamResource {
        return StreamResource(StreamResource.StreamSource {

            val csv = dataProvider.items.toList().toCsv()

            try {
                return@StreamSource csv.byteInputStream()
            } catch (e: IOException) {
                e.printStackTrace()
                return@StreamSource null
            }
        }, "financial_transactions.csv")
    }

    private fun setDataProvider() {
        dataProvider.clearFilters()

        if (!yearField.isEmpty)
            dataProvider.setFilterByValue(FinancialTransaction::fiscalYear, yearField.value)
    }
}

toCsv()是扩展函数 List< FinancialTransaction> ,它返回包含csv数据的字符串.

toCsv() is an extension function List<FinancialTransaction> which returns a string containing csv data.

我该怎么做才能在csv文件中获取过滤后的结果?

What can I do to get the filtered results in my csv file?

推荐答案

val csv = dataProvider.items.toList().toCsv()

我不是Kotlin家伙,但我认为dataProvider.items是Java中dataProvider.getItems()的简写,即此方法(您使用ListDataProvider)

I am not Kotlin guy, but I assume dataProvider.items is a shorthand to dataProvider.getItems() in Java, i.e. this method (and you use ListDataProvider)

在Vaadin中,getItems()通过传递所有过滤器返回所有项目.

In Vaadin getItems() returns all items by passing all filters.

因此,您应该执行以下任一操作

So instead you should do either of the following

dataProvider.fetch(..)

https://vaadin.com/download/release/8.4/8.4.1/docs/api/com/vaadin/data/provider/DataProvider.html#fetch-com.vaadin.data.provider.Query-

在何处提供要在查询中应用的过滤器,或

Where you give the filters you want to apply in the query, or

grid.getDataCommunicator.fetchItemsWithRange(..)

https://vaadin.com/download/release/8.4/8.4.1/docs/api/com/vaadin/data/provider/DataCommunicator.html#fetchItemsWithRange-int-int-

哪个返回您已应用过滤器的项目列表,我认为这对您来说是理想的

Which returns list of items with filters you have set applied, which I think is ideal for you

这篇关于在Vaadin中调用FileDownloader时,请使用过滤后的dataProvider内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆