Dataset as a datasource???

Hi

I’m trying to use a dataset made up of XML data as a datasource for a form. I want to be able to display the dataset in a grid like you can with a foundset. I have the dataset all created, rows and columns are populated. Any advice on how I can do that :?:

I followed this example:

viewtopic.php?f=22&t=16795&p=90770&hilit=flat+xml#p90772

Hi Westly,

use the function ‘createDataSource()’ on the dataset.
Then you have to create a form and bind the datasource to it via the solutionModel.

Hope this gives you a bit of a pointer where to go.

Thank you marc. I have the form created, dataset and datasource are form variables. I am wondering now how I make it be presented in a grid. I’ve used servoy-plugins.de datagrid bean, but I need the data presented in a webclient. I’m missing something and just not sure what concept I’m missing. Some Code:

var dataset = databaseManager.createEmptyDataSet(4, new Array('a','b','c','d','e','f','g'))

var nDS = dataset.createDataSource('sweet',[JSColumn.TEXT,JSColumn.TEXT,JSColumn.DATETIME,JSColumn.NUMBER,JSColumn.NUMBER,JSColumn.NUMBER,JSColumn.NUMBER]);


	application.output(nDS);
	var vForm = solutionModel.newForm('vForm',nDS,null,false,800,600);
	vForm.newButton('Home',800,20,65,25,globals.btnHome);
	application.output(vForm.getField('a'));
	forms['vForm'].controller.show();
	application.sleep(200);
	forms['vForm'].controller.recreateUI();

Hi westly,

I’m not sure if this will be helpful to you, but I’m working on something similar right now, and am told that one way to do the grid is to put an html_area field on your form, and then fill it with HTML that will build your grid for you, basically a table. This will work in both the smart client and the web client. Also, it’s been suggested that reporting tools such as JasperReports or VelocityReports can do this kind of thing as well.

Hope that helps a bit.

Ron

RonG:
Also, it’s been suggested that reporting tools such as JasperReports or VelocityReports can do this kind of thing as well.

Don’t know about Jasper, but I can confirm that using Velocity to construct any kind of HTML based on Servoy’s objects is a trivial task.
I still wonder why people persist in doing it differently!

RonG:
Hi westly,

I’m not sure if this will be helpful to you, but I’m working on something similar right now, and am told that one way to do the grid is to put an html_area field on your form, and then fill it with HTML that will build your grid for you, basically a table. This will work in both the smart client and the web client. Also, it’s been suggested that reporting tools such as JasperReports or VelocityReports can do this kind of thing as well.

Hope that helps a bit.

Ron

Thanks Ron,
I just looped through a 2D Array and wrapped td tags around each element, and put them into an HTML table. It works. Haha :P

I did a quick benchmark with 3 different approaches to get the data wrapped into HTML:

  1. simple concatenation of a string (using a loop)
  2. making use of the array.join() function to wrap the data into another array in a loop
  3. using the VelocityReport plugin with a template
var _aRow = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'],
    _aData = new Array(),
    i,
    j,
    _dStart,
    _sTmp,
    _aTmp;

// create a 10000 row 2D array (could also be a Dataset)
for (i = 1; i <= 10000; i++) {
    _aData.push(_aRow);
}

// Loop + string concat
_dStart = new Date();
_sTmp = '<table>'
for (i = 0; i < _aData.length; i++) {
    _sTmp += '<tr>';
    for (j = 0; j < _aData[i].length; j++) {
        _sTmp += '<td>' + _aData[i][j] + '</td>';
    }
    _sTmp += '</tr>';
}
_sTmp += '</table>';
application.output(_sTmp);
application.output("loop + string concat: " + (new Date() - _dStart));

// Loop + array.join()
_dStart = new Date();
_aTmp = new Array();
for (i = 0; i < _aData.length; i++) {
    _aTmp.push('<td>' + _aData[i].join('</td><td>') + '</td>');
}
application.output('<table><tr>' + _aTmp.join('</tr><tr>') + '</tr></table>');
application.output("loop + array.join(): " + (new Date() - _dStart));

// Velocity + template
_dStart = new Date();
application.output(plugins.VelocityReport.evaluateWithContext("<table>#foreach( $row in $data )<tr>#foreach( $col in $row )<td>$col</td>#end</tr>#end</table>", {data: _aData}));
application.output("Velocity: " + (new Date() - _dStart));

100 records:

  1. 6 milliseconds
  2. 3 milliseconds ← fastest
  3. 10 milliseconds

1000 records:

  1. 69 milliseconds
  2. 31 milliseconds ← fastest
  3. 64 milliseconds

5000 records:

  1. 770 milliseconds
  2. 186 milliseconds ← fastest
  3. 464 milliseconds

10000 records:

  1. 3008 milliseconds
  2. 385 milliseconds ← fastest
  3. 927 milliseconds

So it seems the array.join approach is the fastest but I have no idea which one is more memory efficient.
Velocity seems the second best approach when you have a bit larger datasets (and as you can see it’s essentially a one-liner).

All tests were done on MacOS X. Your milage may vary on other platforms.
And I bet you get quite some different results when you start using real data (instead of an array with the alphabet).

:) Nice one Robert!

One thing to keep in mind is that as soon as you start doing more complex HTML (think using classes, or embedding styles, and alternating rows, formatting dates and numbers, and other niceties…), then the array.join approach is going to be pretty difficult to code, and pretty soon this will be unreadable/unmaintanable.

IMHO The overall gain in code readability/maintenance you’ll have with Velocity compensates by a long reach any lost in performance compared to any merge method (and I’m not even talking about concatenating String, which is a very bad idea in any language anyway).

ptalbot:
One thing to keep in mind is that as soon as you start doing more complex HTML (think using classes, or embedding styles, and alternating rows, formatting dates and numbers, and other niceties…), then the array.join approach is going to be pretty difficult to code, and pretty soon this will be unreadable/unmaintanable.

I agree with that. And I know, because I have done that many times. :shock:
Using Velocity makes it really easy. :D