group_by_fields: this.group_by
}, function () { }).then(function (response) {
self.groups = response.result;
- d.resolveWith(self, [response.result]);
+ // read_group results are annoying: they use the name of the
+ // field grouped on to hold the value and the count, so no
+ // generic access to those values is possible.
+ // Alias them to `value` and `length`.
+ d.resolveWith(self, [_(response.result).map(function (group) {
+ var field_name = self.group_by[0];
+ return _.extend({}, group, {
+ // provide field used for grouping
+ grouped_on: field_name,
+ length: group[field_name + '_count'],
+ value: group[field_name]
+ });
+ })]);
}, function () {
d.rejectWith.apply(d, self, [arguments]);
});
* :js:func:`~openerp.base.DataGroup.list` beforehand will likely result
* in an error.
*
+ * The resulting :js:class:`~openerp.base.DataGroup` or
+ * :js:class:`~openerp.base.DataSet` will be provided through the relevant
+ * callback function. In both functions, the current DataGroup will be
+ * provided as context (``this``)
+ *
* @param {Number} index the index of the group to open in the datagroup's collection
- * @param {Function} ifDataSet executed if the item results in a DataSet, provided with the dataset as parameter and as context
- * @param {Function} ifDataGroup executed if the item results in a DataSet, provided with the datagroup as parameter and as context
+ * @param {Function} ifDataSet executed if the item results in a DataSet, provided with the new dataset as parameter
+ * @param {Function} ifDataGroup executed if the item results in a DataSet, provided with the new datagroup as parameter
*/
get: function (index, ifDataSet, ifDataGroup) {
var group = this.groups[index];
columns: this.columns,
rows: this.rows
});
- $(this.list).bind({
+ this.groups = new openerp.base.ListView.Groups({
+ options: this.options,
+ columns: this.columns
+ });
+ $([this.list, this.groups]).bind({
'selected': function (e, selection) {
self.$element.find('#oe-list-delete')
.toggle(!!selection.length);
self.dataset.context = results.context;
self.dataset.domain = results.domain;
if (results.group_by.length) {
- var group = new openerp.base.DataGroup(
+ self.groups.datagroup = new openerp.base.DataGroup(
self.session, results.group_by, self.dataset);
- group.list().then(function (lst) {
- console.log(this, lst);
- this.get(0, function () {
- console.log('set', this);
- }, function () {
- console.log('group', this);
- });
- });
+ self.$element.html(self.groups.render());
+ return;
}
return self.do_reload();
});
// drag and drop
// editable?
});
-
+openerp.base.ListView.Groups = Class.extend(
+ /** @lends openerp.base.ListView.Groups# */{
+ /**
+ * Grouped display for the ListView. Handles basic DOM events and interacts
+ * with the :js:class:`~openerp.base.DataGroup` bound to it.
+ *
+ * Provides events similar to those of
+ * :js:class:`~openerp.base.ListView.List`
+ */
+ init: function (opts) {
+ this.options = opts.options;
+ this.columns = opts.columns;
+ this.datagroup = {};
+ },
+ make_level: function (datagroup) {
+ var self = this, $root = $('<dl>');
+ datagroup.list().then(function (list) {
+ _(list).each(function (group, index) {
+ var $title = $('<dt>')
+ .text(group.grouped_on + ': ' + group.value + ' (' + group.length + ')')
+ .appendTo($root);
+ $title.click(function () {
+ datagroup.get(index, function (new_dataset) {
+ var $content = $('<ul>').appendTo(
+ $('<dd>').insertAfter($title));
+ new_dataset.read_slice([], null, null, function (records) {
+ _(records).each(function (record) {
+ $('<li>')
+ .appendTo($content)
+ .text(_(record).map(function (value, key) {
+ return key + ': ' + value;
+ }).join(', '));
+ });
+ });
+ }, function (new_datagroup) {
+ console.log(new_datagroup);
+ $('<dd>')
+ .insertAfter($title)
+ .append(self.make_level(new_datagroup));
+ });
+ });
+ });
+ });
+ return $root;
+ },
+ render: function () {
+ return this.make_level(this.datagroup);
+ }
+});
openerp.base.TreeView = openerp.base.Controller.extend({
});