Odoo POS自定义模块调用orderline_remove函数及获取line参数方案咨询
Got it, let's break this down for you! The core issue here is figuring out how to grab the Orderline instance that your orderline_remove function needs—this depends on when you want to trigger the function. Here are two common scenarios with working code examples:
Scenario 1: Trigger remove when clicking an order line (add a custom delete action)
If you want to let users click an order line to remove it, you can bind a click event to the order line DOM elements and fetch the corresponding line instance from the current order:
screens.OrderWidget.include({ renderElement: function(){ this._super(); var self = this; // Bind a click event to each order line (adjust selector for your Odoo version) this.$el.find('.order-line').off('click.remove-line').on('click.remove-line', function(){ // Get the line ID from the DOM element's dataset var lineId = $(this).data('id'); // Fetch the matching Orderline instance from the current order var targetLine = self.pos.get_order().get_orderline(lineId); if(targetLine){ self.orderline_remove(targetLine); } }); }, // Define your orderline_remove function in the extended widget orderline_remove: function(line){ this.remove_orderline(line); this.numpad_state.reset(); this.update_summary(); } });
Scenario 2: Remove a specific line automatically (e.g., on widget render)
If you need to remove a line programmatically when the widget loads (like cleaning up a default line), you can fetch lines directly from the current POS order:
screens.OrderWidget.include({ renderElement: function(){ this._super(); var self = this; // Get all order lines from the current POS order var allOrderLines = self.pos.get_order().get_orderlines(); // Example: Remove the first order line (adjust filtering to match your needs) if(allOrderLines.length > 0){ var lineToRemove = allOrderLines[0]; self.orderline_remove(lineToRemove); } }, orderline_remove: function(line){ this.remove_orderline(line); this.numpad_state.reset(); this.update_summary(); } });
Key Notes to Keep in Mind:
- DOM Selector Adjustment: Depending on your Odoo version, the order line CSS class might differ (e.g.,
.pos-order-lineinstead of.order-line). Inspect the POS UI to confirm the correct selector. - Event Cleanup: Use
.off('click.remove-line')before binding the click event to avoid duplicate event listeners if the widget re-renders multiple times. - Order Instance Access:
self.pos.get_order()always gives you the active POS order, which is where all order lines are stored.
内容的提问来源于stack exchange,提问作者user_odoo
相关产品推荐
相关产品推荐

