[英]How to Export grid as CSV file in Agiletoolkit atk4
我正在使用agiletoolkit atk4,我想将网格导出到CSV文件,我正在使用下面的代码,我遇到的问题是下面的细节:
代码:
$grid->menu->addItem(['Export as CSV..', 'icon' => 'add square'], new \atk4\csv\Export($model))->saveAsCSV('file.csv', ['id', 'name']);
问题:错误:找不到类“atk4\\csv\\Export”。 意味着无法在agiletoolkit atk4 文件夹中找到导出库。
我正在使用这篇文章: https : //github.com/atk4/csv
寻求帮助?
注意:我以不同的方式完成了下面的细节:
if (isset($_GET['export'])) {
header("Location: exportCSV.php?exportid=".@$_GET['export']);
exit();
}
$button =$grid->menu->addItem(['Export as CSV..', 'icon' => 'add square']);
$button->on('click', null, function ($b) use($app) {
return [$app->jsRedirect(['export' =>'export'])];
//return 'success';
});
在 exportCSV.php 文件中
$query = "SELECT id,name,address FROM tableName;";
$result = mysqli_query($conn, $query) or die("database error:". mysqli_error($conn));
$records = array();
while( $rows = mysqli_fetch_assoc($result) ) {
$records[] = $rows;
}
if(isset($records)) {
$csv_file = "csv_export_".date('Ymd') . ".csv";
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=\"$csv_file\"");
$fh = fopen( 'php://output', 'w' );
$is_coloumn = true;
if(!empty($records)) {
foreach($records as $record) {
if($is_coloumn) {
fputcsv($fh, array_keys($record));
$is_coloumn = false;
}
fputcsv($fh, array_values($record));
}
fclose($fh);
}
exit;
}
正如您在/atk4/csv
github 存储库中看到的那样 - 它从未实现过。 它只包含自述文件,仅此而已:)
您上面的解决方案不使用 atk4 模型并使用普通的 mysqli 连接。 如果它有效也没关系,但是如果您的项目依赖于 atk4 模型,那么您可能应该导出 atk4 模型。
要将您的数据导出到 CSV 文件,您可以执行以下操作:
$records = $grid->model->export();
if(isset($records)) {
... here goes your code to save data in CSV file
}
或者如果您想要功能齐全的解决方案,那么您可以将此特征添加到您的数据模型中:
<?php
/**
* Trait for exporting model data as CSV file.
*
* Usage:
* Add this trait in your model. And then simply use it like this:
* $model->exportCSV('filename_without_extension', [fields_to_export]);
*/
trait Trait_ModelExportCSV
{
// CSV configuration
public $csv_delimiter = ',';
public $csv_enclosure = '"';
public $csv_escape_char = "\\";
public function exportCSV($filename, $fields = [])
{
$fields = $fields ?: array_keys($this->getFields);
// HTTP header
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$filename.csv");
header("Pragma: no-cache");
header("Expires: 0");
$output = fopen("php://output", "w");
fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF)); // add BOM for UTF-8 support
// save CSV header
$header = [];
foreach ($fields as $f) {
$header[] = $this->getField($f)->getCaption();
}
fputcsv($output, $header, $this->csv_delimiter, $this->csv_enclosure, $this->csv_escape_char);
// save CSV rows
foreach ($this as $row) {
$r = [];
foreach ($fields as $f) {
$v = $row[$f];
if ($this->getField($f)->type == 'boolean') {
$v = $v === true ? "Y" : "N";
}
if ($v instanceof \DateTime) {
$v = $v->format('Y-m-d');
}
$r[] = $v;
}
fputcsv($output, $r, $this->csv_delimiter, $this->csv_enclosure, $this->csv_escape_char);
}
// close CSV file
fclose($output);
exit;
}
}
这是针对较旧的 atk4 版本,但您可以了解它是如何完成的并对其进行调整以匹配您正在使用的 atk4 版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.