todos.json() returns a list of dictionaries, each one being a row. in js 
terms, it's an array of objects.

skimming through handsontable docs 
<http://handsontable.com/demo/datasources.html>, you can use an array of 
objects to populate the table, but if you want your own headers (and 
possibly in order) you should specify not only "data" but also "colheaders" 
(if you want headers not to be "A", "B", "C", etc) and "columns".
you can "forge" a "columns" array easily, with all the fields in the 
correct order (needs to be put in json if you pass it on the view)

[{'data': f} for f in db.todo.fields]



On Wednesday, April 8, 2015 at 11:03:02 PM UTC+2, Tom Campbell wrote:
>
> I have a query result named todos I am loading into a Handsontable in the 
> view. The code below populates the Handsontable, but columns do not  appear 
> in the order they are defined in db.define_table().
> In this case, while the database columns are 
> 'id','title'.'description','priority', and 'completed', but Handsontable 
> renders in the order 'description','title','completed', 'priority', and 
> 'id'.
>
> What is the most idiomatic way to ensure Handsontable populates in the 
> right column order? I know todos.colnames does list them as desired.
>
> *CONTROLLER FILE default.py:*
> # Fetch all todos completed or now into a query result
> def index():
>     return dict(todos=db(db.todo).select())
>
>
> *VIEW FILE index.html:*
> <!-- List all todos -->
> {{extend 'layout_adminlte.html'}}
> <script src="http://handsontable.com/dist/handsontable.full.js";></script>
> <link rel="stylesheet" media="screen" href="
> http://handsontable.com/dist/handsontable.full.css";>
> <div id="todo-table"></div>
> <script>
> var    
>     todoTableData = {{=XML(todos.json())}},
>     todoTableDiv = document.getElementById('todo-table'),
>     todoTableSettings = {
>         data:todoTableData
>     },
>     todoTableRef;    
> todoTableRef = new Handsontable(todoTableDiv, todoTableSettings);
>     
> </script>
>
> *MODEL FILE db.py:*
> db.define_table('todo',
>     Field('title',notnull=True),
>     Field('description','text'),
>     Field('priority','integer',default=3,
>         requires=IS_IN_SET([1,2,3,4,5],
>         labels=[T('Very low'), T('Low'), T('Medium'), T('High'), T('Very 
> High')], zero=None)),
>     Field('completed','boolean',default=False),format='%(name)s')
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to