ListGrid fromIndex(0) -> toIndex(-1) error (server-side dsRequest.endRow = -1)

Hi,

I’ve been using a DMI to fetch rows for my ListGrid , using the approach described in the SupplyItemDMI example:

    public DSResponse fetch(ProdutoSeguroBean criteria, DSRequest dsRequest) throws Exception {

...
        
        long startRow = dsRequest.getStartRow();
        long endRow = dsRequest.getEndRow();
        
        long totalRows = matchingItems.size();

        dsResponse.setTotalRows(totalRows);
        dsResponse.setStartRow(startRow);
        
        endRow = Math.min(endRow, totalRows);
        dsResponse.setEndRow(endRow);
        
        List results = matchingItems.subList((int)dsResponse.getStartRow(),
                (int)dsResponse.getEndRow());
        dsResponse.setData(results);
        
        return dsResponse;
    }

While debuging, I’ve noticed that endRow = -1 (matchingItems is filled correctly), so after Math.min, dsResponse.endRow = -1, leading to the error “fromIndex(0) -> toIndex(-1)”. If I set endRow = totalRows, everything works fine, but I guess I’d lose my ability to paginate through large datasets.

I don’t know if there’s something missing in the ListGrid declaration:

isc.ListGrid.create({
	autoDraw:false, 
	fields: [
		{name:"Produtos", value: "nomeProduto", width: 250},
                {name:"Versão", value: "versaoProduto", width: 100}
	],
        dataSource: "produtoSeguro",
        autoFetch: false,
	emptyMessage: "Nenhum produto cadastrado no sistema.",
	ID:"produtosGrid"
})

Any suggestions?

Best,
Leo

Hi Leonardo
What’s happening here is that there are no matching rows returned from your dataSet. In this case totalRows gets set to 0, startRow gets set to 0 and endRow gets set to -1.

In this situation calling ‘matchingItems.subList(startRow, endRow)’ will lead to the error you’re seeing (it’s a result of a java exception on the server when the subList() method is passed bad arguments).

You can avoid this by explicitly catching the case of matchingRows having length zero and avoiding the call to ‘subList’ in this case - something like this:

        ....

        long totalRows = matchingItems.size();

        ....

        endRow = Math.min(endRow, (totalRows-1));
        dsResponse.setEndRow(endRow);

        List results;
        if (totalRows > 0) {
            results = matchingItems.subList((int)dsResponse.getStartRow(), 
                                                 (int)dsResponse.getEndRow());
        } else {
            results = matchingItems;
        }

In fact you’ll notice another change in our logic here: I’ve modified the Math.min(…) call to clamp ‘endRow’ to totalRows-1 rather than totalRows.

We’ll handle either version when the response returns to the client, but this is more appropriate as endRow should be set to the index of the last row in the returned set of data.

Let us know how you get on
Thanks
Isomorphic Software

Hi,

Everything works fine now.

Thanks!

Hi,

Actually, there was a problem with Math.min(endRow, (totalRows-1)): when totalRows > 1, say totalRows = 2 and endRow > totalRows, endRow turns to be 1. Thus, one row will be always missing and the component will fetch again to obtain the remaining row, entering in a loop condition.

I just removed (totalRows-1) and replaced with the old approach Math.min(endRow, totalRows) and everything is fine now.

Thanks!