Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a dynamic table

My problem is that I need to read the names and number of columns in the table from one array (tFields), and the element data from the second array (tRecords).

I have already implemented the reading and creation of columns, I ask you to help or tell me where the problem is in creating the list.

Here is the code:

function buildColumns() {
var rc = vConnection.recCount
var fc = vConnection.fldCount

for(var i = 0; i < fc; ++i)
{
    var str = 'import QtQuick 2.1; import QtQuick.Controls 1.0; import QtQuick.Layouts 1.0; TableViewColumn{ id: idColumn; role: '
    str += '"' + vConnection.tFields[i] + '"' + '; title: ' + '"' + vConnection.tFields[i] + '"' +  '; width: ' + (tblPage.width / vConnection.fldCount) +'; Component.onCompleted: console.log("YEY");}'
    tbl.addColumn(Qt.createQmlObject(str, tbl))
}

for(var j = 0; j < rc; ++j)
{
    var str2 = 'import QtQuick 2.12; import QtQuick.Window 2.12; import QtQuick.Controls 2.4; import QtQuick.Controls.Imagine 2.3; import QtQuick.Templates 2.5; import QtQuick.Controls 1.4; ListElement { id: idColumn; '
    for(var f = 0; f < fc; ++f)
    {
        str2 += vConnection.tFields[f] + ': "' + vConnection.tRecords[f + rc] + '"; '
    }

    str2 += '}'

    Qt.createQmlObject(str2, lmTable)
}
}

Component.onCompleted: {
    buildColumns();
}

TableView {
id: tbl
anchors.fill: parent

model: ListModel {
    id: lmTable
}
}

}
like image 847
Wargos Avatar asked Mar 02 '26 06:03

Wargos


1 Answers

Do not overuse the string concatenation as it can be confusing to handle, instead you can use the createObject method of a "Component" that can assign the properties in a more readable way.

To add items to the model it is not necessary to use X, instead use the append method where you can pass a dictionary making the code more readable.

Page {
    id: tblPage
    title: qsTr("Table")

    Component{
        id: columnComponent
        TableViewColumn{
            Component.onCompleted: console.log("YEY")
        }
    }
    ListModel {
        id: lmTable
    }
    function buildColumns() {
        var rc = vConnection.recCount
        var fc = vConnection.fldCount
        for(var i = 0; i < fc; ++i){
            var col = columnComponent.createObject(tbl, {"role": vConnection.tFields[i],
                                                       "title": vConnection.tFields[i],
                                                       "width": (tblPage.width / vConnection.fldCount)})
            tbl.addColumn(col)
        }
        for(var j = 0; j < rc; ++j){
            var d = {};
            for(var f = 0; f < fc; ++f){
                d[vConnection.tFields[f]] = vConnection.tRecords[f + rc]
            }
            lmTable.append(d)
        }
    }
    TableView {
        id: tbl
        anchors.fill: parent
        model: lmTable
    }
    Component.onCompleted: buildColumns()
}
like image 79
eyllanesc Avatar answered Mar 03 '26 19:03

eyllanesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!