Function QStringList -> HTML table

This commit is contained in:
Klaus Basan
2018-10-06 04:32:32 +02:00
parent 484d89dd85
commit 4ba8d7a758
2 changed files with 49 additions and 4 deletions

View File

@@ -0,0 +1,41 @@
/* Copyright (C) 2018
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include <QStringBuilder>
#include "htmlutils.h"
namespace BlackMisc
{
QString toHtmTable(const QStringList &values, int columns)
{
if (values.isEmpty() || columns < 1) { return QStringLiteral(""); }
QString html;
QString row;
const int size = values.size();
for (int i = 0; i < size; i++)
{
html += QStringLiteral("<tr>");
for (int c = 0; c < columns; c++)
{
if (i < size)
{
html += QStringLiteral("<td>") % values.at(i) % QStringLiteral("</td>");
}
else
{
html += QStringLiteral("<td></td>");
}
i++;
}
html += QStringLiteral("</tr>");
}
return QStringLiteral("<table>") % html % QStringLiteral("</table>");
}
} // ns

View File

@@ -12,10 +12,10 @@
#ifndef BLACKMISC_HTMLUTILS_H
#define BLACKMISC_HTMLUTILS_H
#include <QStringBuilder>
#include "blackmisc/blackmiscexport.h"
#include "blackmisc/propertyindexlist.h"
//! Free functions in BlackMisc
namespace BlackMisc
{
@@ -29,12 +29,16 @@ namespace BlackMisc
QString rowHtml;
for (const CPropertyIndex &index : indexes)
{
rowHtml += "<td>" + obj.propertyByIndex(index).toQString(true) + "</td>";
rowHtml += QStringLiteral("<td>") % obj.propertyByIndex(index).toQString(true) % QStringLiteral("</td>");
}
html += "<tr>" + rowHtml + "</tr>";
html += QStringLiteral("<tr>") % rowHtml % QStringLiteral("</tr>");
}
return "<table>" + html + "</table>";
return QStringLiteral("<table>") % html % QStringLiteral("</table>");
}
//! Values as HTML table
BLACKMISC_EXPORT QString toHtmTable(const QStringList &values, int columns);
} // ns
#endif // guard