简体   繁体   English

jquery-jtable:使用ajax / jsp导出选定的行

[英]jquery-jtable: exporting selected lines using ajax/jsp

I need to implement an export to Excel function, but only for selected lines of the jtable, in a JSP environment. 我需要在JSP环境中实现对Excel的导出功能,但仅针对jtable的选定行。

My question is: what is the best way to do it? 我的问题是:最好的方法是什么? From the jtable, I pick the $('#SearchResultTable').jtable('selectedRows') and post it (using jquery $.post() ) to my servlet (that is generating the Excel file using poi). 从jtable中,我选择$('#SearchResultTable')。jtable('selectedRows')并将其发布(使用jquery $ .post())到我的servlet(使用poi生成Excel文件)中。 Everything good until I have to pick up the response from the servlet. 一切都很好,直到我不得不从servlet获取响应为止。 Turns out I cannot save it to disk or prompt the browser to download it, since ajax is basically javascript, and thus have no access to the disk. 事实证明,我无法将其保存到磁盘或提示浏览器下载它,因为ajax基本上是javascript,因此无法访问该磁盘。

Is there a better way to do this? 有一个更好的方法吗? I want to be able to name and save the resulting file. 我希望能够命名并保存生成的文件。

This is my call function which handles the post: 这是我处理该帖子的call函数:

toolbar: {
    items: [{
        icon: 'art/excel_ico.gif',
        text: 'Esporta in Excel',
        display: 'download="Report.xls"',
        click: function() {
            return $.post("ExcelExport", {n: "Magazzino", t: $('#SelectedRowList')[0].innerHTML})
            .done(function(data) {
                alert("Data Loaded: " + data);
            });
        }
    }]
},

Thank you for your help, Fabio. 谢谢您的帮助,法比奥。

Solved. 解决了。

I used a form instead of the "toolbar:" item in JTable. 我在JTable中使用了表单而不是“ toolbar:”项。 To whoever may be interested: 对任何可能感兴趣的人:

I removed the "toolbar" item from JTable init function. 我从JTable初始化函数中删除了“工具栏”项。 Added a form to the page, like this: 在页面上添加一个表单,如下所示:

<div id="formWrapper">
    <form id="exportForm" action="ExcelExport" method="post">
        <input type="hidden" value="" name="exportRows" id="exportRows">
        <input type="hidden" value="Ordini" name="dataType" id="dataType">
        <input type="submit" value="Esporta">
    </form>
</div>

Styled the form using css to make it look like the button on the JTable toolbar. 使用css为表单设置样式,使其看起来像JTable工具栏上的按钮。

In the JTable init, I added a selectionChange item, like this: 在JTable初始化中,我添加了selectionChange项,如下所示:

selectionChanged: function() {
  var $selectedRows = $('#SearchResultTable').jtable('selectedRows');
  $('input:hidden[name="exportRows"]').val("");

  $selectedRows.each(function() {
    var record = $(this).data('record');

    $('input:hidden[name="exportRows"]').val($('input:hidden[name="exportRows"]').val()
        + record.id. ... );
  });
}

So every time I change something on the JTable selection, the value of exportRows changes. 因此,每当我更改JTable选择中的内容时,exportRows的值都会更改。 The form points to my servlet that can then send back the data as an Excel file, which I can name and save. 该表单指向我的Servlet,然后可以将其作为Excel文件发送回数据,我可以对其进行命名和保存。

Hope it helps. 希望能帮助到你。 Fabio. 法比奥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM