I am using SmartClient_v121p_2020-04-14_PowerEdition.
Is it possible to add a column (field) to a DataSource and ListGrid after the initial DS and grid definitions were declared? I have a grid backed by a client only datasource. The grid displays a list of tests. When a test is run, I will receive a result that I want to display. When the test result comes in, I have tried adding a new field to both the DataSource and the ListGrid and then updating the row in the DataSource with the test response. I've tried several variations of invalidating the caches on both the DataSource and ListGrid, but nothing seems to be working. In the code snippet below, what happens is that when the first result comes in, the last column is updated with the data (but not the column header until I hover over it, and then the header title updates), essentially erasing whatever was in that column. Subsequent data is not displayed, although in the debugger, when I look at the values of "fields" for both, the new columns appear to exist. So, the underlying data is there. Am I missing something simple here?
function addTestResult(result) {
const testType = result.testType;
// Build the data source and the grid IDs and check if they exist
const dsID = testType + "_DS";
const gridID = testType + "_grid";
let testDS = window[dsID];
let testGrid = window[gridID];
// If these exist, add the result
if( testDS && testGrid ) {
// The column name should be the time that the result came in.
const time = result.time;
// Check to see if the data source contains the column
let dsField = testDS.getField(time);
if( dsField === undefined ) {
// Build the field definition
let field = {name: time, title: time, width: 80, canSort: false, align: "center"};
// Add this field to the data source and the grid.
// Add the field at the second to last column.
// Per the documentation, addAt puts the item at the specified position and moves everything else over, so
// make the position the last one so that it'll insert it there and then move the last column over.
const numCols = testGrid.fields.length;
let position = 0;
if( numCols > 0 ) {
position = numCols - 1;
}
// NOTE: For some reason, the DS fields don't show up as an array, but a JSON object,
// even though the documentation says fields is an Array.
testDS.fields[time] = field;
testGrid.fields.addAt(field, position);
//testGrid.fields.add(field);
}
// Now that the time column exists, we can add the data
// Get the row that the result belongs on
// See if the row exists (it should because we create all rows at initialization)
let testData = testDS.getCacheData();
if( testData !== undefined ) {
let row = testData.find({testName: result.testName});
if( row !== null ) {
row[time] = result.testResponse;
testDS.updateData(row);
}
}
testGrid.markForRedraw();
//testGrid.redrawHeader(true);
testGrid.scrollToColumn(testGrid.fields.length - 1);
}
}
Is it possible to add a column (field) to a DataSource and ListGrid after the initial DS and grid definitions were declared? I have a grid backed by a client only datasource. The grid displays a list of tests. When a test is run, I will receive a result that I want to display. When the test result comes in, I have tried adding a new field to both the DataSource and the ListGrid and then updating the row in the DataSource with the test response. I've tried several variations of invalidating the caches on both the DataSource and ListGrid, but nothing seems to be working. In the code snippet below, what happens is that when the first result comes in, the last column is updated with the data (but not the column header until I hover over it, and then the header title updates), essentially erasing whatever was in that column. Subsequent data is not displayed, although in the debugger, when I look at the values of "fields" for both, the new columns appear to exist. So, the underlying data is there. Am I missing something simple here?
function addTestResult(result) {
const testType = result.testType;
// Build the data source and the grid IDs and check if they exist
const dsID = testType + "_DS";
const gridID = testType + "_grid";
let testDS = window[dsID];
let testGrid = window[gridID];
// If these exist, add the result
if( testDS && testGrid ) {
// The column name should be the time that the result came in.
const time = result.time;
// Check to see if the data source contains the column
let dsField = testDS.getField(time);
if( dsField === undefined ) {
// Build the field definition
let field = {name: time, title: time, width: 80, canSort: false, align: "center"};
// Add this field to the data source and the grid.
// Add the field at the second to last column.
// Per the documentation, addAt puts the item at the specified position and moves everything else over, so
// make the position the last one so that it'll insert it there and then move the last column over.
const numCols = testGrid.fields.length;
let position = 0;
if( numCols > 0 ) {
position = numCols - 1;
}
// NOTE: For some reason, the DS fields don't show up as an array, but a JSON object,
// even though the documentation says fields is an Array.
testDS.fields[time] = field;
testGrid.fields.addAt(field, position);
//testGrid.fields.add(field);
}
// Now that the time column exists, we can add the data
// Get the row that the result belongs on
// See if the row exists (it should because we create all rows at initialization)
let testData = testDS.getCacheData();
if( testData !== undefined ) {
let row = testData.find({testName: result.testName});
if( row !== null ) {
row[time] = result.testResponse;
testDS.updateData(row);
}
}
testGrid.markForRedraw();
//testGrid.redrawHeader(true);
testGrid.scrollToColumn(testGrid.fields.length - 1);
}
}
Comment