Context menu fail when data source is web2py

I’m using HandsOnTables with a web2py app. All seems to work well, except that I cannot get the alter(‘row_above’) or alter(‘row_below’) to work. In the context menu, they are grayed out, and if I invoke them in code, nothing happens. Some digging around turned up a post (somewhere) that these two don’t work unless the data source is an array. In fact, when I replace the web2py data source with an array in the HTML for the view, the two add row commands do work as advertised.

The two alter functions in the context menu work with this:

$(document).ready(function() {

var data = [
		{
		    "id":1,
			"last_name":"Abbrederis",
		    "first_name":"Jason",
		    "pos":"WR"

		},
		{
		    "id":2,
			"last_name":"Abbrederis",
		    "first_name":"Jason",
		    "pos":"WR"
		},
		{
		    "id":3,
			"last_name":"Adams",
		    "first_name":"Davante",
		    "pos":"WR"
		}
		];


var hotElement = document.getElementById('list');
    datatable = new Handsontable(hotElement, {
	    data: data,
   <...snip rest of table configuration...>

They do not work with this:

var data = {{=XML(injuries.json())}}

var hotElement = document.getElementById('list');
datatable = new Handsontable(hotElement, {
	    data: data,
   <...snip rest of table configuration...>

If I directly invoke datatable.alter(‘insert_row’,0), I get no error, but nothing happens.

How can I make the context menu options “Insert row above” and “Insert row below” work when the datasource is JSON from web2py?

Hi @jzumsteg I see that you’ve an interesting project going on. I’d be happy to help but unfortunately I’ve never used web2py before. Could you share a demo (preferably using JSFiddle/JSBin or CodePen) were we could try to work it out?

I will try to put this up on a demo site. I’m not at all sure about how to put an entire web2py app on those, but I’ll give it a try.

Hi, @jzumsteg, just like @aleksandra_budnik said, we don’t have much experience with web2py, so a demo would be greatly appreciated. If you cannot make it on the sites that @aleksandra_budnik listed, please make a demo on your server, or wherever you find it easier, so we could look at a working example.

Also, what does exactly {{=XML(injuries.json())}} return? Is it an array of objects, like in your first example?

Thanks for your interest in getting this solved. I am away from my office on business for a few days, but will try to put together a demo this weekend and post it on a website for you to look at.

In response to your question, {{=XML(injuries.json())}} does return an array of objects. I can look at the array returned by this statement in the page’s HTML and see that it’s a valid and correct format.

I’ll let you know when I’ve got a working demo for you. I’d really like to get this resolved, as I think HandsInTables is an excellent product, but I have to be able to add rows to use it in production.

Again, thanks for your interest.

I built a simple test web2py app to show the problem I detailed above. The app uses web2py to select from the database and sends it to a view that uses HandsOnTable. The test app, of course, works perfectly, with all context menu items, working as they’re supposed to work, including add_row.

Clearly, the problem I describe above is not a HandsOnTables problem, but some quirk in my full app that’s causing the problem. I will try to figure it out, and post what I found when I do solve the problem. Some other web2py developer may benefit.

This is too simple: I had a maxrows value in the table constructor and the dataset had more rows in it than the maxrow value. So, of course, the table cannot expand beyond the maxrows value. Once I removed the maxrows value, the table and context menu work as they should. I simply had not tested the table’s size, since I was wrapped up in trying to make the context menu work. Sorry to take up everyone’s time on this.

@jzumsteg
Thanks for an update.

Glad everything’s working now.