Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
d3v3l0p3r
Posts: 165
Registered: ‎07-19-2012
My Device: Q5, Q10, Z10, Z30, PlayBook
My Carrier: Carrier

Need some help with sortable and filtered group data model from SQL table

Can someone point me to an example of an implementation of a data model which allows sorting, filtering and can use headers in ListView? (with data coming from a SQL table).

 

Here is my SQL table:

 

table: tblTransactions
columns: txDate, txAmount

 My query:

SELECT txDate, txAmount FROM tblTransactions

 The ListView should produce the following:

2012-09-12 <- header
   $33.00
   $2.00
   $99.99
2012-09-11 <- header
   $1.99
   $345.32

 

What I need is also ability to call the page from another page and pass date to it and be able to see only entries for that date (without re-querying the database) and be able to sort by date and amount

 

Any help or pointers are appreciated.

 

I looked at the weather guesser but the SQL datamodel does not support sorting / groups

---
If you find my post helpful please "like" it and "accept as a solution"
Please use plain text.
Developer
d3v3l0p3r
Posts: 165
Registered: ‎07-19-2012
My Device: Q5, Q10, Z10, Z30, PlayBook
My Carrier: Carrier

Re: Need some help with sortable and filtered group data model from SQL table

Let me ask this question from another angle (if that helps).

 

I managed to produce the following model using sample from the "Repeater" project:

#include "transactionsmodel.hpp"
#include  <bb/data/SqlDataAccess>

using namespace bb::data;

TransactionsModel* TransactionsModel::fromSQLAsset(const QString &fileName)
{
    SqlDataAccess dbda("app/native/assets/" + fileName);

    // Load the result and extract the list of rows.
    const QVariantList sqlRoot = dbda.execute("SELECT txDate, txAmount FROM tblTransactions").value<QVariantList>();

    // Use the result as data source for a model.
    TransactionsModel *model = new TransactionsModel();
    model->append(sqlRoot);
    return model;
}

which I make available to my QML in app.cpp as follows:

 

qml->setContextProperty("_transactionsModel", TransactionsModel::fromSQLAsset("myDB.db"));

 and then in my QML I do the following:

 

ListView {
   dataModel: _transactionsModel
   listItemComponents: [
      ListItemComponent {
          Container {
               Label {
                   text: ListItemData.txDate
               }
               Label {
                   text: ListItemData.txAmount
               }	                           
           }
       }
   ]
}

 

It seems to work but how do I put the date into header, make it sortable and filter it?

---
If you find my post helpful please "like" it and "accept as a solution"
Please use plain text.
BlackBerry Development Advisor (Retired)
amarcon
Posts: 155
Registered: ‎07-16-2012
My Device: Dev Alpha
My Carrier: Bell

Re: Need some help with sortable and filtered group data model from SQL table

Hi Produktyf,

 

You may want to look at the Quotes sample, available under https://github.com/blackberry/Cascades-Samples/tree/master/quotes

 

It has a GroupDataModel, might be what you're looking for.

 

Hope this helps.

Please use plain text.
Developer
Zukka
Posts: 201
Registered: ‎05-08-2012
My Device: PlayBook 64 Gb and Z10 LE
My Carrier: Vodafone

Re: Need some help with sortable and filtered group data model from SQL table

But quotes app work only with navigationPane. :smileysad:
_______________________________________________

If U like my post click on LIKE :smileywink:
Please use plain text.
Developer
BBornot2B
Posts: 19
Registered: ‎11-11-2012
My Device: Z10, DevAlphaC, Torch 9810, PlayBook
My Carrier: Movistar

Re: Need some help with sortable and filtered group data model from SQL table

Why not trying something like this...

 

The QML:

ListView {
	id: bookmarks
	objectName: "bookmarks"

	listItemComponents: [
        	ListItemComponent {
			type: "header"
                        
                        Header {
                            title: ListItemData
                	}
                },
                    
		ListItemComponent {
			type: "item"
	                
			...    

 

 

The app C++ code:

	dbFile.open(QIODevice::ReadOnly);
	SqlDataAccess data(dbFile.fileName());
	QVariant list = data.execute("SELECT * FROM Bookmark");
	dbFile.close();

	GroupDataModel *model = new GroupDataModel(QStringList() << "date" << "time");
	model->insertList(list.value<QVariantList>());
	model->setGrouping(ItemGrouping::ByFullValue);
	model->setSortedAscending(false);

	homeQml = QmlDocument::create("asset:///HomePage.qml").parent(this);
	homePage = homeQml->createRootObject<NavigationPane>();
	bookmarks = homePage->findChild<ListView*>("bookmarks");
	bookmarks->setDataModel(model);

 

Please use plain text.