xTuple.com xTupleU Blog & News Customer Support

Retrieving values from a QList in Javascript

I’ve been attempting to retrieve text values from a QList (extracted from an XTreeWidget) and put them into a concatenated string for use in a MetaSQL query (WHERE IN ())

However the specific format for retrieving the values is not clear. The at(index) method is not implemented and value(index) does not seem to work either. See below for the non-functioning code:

  var tags = _custTagFilter.selectedItems();
  if (tags.length > 0) {
    params.tags = tags.value(0).toString();
    for (var i = 1; i < tags.length; i++) {
      params.tags = params.tags + ',' + tags.value(i).toString();
    }
  }

Any help is appreciated.

Lee,

You should be able to pass a JavaScript array as the parameter value:

var tags = _custTagFilter.selectedItems();
if (tags.length > 0) {
  params.tags = [];
  for (var i = 0; i < tags.length; i++) {
    params.tags.push(tags.value(i).toString());
  }
}

You should also be able to map() the list of XTreeWidgetItems to the list you need, which looks something like this:

var tags = _custTagFilter.selectedItems()
                         .map(function (e) { return e.toString(); });
if (tags.length > 0)
  params.tags = tags;

The value method is not implemented for Qlist, so that one is out.

The second variation passes the array as null strings. Am I over-thinking this? Should I just copy the array and pass that to MetaSQL and resolve with a foreach?

The problem appears to be partially with the QList that selectedItems() is returning. All values are null, even though the number of returned items in the array matches the number of selected items.

Here’s the code that populates it:

var tparams = {};
var tqry = "SELECT tag_name FROM bayeq.tag JOIN bayeq.taguse ON (tag_id = taguse_tag_id) WHERE taguse_target_type = 'C' ORDER BY tag_name ASC; ";
var tresults = toolbox.executeQuery(tqry, tparams);
if (tresults.first()) {
  _custTagFilter.populate(tresults, false);
}

Oh! The query to populate an XTreeWidget is expected to return the id column first. This might explain some of the weirdness. Try

var tqry = "SELECT tag_id, tag_name"
         + "  FROM bayeq.tag JOIN bayeq.taguse ON tag_id = taguse_tag_id"
         + " WHERE taguse_target_type = 'C' ORDER BY tag_name ASC;";

Then rereading the map() suggestion above I see a bug (of course!). selectedItems() is a list of XTreeWidgetItems, each of which is a container of sorts. We have to extract the data of interest from each item, not get a stringified container:

var tags = _custTagFilter.selectedItems()
                         .map(function (e) { return e.text('tag_name'); });
// or
var tags = _custTagFilter.selectedItems()
                         .map(function (e) { return e.id(); });

Then set params.tags = tags; and use foreach in the MetaSQL to process the list. If this still doesn’t work we’ll need to discuss in more depth offline.

That was the issue. I was expecting to see a length of greater than zero for any of the XTreeWidgetItems, but I guess that doesn’t apply in this case.

Thanks for the help.