Fixed exporting to csv

pull/496/head
Cagatay Civici 2020-09-24 11:55:27 +03:00
parent 488c09b0ee
commit 57374fc2b7
2 changed files with 17 additions and 14 deletions

View File

@ -435,7 +435,6 @@ export default {
let columnOrder = []; let columnOrder = [];
this.columns.forEach(col => columnOrder.push(col.props?.columnKey||col.props?.field)); this.columns.forEach(col => columnOrder.push(col.props?.columnKey||col.props?.field));
this.d_columnOrder = columnOrder; this.d_columnOrder = columnOrder;
console.log()
} }
}, },
beforeUnmount() { beforeUnmount() {
@ -935,14 +934,17 @@ export default {
} }
//headers //headers
let headerInitiated = false;
for (let i = 0; i < this.columns.length; i++) { for (let i = 0; i < this.columns.length; i++) {
let column = this.columns[i]; let column = this.columns[i];
if (column.props?.exportable !== false && column.props?.field) { if (column.props?.exportable !== false && column.props?.field) {
csv += '"' + (column.props?.header || column.props?.field) + '"'; if (headerInitiated)
if (i < (this.columns.length - 1)) {
csv += this.csvSeparator; csv += this.csvSeparator;
} else
headerInitiated = true;
csv += '"' + (column.props?.header || column.props?.field) + '"';
} }
} }
@ -950,9 +952,15 @@ export default {
if (data) { if (data) {
data.forEach(record => { data.forEach(record => {
csv += '\n'; csv += '\n';
let rowInitiated = false;
for (let i = 0; i < this.columns.length; i++) { for (let i = 0; i < this.columns.length; i++) {
let column = this.columns[i]; let column = this.columns[i];
if (column.props?.exportable !== false && column.props?.field) { if (column.props?.exportable !== false && column.props?.field) {
if (rowInitiated)
csv += this.csvSeparator;
else
rowInitiated = true;
let cellData = ObjectUtils.resolveFieldData(record, column.props?.field); let cellData = ObjectUtils.resolveFieldData(record, column.props?.field);
if (cellData != null) { if (cellData != null) {
@ -968,12 +976,7 @@ export default {
else else
cellData = ''; cellData = '';
csv += '"' + cellData + '"'; csv += '"' + cellData + '"';
if (i < (this.columns.length - 1)) {
csv += this.csvSeparator;
}
} }
} }
}); });

View File

@ -35,7 +35,7 @@
</div> </div>
</template> </template>
<Column selectionMode="multiple" headerStyle="width: 3rem"></Column> <Column selectionMode="multiple" headerStyle="width: 3rem" :exportable="false"></Column>
<Column field="code" header="Code" :sortable="true"></Column> <Column field="code" header="Code" :sortable="true"></Column>
<Column field="name" header="Name" :sortable="true"></Column> <Column field="name" header="Name" :sortable="true"></Column>
<Column header="Image"> <Column header="Image">
@ -59,7 +59,7 @@
<span :class="'product-badge status-' + slotProps.data.inventoryStatus.toLowerCase()">{{slotProps.data.inventoryStatus}}</span> <span :class="'product-badge status-' + slotProps.data.inventoryStatus.toLowerCase()">{{slotProps.data.inventoryStatus}}</span>
</template> </template>
</Column> </Column>
<Column> <Column :exportable="false">
<template #body="slotProps"> <template #body="slotProps">
<Button icon="pi pi-pencil" class="p-button-rounded p-button-success p-mr-2" @click="editProduct(slotProps.data)" /> <Button icon="pi pi-pencil" class="p-button-rounded p-button-success p-mr-2" @click="editProduct(slotProps.data)" />
<Button icon="pi pi-trash" class="p-button-rounded p-button-warning" @click="confirmDeleteProduct(slotProps.data)" /> <Button icon="pi pi-trash" class="p-button-rounded p-button-warning" @click="confirmDeleteProduct(slotProps.data)" />
@ -172,7 +172,7 @@
&lt;/div&gt; &lt;/div&gt;
&lt;/template&gt; &lt;/template&gt;
&lt;Column selectionMode="multiple" headerStyle="width: 3rem"&gt;&lt;/Column&gt; &lt;Column selectionMode="multiple" headerStyle="width: 3rem" :exportable="false"&gt;&lt;/Column&gt;
&lt;Column field="code" header="Code" :sortable="true"&gt;&lt;/Column&gt; &lt;Column field="code" header="Code" :sortable="true"&gt;&lt;/Column&gt;
&lt;Column field="name" header="Name" :sortable="true"&gt;&lt;/Column&gt; &lt;Column field="name" header="Name" :sortable="true"&gt;&lt;/Column&gt;
&lt;Column header="Image"&gt; &lt;Column header="Image"&gt;
@ -196,7 +196,7 @@
&lt;span :class="'product-badge status-' + slotProps.data.inventoryStatus.toLowerCase()"&gt;{{slotProps.data.inventoryStatus}}&lt;/span&gt; &lt;span :class="'product-badge status-' + slotProps.data.inventoryStatus.toLowerCase()"&gt;{{slotProps.data.inventoryStatus}}&lt;/span&gt;
&lt;/template&gt; &lt;/template&gt;
&lt;/Column&gt; &lt;/Column&gt;
&lt;Column&gt; &lt;Column :exportable="false"&gt;
&lt;template #body="slotProps"&gt; &lt;template #body="slotProps"&gt;
&lt;Button icon="pi pi-pencil" class="p-button-rounded p-button-success p-mr-2" @click="editProduct(slotProps.data)" /&gt; &lt;Button icon="pi pi-pencil" class="p-button-rounded p-button-success p-mr-2" @click="editProduct(slotProps.data)" /&gt;
&lt;Button icon="pi pi-trash" class="p-button-rounded p-button-warning" @click="confirmDeleteProduct(slotProps.data)" /&gt; &lt;Button icon="pi pi-trash" class="p-button-rounded p-button-warning" @click="confirmDeleteProduct(slotProps.data)" /&gt;