Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort records by sequence instead of name in Odoo OCA widget web_widget_x2many_2d_matrix?

I already try with no success to sort by sequence a dict of records by jquery I don't know where sorted again by name.

I ask the community on git but nobody answer me, I'm trying to sort by odoo sequence. using modules web_widget_x2many_2d_matrix, and sale_order_variant_mgmt

I modify python code, and if I debug the list of records the sort is the intended, but when the javascript code is loaded, it sorted by name and cant debug where the problem is

@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

I think the problem is here

 // 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
        },
like image 334
Mariano DAngelo Avatar asked Aug 28 '18 15:08

Mariano DAngelo


1 Answers

It looks like your sorting the dictionary, but dictionaries don't have or maintain an order.

Create a temporary list to hold the key values in order based on the value then iterate through that list to handle the dictionary values in the desired order.

alternatively you could use an "OrderedDict" in python from collections import OrderedDict

like image 183
Schalton Avatar answered Oct 21 '22 11:10

Schalton