This question has been flagged

I'm trying to allow a user to duplicate a line on a one2many (sales order line to be precise) on the client side.  The customer expressly doesn't want to have to save the sales order in order to do this.

I've tried to add a copy button to the right hand side of ListView:

    <t t-extend="ListView.row">
<t t-jquery=".o_list_record_delete" t-operation="after">
<td t-if="options.deletable">
<button type="button" aria-hidden="true"
class="o_icon_button o_kf_so_line_copy_button">
<i class="fa fa-copy"/>
</button>
<!-- <a class="o_kf_so_line_copy_button"><i class="fa fa-copy"/></a> -->
</td>
</t>
</t>
<t t-extend="ListView">
<!-- TODO little quirk is that striping doesn't carry into copy column. Fix this at end if time and solution otherwise works. -->
<t t-jquery='t[t-set="columns_count"]' t-operation="after">
<t t-set="columns_count" value="columns_count + (options.deletable ? 1 : 0)"/>
</t>
<t t-jquery=".o_list_record_delete" t-operation="after">
<td t-if="options.deletable" class="o_kf_so_line_copy"/>
</t>
</t>

Note a couple of attempts one using <button> and another using <a>.

Here is my javascript where I try to set up a click handler:

odoo.define('acme_flobbits_management.SOLineCopyWidgets', function (require) {
"use strict";


var core = require('web.core');
var Widget = require('web.Widget');
var utils = require('web.utils');
var ListView = require('web.ListView');
var SOLineCopyDialog = require('acme_flobbits_management.SOLineCopyDialog');
ListView.include({
reload_content: function() {
var self = this;
var reloaded = this._super();
reloaded.then(function() {
var $elements = self.$el.find('.o_kf_so_line_copy_button');
// Finding the elements OK
$elements.click(function(e) {
// But this click handler's getting ignored when it's a "span"
// Declaring as button lets it work, but then Odoo ListView gets its knickers in a twist
// list_view.js 996 will give clues for this and for how to get at the data we need!
e.preventDefault();
e.stopPropagation();
SOLineCopyDialog.createSOLineCopyDialog(self);
});
});
return reloaded;
}
});
});


Using the <button> tag, Odoo throws a wobbly on load because the javascript is looking for any button tags nested inside the td and making its own click handler that requires a 'field' data attribute, but my button doesn't bind to a field so I don't think that makes sense.

Using the <a> tag, Odoo happily lets me do it, but the click is ignored and passed through to the row, so the Sale Order Line form is popped up as if I'd clicked elsewhere on the row instead of running the javascript I've tried to connect to the <a> tag's click event.

Looking in the Firefox debugger, there is indeed no little [ev] marker next to my <a> tag in the inspector.

Q1: Firstly, am I approaching this the right way overall (by extending ListView)?  Or is there some standard way to add a column of buttons to a specific field's one2many table, only in Edit mode, and perform some client-side-only action in response to it while keeping knowledge of the record being copied and allowing the one2many view to be updated after adding what I assume will be a new (0, False, {fields}) thing to Javascript's copy of the field.

Q2: Secondly, if I am approaching this the right way, how do I:

  a) get my click handler to be registered and triggered?

  b) find out in my extended ListView whether a particular attribute of the tree in my view arch XML, e.g. <tree kf_copyable_so_line="1"/>, has been set?


Avatar
Discard