Laravel 5 - Download Excel file of a View - excel

I'm working on adding an export/downloading feature to a Laravel 5 application. I found the Laravel-Excel library (http://www.maatwebsite.nl/laravel-excel/docs), which seems perfect -- ideally, I will be able to take a Laravel View or html and make a downloadable Excel file.
So far, I've gotten the creation and storage to work. I can create an Excel file, and it's okay (it's the right data and View), and on the server in storage/exports/ I see "Report.xls" - which is right save path. The issue I'm having is I cannot seem to download the file through the browser after creating it.
The Laravel-Excel library has ->download('xls') and ->export('xls') methods, but these only seems to return the raw content, which is visible in the developer's console:
On the right, you can see the file was created and stored as "Report.xls", and presumably the raw content is the right content - but I don't know why it's just spitting raw content into the console. I can open the Report.xls file in Excel, so I know it's not corrupted.
I also tried to use the Laravel 5 Response::download() function and set headers for the download, but it also spit out raw content in the console instead of downloading the file:
The code I'm using is an AJAX call that hits a Controller method, and the controller method just triggers a Service method called "downloadReportFile()":
public function downloadReportFile($requestData, $fileType) {
$configJSON = \Report::loadConfigFile($requestData['reportName']);
$today = Carbon::today()->format('Y-m-d');
$FileViewdata = array(
'config' => $configJSON,
'html' => $requestData['report_html']
);
\Excel::create("Report", function($excel) use ($FileViewdata) {
$excel->setTitle($FileViewdata['config']['title']);
$excel->setDescription($FileViewdata['config']['description']);
$excel->sheet("page 1", function($sheet) {
$sheet->loadView("reports.sample");
});
})->store('xls', storage_path('exports')); // ->export('xls');
$report_excelFilepath = storage_path('exports') . "/Report.xls";
return response()->download($report_excelFilepath, "Report.xls", [
'Content-Type' => 'application/vnd.ms-excel',
'Content-Disposition' => "attachment; filename='Report.xls'"
]);
}
The View being made into the Excel file is a simple table - the same table as in the screenshots. Here's the template/HTML of the View:
<div class="container full-width-container">
<link href="http://192.168.33.10/css/reports.css" rel="stylesheet">
<div id="results" class="row">
<div class="col-xs-12">
<h2 class="report-title">Active Products </h2>
<br>
<div class="row">
<div class="col-xs-12">
<div class="table-responsive report-component" name="table" template="table">
<table id="report-table" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td class="report-table-th">ENGINE</td>
<td class="report-table-th">PRODUCT COUNT</td>
<td class="report-table-th">PRODUCT PRICE</td>
</tr>
</thead>
<tbody>
<tr class="results-cell">
<td class="report-table-cell">
<p>Meows</p>
</td>
<td class="report-table-cell">
<p>1,234</p>
</td>
<td class="report-table-cell">
<p>781230.00</p>
</td>
</tr>
</tbody>
<tbody>
<tr class="results-cell">
<td class="report-table-cell">
<p>Paws</p>
</td>
<td class="report-table-cell">
<p>10,777</p>
</td>
<td class="report-table-cell">
<p>3919823.00</p>
</td>
</tr>
</tbody>
<tbody>
<tr class="results-cell">
<td class="report-table-cell">
<p>Meow Mobile</p>
</td>
<td class="report-table-cell">
<p>177</p>
</td>
<td class="report-table-cell">
<p>334323.00</p>
</td>
</tr>
</tbody>
<tbody>
<tr class="results-cell">
<td class="report-table-cell">
<p>Tails Cloud</p>
</td>
<td class="report-table-cell">
<p>335</p>
</td>
<td class="report-table-cell">
<p>378918923.00</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
Does anyone see what I might be missing to download an XLS file?
Thanks for your time!
EDIT
After doing some more research, it seems that my original workflow for downloading -- using an AJAX call to trigger -- is part of the problem. For example Download a file from Servlet using Ajax talks about AJAX storing results in JavaScript memory, which would explain why I get content in my console and not a file.

Try to add code below before Excel::create():
ob_end_clean();
ob_start();
Example code:
ob_end_clean();
ob_start();
Excel::create($filename, function($excel) use($records, $sheetname, $data) {}
This works for me.

Related

Use HTML Table without headers

I've a table, where the normal guidance for HTML tables arn't followed.
My best move will be, to just create a proper JSON-object, and using that.
But i'll like to ask, if there is any options for parsing an HTML table, "without headers", and define them in Tabulator, instead.
I know the case id odd, but i'll just like to hear :-)
Example where no thead and th is in the HTML-source:
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr height="16">
<td colspan="16">
Something
</td>
<td colspan="16">
14
</td>
<td colspan="16">
2020-01-28
</td>
</tr>
</tbody>
</table>
Im afraid not.
When Tabulator is built on a table element, it parses the HTML to create a JavaScript object for each row of the table, using the column headers as property names.
Without headers it would have no reasonable way to map the column values onto an object.
I solve problem like this
<table border="0" cellpadding="0" cellspacing="0">
<tr height="16" hidden>
<td colspan="16">
</td>
<td colspan="16">
</td>
<td colspan="16">
</td>
</tr>
<tr height="16">
<td colspan="16">
Something
</td>
<td colspan="16">
14
</td>
<td colspan="16">
2020-01-28
</td>
</tr>
</table>

Displaying two different tables on different click

So I'm creating website for a sports team. I have a "Results" page, where their scores for every round will be posted in form of table. Above the table I need to have two options, one that says "Season 2014/2015" and the other "Season 2015/2016", and I want two different tables displayed for each one of them? How do I do that?
You can do this by simply using jquery.Here is an example.
HTML
<div id="foo">
<span id="table1-btn">Table 1</span>
<span id="table2-btn">Table 2</span>
</div>
<table id="table1">
<tr>
<td>Table 1</td><td>Table 1</td>
</tr>
<tr>
<td>Table 1</td><td>Table 1</td>
</tr>
</table>
<table id="table2">
<tr>
<td>Table 2</td><td>Table 2</td>
</tr>
<tr>
<td>Table 2</td><td>Table 2</td>
</tr>
</table>
css
#foo{
width:200px;
height:auto;
padding:5px;
}
#table1-btn,
#table2-btn{
width:100px;
height:auto;
padding:5px;
background-color:#e3e3e3;
cursor:pointer;
}
jQuery
$(document).ready(function () {
$('#table2').hide();
$('#table1-btn').click(function () {
$('#table2').hide();
$('#table1').show();
});
$('#table2-btn').click(function () {
$('#table1').hide();
$('#table2').show();
});
});
Find jsfiddle code Here
If you are using bootstrap, you could look into tabs: http://getbootstrap.com/javascript/#tabs
Have your tables in a tab 2014/2015 and the other tables in a tab 2015/2016

How to pass product TVs to SimpleCart's scGetCart snippet?

I need some TVs (weight, dimensions, etc) I've associated with my products to appear in the Cart page of my SimpleCart site.
Problem is I have no idea how to do this. I don't understand how the SimpleCart cart is built and there isn't documentation for this.
Would anyone know how I can show TVs associated with each product in the cart output chunk?
The cart snippet has the following code which gets data from the cart and puts it into Chunks:
$sc = $modx->getService('simplecart','SimpleCart',$modx->getOption('simplecart.core_path',null,$modx->getOption('core_path').'components/simplecart/').'model/simplecart/',$scriptProperties);
if (!($sc instanceof SimpleCart)) return '';
 
$controller = $sc->loadController('Cart');
$output = $controller->run($scriptProperties);
The output Chunk looks like:
<div id="simplecart">
<form action="[[~[[*id]]]]" method="post" id="form_cartoverview">
<input type="hidden" name="updatecart" value="true" />
<table>
<tr>
<th class="desc">[[%simplecart.cart.description]]</th>
<th class="price">[[%simplecart.cart.price]]</th>
<th class="quantity">[[%simplecart.cart.quantity]]</th>
[[+cart.total.vat_total:notempty=`<th class="quantity">[[%simplecart.cart.vat]]</th>`:isempty=``]]
<th class="subtotal">[[%simplecart.cart.subtotal]]</th>
<th> </th>
</tr>
[[+cart.wrapper]]
[[+cart.total.discount:notempty=`<tr class="total first discount">
<td colspan="[[+cart.total.vat_total:notempty=`3`:isempty=`2`]]"> </td>
<td class="label">[[%simplecart.cart.discount]]</td>
<td class="value">- [[+cart.total.discount_formatted]]</td>
<td class="extra">[[+cart.total.discount_percent:notempty=`([[+cart.total.discount_percent]]%)`:isempty=` `]]</td>
</tr>`:isempty=``]]
[[+cart.total.vat_total:notempty=`
<tr class="total [[+cart.total.discount:notempty=`second`:isempty=`first`]]">
<td colspan="3"> </td>
<td class="label">[[%simplecart.cart.total_ex_vat]]</td>
<td class="value">[[+cart.total.price_ex_vat_formatted]]</td>
<td class="extra"> </td>
</tr>
[[+cart.vat_rates]]
<tr class="total [[+cart.total.discount:notempty=`third`:isempty=`second`]]">
<td colspan="3"> </td>
<td class="label">[[%simplecart.cart.total_vat]]</td>
<td class="value">[[+cart.total.vat_total_formatted]]</td>
<td class="extra"> </td>
</tr>
<tr class="total [[+cart.total.discount:notempty=`fourth`:isempty=`third`]]">
<td colspan="3"> </td>
<td class="label">[[%simplecart.cart.total_in_vat]]</td>
<td class="value">[[+cart.total.price_formatted]]</td>
<td class="extra"> </td>
</tr>
`:isempty=`
<tr class="total [[+cart.total.discount:notempty=`second`:isempty=`first`]]">
<td colspan="2"> </td>
<td class="label">[[%simplecart.cart.total]]</td>
<td class="value">[[+cart.total.price_formatted]]</td>
<td class="extra"> </td>
</tr>
`]]
</table>
<div class="submit">
<input type="submit" value="[[%simplecart.cart.update]]" />
</div>
</form>
This does appear to be documented:
Product Options (TVs)
and to output them:
Modifying the Product Template
It appears that you would just output them normally [[*myProductOptions]]
Though, it appears that your template is using a placeholder, I would try
[[+cart.myProductOptions] as well. If all else fails you might try debugging the simplecart class and dump the array of product data before it populates the chunk, there might be a clue in there.
Found (through trial and error) you must use:
[[+product.tv.name_of_tv]]

Using watir to click table element without id

I have a dynnamic table that I would like to use watir to select a edit button. It doesn't have a traditional watir id that I can select (example: browser.img(:title, "iconname")) and there may be multiple edit icons to choose from. In the past I would get the correct element by querying the database. However this does not have a database entry to help me select the correct edit link this time.
In the code below what I am trying to select is from the section where it shows "autogenerated3" I am trying to select either the "onclick" element or the "img src"
Both are selectable items that will click the edit icon.
<div id="certificate_table" style="margin-bottom: 1em">
<table cellspacing="0">
<tbody>
<tr>
<th>Alias</th>
<th>Common Name</th>
<th class="centered">Status</th>
<th class="centered">In Use</th>
<th class="centered">Issued</th>
<th class="centered">Expires</th>
<th class="centered">Days</th>
<th>Actions</th>
</tr>
<tr class="normal">
<td>autogenerated2</td>
<td>default.domain.com</td>
<td class="centered"> Revoked </td>
<td class="centered">
<td class="centered"> 10/18/2013 19:46:34 GMT </td>
<td class="centered"> 10/17/2016 19:46:34 GMT </td>
<td class="centered">N/A</td>
<td>
<a onclick="new Ajax.Request('/media/certificates/edit_certificate/3', {asynchronous:true, evalScripts:true}); return false;" href="#">
<img title="Edit" src="/media/images/icons/edit.gif?1276876449" alt="Edit">
</a>
</td>
</tr>
<tr class="alt">
<td>autogenerated3</td>
<td>autogenerated3.domain.com</td>
<td class="centered"> CSR Issued </td>
<td class="centered">
<td class="centered"> 10/18/2013 20:54:55 GMT </td>
<td class="centered"> 10/17/2016 20:54:55 GMT </td>
<td class="centered">1092 </td>
<td>
<a onclick="new Ajax.Request('/media/certificates/edit_certificate/4', {asynchronous:true, evalScripts:true}); return false;" href="#">
<img title="Edit" src="/media/images/icons/edit.gif?1276876449" alt="Edit">
</a>
<a onclick="new Ajax.Request('/media/certificates/generate_csr/4', {asynchronous:true, evalScripts:true}); return false;" href="#">
<a onclick="new Ajax.Request('/media/certificates/import_certificate_reply/4', {asynchronous:true, evalScripts:true}); return false;" href="#">
<a onclick="if (confirm('Are you sure you want to revoke this certificate?')) { new Ajax.Request('/media/certificates/revoke_certificate/4', {asynchronous:true, evalScripts:true}); }; return false;" href="#">
</td>
</tr>
<tr class="normal">
<td>Original Certificate</td>
<td>localhost.localdomain</td>
<td class="centered"> Self Signed </td>
<td class="centered">
<td class="centered"> 10/03/2013 22:37:02 GMT </td>
<td class="centered"> 10/03/2014 22:37:02 GMT </td>
<td class="centered">347 </td>
<td>
<a onclick="new Ajax.Request('/media/certificates/edit_certificate/1', {asynchronous:true, evalScripts:true}); return false;" href="#">
<img title="Edit" src="/media/images/icons/edit.gif?1276876449" alt="Edit">
</a>
</td>
</tr>
<tr class="alt">
<td>vhost4</td>
<td>vhost4.domain.com</td>
<td class="centered"> Revoked </td>
<td class="centered">
<td class="centered"> 10/18/2013 15:58:01 GMT </td>
<td class="centered"> 10/17/2016 15:58:01 GMT </td>
<td class="centered">N/A</td>
<td>
<a onclick="new Ajax.Request('/media/certificates/edit_certificate/2', {asynchronous:true, evalScripts:true}); return false;" href="#">
<img title="Edit" src="/media/images/icons/edit.gif?1276876449" alt="Edit">
</a>
</td>
</tr>
</tbody>
</table>
I don't have trouble selecting a icon. Just trouble selecting the correct icon. Both the onclick and image values are selectable items. The icon may not be the last item in the list. I saw a post to try .last.click which does select the last icon in the list. Unfortunately the table posts the data in alphabetical order based on the Alias name. So it may not be the last item in the list and cannot use this method. Suggestions?
b.div(:id, "certificate_table").imgs(:src => "/media/images/icons/edit.gif?1276876449").last.when_present.click
It sounds like you need to find an element based on its siblings, so you might find this blog post useful. The post gives two options you might consider.
If the text you are looking for is unique - ie the row that has the text is definitely going to be the right row, you can find the td, go to the parent row and then get the link.
b.td(:text => 'autogenerated3').parent.link.click
If you need to ensure that the text is in the first column, then you can do:
b.trs.find{ |tr|
tr.td.exists? and tr.td(:index => 0).text == 'autogenerated3'
}.link.click
The tr.td.exists? is added since some of your rows do not have any tds, which would cause an exception when checking the second criteria.

Trying to use laravel pagination breaks my controller/view

I'm trying to get Pagination working in Laravel. I've not used pagination before so am trying to use some examples from the docs and google results. I'm wondering if I need to turn 'pagination' on somewhere in config.
Here's my simple working controller:
public function get_index() {
$locations = Location::all();
return View::make('location.index')->with('locations', $locations)->render();
}
When I try and add pagination like this it breaks:
public function get_index() {
$locations = Location::paginate(5);
return View::make('location.index')->with('locations', $locations)->render();
}
..and I get the error:
Unhandled Exception
Message:
Error rendering view: [location.index]
Trying to get property of non-object
Here's my location.index view:
#layout('layouts.blank')
#section('content')
<div class="row">
<div class="twelve columns">
<div role="main" id="content">
<h3>Locations - All</h3>
#if($locations)
<table class="twelve">
<thead>
<tr>
<th style="text-align: left;">Address</th>
<th>Area</th>
<th>Region</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
#foreach($locations as $location)
<tbody>
<tr>
<td style="text-align: left;">{{ $location->company,', ',$location->add_name,', ',$location->add_city }}</td>
<td> – </td>
<td> – </td>
<td>{{ HTML::link('location/update/'.$location->id, '',array('class'=>"foundicon-edit updateicon")) }}</td>
<td>{{ HTML::link('location/delete/'.$location->id, '',array('class'=>"foundicon-remove warnicon")) }}</td>
</tr>
</tbody>
#endforeach
</table>
#else
Looks like we haven't added any locations, yet!
#endif
<p>{{ HTML::link('location/create', 'Create a location', array('class'=>"small radius button")) }}</p>
</div>
</div>
</div>
#endsection
I hadn't read deeply enough into the subject. It's now working and here's what I had to change in the view:
//from:
#foreach($locations as $location)
//to:
#foreach($locations->results as $location)
That returned just 5 results. Then to add links to the footer of my table I added this HTML/Blade:
<tfoot>
<tr>
<td colspan="5"> {{ $locations->links() }} </td>
</tr>
</tfoot>
Hope someone else benefits as I didn't find this terribly clear in the docs.

Resources