django - showing all fields in a Dojo Data Grid with dojo.store.JsonRest -
i have dojo data grid displaying contact information showing values 2 columns: "model" , "pk". other columns blank, because json response server puts other name/value pairs inside of "fields":
[{"pk": 1, "model": "accounting.contacts", "fields": {"mail_name": "andy", "city": "grand rapids", "zip": "49546", "country": "us", "state": "mi"}}]
what best way fields show in grid?
here's relevant view in django:
def contacts(request): json_serializer = serializers.get_serializer("json")() json_contacts = json_serializer.serialize(contacts.objects.all(), ensure_ascii=false) return httpresponse(json_contacts, mimetype="application/json")
and here's dojo page:
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojo/dojo.xd.js" data-dojo-config="isdebug: true,parseonload: true"> </script> <script type="text/javascript"> dojo.require("dojo.store.jsonrest"); dojo.require("dojox.grid.datagrid"); dojo.require("dojo.data.objectstore"); dojo.ready(function(){ objectstore = new dojo.store.jsonrest({target:"/contacts/"}); //alert(objectstore); datastore = new dojo.data.objectstore({objectstore: objectstore}); //alert(datastore); layoutgridcontacts = [{ field: 'mail_name', name: 'name', width: '200px' }, { field: 'model', name: 'db table', width: '100px' ... }]; gridcontacts = new dojox.grid.datagrid({ query: { name: '*' }, store: datastore, clientsort: true, structure: layoutgridcontacts }, dojo.byid("containergridcontacts")); gridcontacts.startup(); }); </script> <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/dojo/1.6/dijit/themes/claro/claro.css" /> <style type="text/css"> @import "http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojox/grid/resources/grid.css"; @import "http://ajax.googleapis.com/ajax/libs/dojo/1.6/dojox/grid/resources/clarogrid.css"; .dojoxgrid table {margin: 0; } html, body { width: 100%; height: 100%; margin: 0;} </style> </head> <body class="claro"> <div id="containergridcontacts" style="width: 100%, height: 100%;"> </div> </body>
thanks.
this question of, "how interact javascript object?" given json in question, , assuming assigned variable obj
, access mail_name
obj[0]['fields']['mail_name']
or using dot notation, obj[0].fields.mail_name
. haven't used dojo, i'd wager need set fields.mail_name
field
in layoutgridcontacts
.
Comments
Post a Comment