# More about JHS with the DHTMLX Grid

I have resolved my DHTMLX standard edition row data extraction problem. The standard edition does not serialize grids or track user cell changes. You have to pay for such luxuries. Because I’m a foul software Grinch and this is just an exploratory hack I had to roll my own. I am posting the relevant JavaScript because I could not find similar examples. Here is how you can fetch rows from standard edition DHTMLX grids and save them as JSON in a hidden textarea element. Eric Iverson suggested hidden textareas and they work like a charm.

function ev_saveme_click(){

if ('undefined' != typeof grid0){

if (0 == grid0.getRowsNum()){
jbyid("rerowcnt").innerHTML = "No rows to save";
return;
}

var st = new Date().getTime(),  // start time
ids = grid0.getAllRowIds(","),
ccnt = 1 + grid0.getColumnsNum();  // includes id

ids = ids.split(",");
var rcnt = ids.length,
tab = new Array(rcnt);

// header row - tab[0][0] cell ignored
tab[0] = new Array(ccnt);
for (var i = 1; i < ccnt; i++) {
tab[0][i] = grid0.getColumnLabel(i-1,0);
}

// cells with leading row id
for (var i = 0 , si = 1 ; i < rcnt; i++ , si++) {
tab[si] = new Array(ccnt);
for (var j = 1; j < ccnt; j++) {
tab[si][j] = grid0.cells((+ids[i]),j-1).getValue();
}
tab[si][0] = ids[i];
}

// prefix row column counts
var pfx = (rcnt+1) + " " + ccnt + "*";
jbyid("gridchgs").innerHTML = pfx + JSON.stringify(tab);
jdoajax(["gridchgs","tout"],"");

var et = new Date().getTime() - st;  // end time
jbyid("rerowcnt").innerHTML= " row count= " + grid0.getRowsNum() +
",  JavaScript ms= " + et;

} else {

jbyid("rerowcnt").innerHTML= "Nothing to save";
}
}


Passing data back to J is fast but the J JSON addon convert\json burps on large datasets. For this demo I substituted a simple table oriented parser that is much faster.

# JHS with the DHTMLX Grid

Grids are the most important GUI user object. It’s hard to think of a user-friendly data munching application that doesn’t have a grid beating at its heart. Consequently, any serious GUI interface contender must support grids. My previous post showed how to use MathJax with JHS. MathJax is an impressive and important JavaScript library; it clearly demonstrates the potential of CHJ1 GUI interfaces but let’s face it, mathematical typesetting will not win many consulting contracts. Grids won’t seal the deal either but their absence is a huge “next” signal. To support serious business and technical applications JHS needs grids.

Fortunately, the JavaScript world is grid saturated. The difficulty is not finding a grid but choosing among dozens of candidates. For this demo I Googled around and found DHTMLX. According to this probably biased article the DHTMLX grid performs well on large inputs and, more importantly, there is an open source version.

You have to start somewhere so I opted to use DHTMLX to build a simple CSV file editor. The CSV files I am going to edit are TAB delimited text files. Each file has a fixed number of columns with column names in the first row. Here is an example TAB delimited file. The idea is to load the file data into the grid. Tweak a few rows and save the result. By increasing the size of the CSV file we can gauge the performance of the grid. Let’s get started.

Using the DHTMLX grid requires some preparation.

1. Create a local directory and edit J’s ~config/folders.cfg to reference the directory with the name GridDemo. jpath '~GridDemo' should return the full directory path.
2. Download the files in the GridDemo folder and copy them to ~GridDemo.
3. Download the Standard Edition (Version 3.5) of DHTMLX. The distribution file dhtmlxGrid.zip contains the grid source and supporting files.
4. Extract the /dhtmlxGrid/codebase/ directory from dhtmlxGrid.zip and copy the entire directory tree to ~GridDemo.
5. Also extract /dhtmlxGrid/samples/common from dhtmlxGrid.zip and copy the directory to ~GridDemo.

When you’re finished the top-level of ~GridDemo will look like the following where names without extensions are directories.

    calendar           dhtmlxgrid.js         GridDemo.ijs   t100rows.txt
common             dhtmlxgrid_skins.css  imgs           t5000rows.txt
dhtmlxcommon.js    excells               jodoval.png
dhtmlxgridcell.js  ext                   skins
dhtmlxgrid.css     favicon.ico           t1000rows.txt

The main J script is ~GridDemo\GridDemo.ijs. Start JHS and load this file.

    load '~GridDemo/GridDemo.ijs'

Then browse to this site.

    http://127.0.0.1:65001/GridDemo

If all goes well you will see the following GridDemo page after pressing the Edit Grid button.

Screenshot of GridDemo running on Chrome

To load and edit files enter their fully qualified names in the Input and Output boxes and press Edit Grid. To edit a cell double-click it. To save changes press Save Grid.2 There are more sophisticated ways to pick files on JavaScript pages. It’s easy to pop up standard host OS file dialogs but it’s not particularly easy to determine host directory paths. This post outlines the demons web programmers must slay to select host files. JHS circumvents these difficulties by asking the J server, which is a typically a local console process, to do the dirty work. JavaScript’s access to local files is limited for security reasons but J has no such restrictions. Use the force Luke!

Three test files t100rows.txt, t1000rows.txt, and t5000rows.txt are included with the demo. On my test machines load times vary from fractions of a second for the smaller files to nine seconds for the largest. This is competitive with the basic C# grid control and fast enough for serious work.

In subsequent posts I will explore JavaScript/JHS graphics options and start the process of integrating, grids, graphs and MathJax with JHS.

1. CSS, HTML and JavaScript.
2. The freebie version of DHTMLX does not support grid serialization. Here is how to roll your own.