[FIX] web: fix rendering issue with fieldselection on m2o
Before this commit, the FieldSelection did not work properly in edit mode with a many2one. This was because it did not take the correct value into account, and the test did not notice it, so both had to be fixed.
This commit is contained in:
committed by
Aaron Bohy
parent
920df597d3
commit
2a40c78a8b
@@ -1337,7 +1337,6 @@ var FieldSelection = AbstractField.extend({
|
||||
*/
|
||||
init: function () {
|
||||
this._super.apply(this, arguments);
|
||||
this.values = [];
|
||||
if (this.field.type === 'many2one') {
|
||||
this.values = this.record.specialData[this.name];
|
||||
} else {
|
||||
@@ -1352,35 +1351,24 @@ var FieldSelection = AbstractField.extend({
|
||||
// Private
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Format the value to a valid string
|
||||
*
|
||||
* @override
|
||||
* @private
|
||||
* @param {any} value
|
||||
* @returns {string}
|
||||
*/
|
||||
_formatValue: function (value) {
|
||||
if (this.field.type === 'many2one') {
|
||||
var options = _.extend({}, this.nodeOptions);
|
||||
return field_utils.format.many2one(value, this.field, this.recordData, options);
|
||||
} else {
|
||||
return this._super(value);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* @override
|
||||
* @private
|
||||
*/
|
||||
_renderEdit: function () {
|
||||
this.$el.empty();
|
||||
for (var i = 0 ; i < this.values.length ; i++) {
|
||||
this.$el.append($('<option/>', {
|
||||
value: JSON.stringify(this.values[i][0]),
|
||||
html: this.values[i][1]
|
||||
}));
|
||||
if (!this.$el.children().length) {
|
||||
for (var i = 0 ; i < this.values.length ; i++) {
|
||||
this.$el.append($('<option/>', {
|
||||
value: JSON.stringify(this.values[i][0]),
|
||||
html: this.values[i][1]
|
||||
}));
|
||||
}
|
||||
}
|
||||
this.$el.val(JSON.stringify(this._parseValue(this.value)));
|
||||
var value = this.value;
|
||||
if (this.field.type === 'many2one' && value) {
|
||||
value = value.data.id;
|
||||
}
|
||||
this.$el.val(JSON.stringify(value));
|
||||
},
|
||||
/**
|
||||
* @override
|
||||
@@ -1389,16 +1377,6 @@ var FieldSelection = AbstractField.extend({
|
||||
_renderReadonly: function () {
|
||||
this.$el.empty().html(this._formatValue(this.value));
|
||||
},
|
||||
/**
|
||||
* @override
|
||||
* @private
|
||||
*/
|
||||
_reset: function () {
|
||||
this._super.apply(this, arguments);
|
||||
if (this.field.type === 'many2one') {
|
||||
this.value = this.value.data.id;
|
||||
}
|
||||
},
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Handlers
|
||||
@@ -1407,6 +1385,7 @@ var FieldSelection = AbstractField.extend({
|
||||
/**
|
||||
* The small slight difficulty is that we have to set the value differently
|
||||
* depending on the field type.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
_onChange: function () {
|
||||
|
||||
@@ -3196,8 +3196,12 @@ QUnit.module('relational_fields', {
|
||||
list.destroy();
|
||||
});
|
||||
|
||||
QUnit.test('widget selection on a many2one field', function (assert) {
|
||||
assert.expect(7);
|
||||
QUnit.test('widget selection, edition and on many2one field', function (assert) {
|
||||
assert.expect(15);
|
||||
|
||||
this.data.partner.onchanges = {product_id: function () {}};
|
||||
this.data.partner.records[0].product_id = 37;
|
||||
this.data.partner.records[0].trululu = false;
|
||||
|
||||
var count = 0;
|
||||
var form = createView({
|
||||
@@ -3206,10 +3210,13 @@ QUnit.module('relational_fields', {
|
||||
data: this.data,
|
||||
arch: '<form string="Partners">' +
|
||||
'<field name="product_id" widget="selection"/>' +
|
||||
'<field name="trululu" widget="selection"/>' +
|
||||
'<field name="color" widget="selection"/>' +
|
||||
'</form>',
|
||||
res_id: 1,
|
||||
mockRPC: function (route, args) {
|
||||
count++;
|
||||
assert.step(args.method);
|
||||
return this._super(route, args);
|
||||
},
|
||||
});
|
||||
@@ -3217,14 +3224,25 @@ QUnit.module('relational_fields', {
|
||||
assert.ok(!form.$('select').length, "should not have a select tag in dom");
|
||||
form.$buttons.find('.o_form_button_edit').click();
|
||||
|
||||
assert.ok(form.$('select').length, "should have a select tag in dom");
|
||||
assert.ok(form.$('option:contains(xphone)').length, "should have fetched xphone option");
|
||||
assert.ok(form.$('option:contains(xpad)').length, "should have fetched xpad option");
|
||||
assert.strictEqual(form.$('select').length, 3,
|
||||
"should have 3 select tag in dom");
|
||||
assert.strictEqual(form.$('select[name="product_id"] option:contains(xphone)').length, 1,
|
||||
"should have fetched xphone option");
|
||||
assert.strictEqual(form.$('select[name="product_id"] option:contains(xpad)').length, 1,
|
||||
"should have fetched xpad option");
|
||||
assert.strictEqual(form.$('select[name="product_id"]').val(), "37",
|
||||
"should have correct product_id value");
|
||||
assert.strictEqual(form.$('select[name="trululu"]').val(), "false",
|
||||
"should not have any value in trululu field");
|
||||
form.$('select[name="product_id"]').val(41).trigger('change');
|
||||
|
||||
assert.strictEqual(form.$('select').val(), "false", "should not have any value");
|
||||
form.$('select').val(37).trigger('input');
|
||||
assert.strictEqual(form.$('select').val(), "37", "should have a value of xphone");
|
||||
assert.strictEqual(form.$('select[name="product_id"]').val(), "41",
|
||||
"should have a value of xphone");
|
||||
|
||||
assert.strictEqual(form.$('select[name="color"]').val(), "\"red\"",
|
||||
"should have correct value in color field");
|
||||
|
||||
assert.verifySteps(['read', 'name_search', 'name_search', 'onchange']);
|
||||
count = 0;
|
||||
form.reload();
|
||||
assert.strictEqual(count, 1, "should not reload product_id relation");
|
||||
|
||||
Reference in New Issue
Block a user