首页 > 解决方案 > 如何在 Odoo OCA 小部件 web_widget_x2many_2d_matrix 中按序列而不是名称对记录进行排序?

问题描述

我已经尝试通过 jquery 按顺序对记录的字典进行排序但没有成功我不知道按名称再次排序的位置。

我在 git 上询问社区,但没有人回答我,我正在尝试按 odoo 序列排序。使用模块 web_widget_x2many_2d_matrix 和sale_order_variant_mgmt

我修改了python代码,如果我调试记录列表,排序是预期的,但是当加载javascript代码时,它按名称排序并且无法调试问题出在哪里

@api.onchange('product_tmpl_id')
    def _onchange_product_tmpl_id(self):
        self.variant_line_ids = [(6, 0, [])]
        template = self.product_tmpl_id
        context = self.env.context
        record = self.env[context['active_model']].browse(context['active_id'])
        if context['active_model'] == 'sale.order.line' or context['active_model'] == 'sale.order.line_group': #TODO check this modify for lentex group_sale_lines module
            sale_order = record.order_id
        else:
            sale_order = record

        num_attrs = len(template.attribute_line_ids)
        if not template or not num_attrs:
            return
        line_x = template.attribute_line_ids[0]
        line_y = False if num_attrs == 1 else template.attribute_line_ids[1]
        lines = []

        for value_x in line_x.value_ids.sorted(key=lambda r: r.sequence):  
            for value_y in line_y and line_y.value_ids.sorted(key=lambda r: r.sequence) or [False]: #I modify this and in python the sort is the intended, but not in JS
                # Filter the corresponding product for that values
                values = value_x
                if value_y:
                    values += value_y
                product = template.product_variant_ids.filtered(lambda x: not(values - x.attribute_value_ids))[:1]
                order_line = sale_order.order_line.filtered(lambda x: x.product_id == product)[:1]
                lines.append((0, 0, {
                    'product_id': product,
                    'disabled': not bool(product),
                    'value_x': value_x,
                    'value_y': value_y,
                    'product_uom_qty': order_line.product_uom_qty,
                }))
        self.variant_line_ids = lines

我认为问题出在这里

 // get x axis values in the correct order
        get_x_axis_values: function()
        {
            return _.keys(this.by_x_axis); //I think here is where the order is defined
        },
        // get y axis values in the correct order
        get_y_axis_values: function()
        {
            return _.keys(this.by_y_axis); //I think here is where the order is defined
        },

标签: javascriptpythonodoo

解决方案


看起来您对字典进行了排序,但字典没有或没有顺序。

创建一个临时列表以根据值按顺序保存键值,然后遍历该列表以按所需顺序处理字典值。

或者,您可以在 python 中使用“OrderedDict” from collections import OrderedDict


推荐阅读