@trendoman,
Thanks for the post you have shared from Stackoverflow!
Replying in accordance:
1) Summarize the problem
- Include details about your goal
- Describe expected and actual results
- Include any error messages
My Problem Summary: I am working on a system where each system user will generate a report which will be in the CSV format.
Goal Details and Expected and actual results:
The export csv helps create a single file. Everytime a new export is requested, the file is overwritten due to the truncate='1'.
Now the system I am working on, is a system where each user can request their own export. But since only one file is generated the file gets overwritten. I tried adding username to the file name but cms:write does not let that happen. It tends to take hardcoded value.
So can it be possible to:
1. Generate a new file everytime an export is requested which is userwise.
2. Save the file on the client machine rather than the server.
3. Dynamically generate a name for the exported file.
4. Download through ajax success
2) Describe what you’ve tried
I have tried the standard as in
this post.
3) Show some code
I actually didnt do this coz a lot of times putting code does not help at all. Nevertheless I am sharing some now. This code is how I have created it according to my requirement, but has nothing to do with what I asked earlier. Its just implementation mehod. I hope this helps replicate the issue as mentioned.
I have a button to initiate an AJAX call:
- Code: Select all
<button id="report" class="btn gxcpl-btn-outline-dark-primary gxcpl-button shadow" type="button">
<i class="fa fa-cloud-download"></i> Download
</button>
The AJAX:
- Code: Select all
<script type="text/javascript">
// Report AJAX
$(function(){
$('#report').click(function(e){
e.preventDefault();
var elem = $(this);
$.ajax({
type: 'POST',
url: "<cms:show k_site_link />support-files/ajax-generate-report.php",
data: {
report_from_date : "<cms:gpc 'report_from_date' />",
report_to_date : "<cms:gpc 'report_to_date' />",
report_status : "<cms:gpc 'report_status' />",
report_owner : "<cms:pages masterpage=k_user_template id=k_user_id limit='1'><cms:show k_user_name /></cms:pages>",
export : "1"
},
success: function(file) {
// if (file != '') {
// window.open(file);
// }
}
});
return false;
});
});
// Report AJAX
</script>
The CSV Exporter Code:
- Code: Select all
<?php require_once( '../couch/cms.php' ); ?>
<cms:template title="AJAX - Report Generation" order="10" />
<cms:set save_report_from_date="<cms:gpc 'report_from_date' method='post' />" scope="global" />
<cms:set save_report_to_date="<cms:gpc 'report_to_date' method='post' />" scope="global" />
<cms:set save_report_status="<cms:gpc 'report_status' method='post' />" scope="global" />
<cms:set save_report_owner="<cms:gpc 'report_owner' method='post' />" scope="global" />
<cms:set save_export="<cms:gpc 'export' method='post' />" scope="global" />
<cms:if k_logged_out>
<cms:redirect url="<cms:show k_login_link />" />
</cms:if>
<cms:if save_report_status eq '-'>
<cms:set show_save_report_status = " | ctr_indate>=<cms:show save_report_from_date /> | ctr_outdate <= <cms:show save_report_to_date />" scope="global" />
<cms:else />
<cms:set show_save_report_status = " | ctr_indate>=<cms:show save_report_from_date /> | ctr_outdate <= <cms:show save_report_to_date /> | ctr_status == <cms:show save_report_status />" scope="global" />
</cms:if>
<cms:if save_export eq "1">
<cms:pages masterpage="index.php" paginate="1" limit="100" show_future_entries="1" order="asc" custom_field="ctr_owner=<cms:show save_report_owner /><cms:show show_save_report_status />" >
<cms:if k_paginated_top >
<cms:if k_current_page='1'>
<cms:write 'report.csv' add_newline='1' truncate='1'>Sr.No.,Name,Mobile,Status</cms:write>
</cms:if>
<cms:if k_paginate_link_next >
<script language="JavaScript" type="text/javascript">
var myVar;
myVar = window.setTimeout( 'location.href="<cms:show k_paginate_link_next />";', 100 );
</script>
<button onclick="clearTimeout(myVar);">Stop</button>
<cms:else />
<cms:set write_footer='1' 'global'/>
Done!
</cms:if>
<h3><cms:show k_current_page /> / <cms:show k_total_pages /> pages (Total <cms:show k_total_records /> records. Showing <cms:show k_paginate_limit /> records per page)</h3>
</cms:if>
<h3><cms:show k_current_record /></h3>
<cms:write 'report.csv' add_newline='1'><cms:format_csv k_absolute_count/>,<cms:format_csv ctr_name/>,<cms:format_csv ctr_mobile/>,<cms:format_csv ctr_status/></cms:write>
<cms:if k_paginated_bottom >
<cms:if write_footer>
<cms:else />
<cms:paginator simple='1' />
</cms:if>
</cms:if>
</cms:pages>
</cms:if>
<?php COUCH::invoke(); ?>
The questions still remain the same as asked earlier. I hope this provides more details as expected. Please let me know if more detail is needed. I would be willing to share if something is needed in specific.
Regards,
GXCPL (Priya)