From 02636b462291084c1c2097967abde3bef12c2fc9 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Thu, 7 Apr 2011 14:46:39 +0200 Subject: [PATCH] [ADD] method to get the selection (list of identifiers) from a list view bzr revid: xmo@openerp.com-20110407124639-tua9qkcl4w8mhtdc --- addons/base/static/src/js/list.js | 14 ++++++++++++++ addons/base/static/test/list.js | 16 +++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/addons/base/static/src/js/list.js b/addons/base/static/src/js/list.js index 65f503649b8..296f8d09759 100644 --- a/addons/base/static/src/js/list.js +++ b/addons/base/static/src/js/list.js @@ -143,6 +143,20 @@ openerp.base.ListView = openerp.base.Controller.extend( do_update: function () { var self = this; self.dataset.fetch(self.dataset.fields, 0, self.limit, self.do_fill_table); + }, + /** + * Gets the ids of all currently selected records, if any + * @returns a list of ids, empty if no record is selected (or the list view is not selectable + */ + get_selection: function () { + if (!this.options.selectable) { + return []; + } + var rows = this.rows; + return this.$element.find('th.oe-record-selector input:checked') + .closest('tr').map(function () { + return rows[$(this).prevAll().length].id; + }).get(); } }); diff --git a/addons/base/static/test/list.js b/addons/base/static/test/list.js index 92c0a092ee1..e7d8152dee1 100644 --- a/addons/base/static/test/list.js +++ b/addons/base/static/test/list.js @@ -45,7 +45,6 @@ $(document).ready(function () { }); }); asyncTest('render no checkbox if selectable=false', 1, function () { - var listview = new openerp.base.ListView( {}, null, 'qunit-fixture', {model: null}, false, @@ -58,4 +57,19 @@ $(document).ready(function () { start(); }); }); + asyncTest('select a bunch of records', 2, function () { + var listview = new openerp.base.ListView( + {}, null, 'qunit-fixture', {model: null}); + listview.on_loaded(fvg); + + listview.do_fill_table([{id: 1}, {id: 2}, {id: 3}]).then(function () { + listview.$element.find('tbody th input:eq(2)') + .attr('checked', true); + deepEqual(listview.get_selection(), [3]); + listview.$element.find('tbody th input:eq(1)') + .attr('checked', true); + deepEqual(listview.get_selection(), [2, 3]); + start(); + }); + }); });