frappe / erpnext

Free and Open Source Enterprise Resource Planning (ERP)
https://erpnext.com
GNU General Public License v3.0
19.78k stars 7.01k forks source link

Change the link fields filter dynamically via script (without the need of a manual full page refresh) #40656

Open GeorgeDeac opened 5 months ago

GeorgeDeac commented 5 months ago

Information about bug

On a client script when we try to dynamically change the filtering on a link type field, via overriding the default set_query method of the field by specifying new filter values, we can't do so dynamically. I've tried both refreshing just the field, and refreshing the entire doctype, but it doesn't work. The new filter value is however applied only after a full manual browser page refresh.

frm.set_query('fieldName', 'doctype', function() {
    return {
        filters: { 'filterField': 'value' }
    };
});

// Refresh the field
frm.refresh_field('fieldName');

// Refresh all fields in the form
frm.refresh_fields();

// Refresh the doc
frm.refresh();

// None of them work, the filters are updated only after hard manual refresh

Is there any other method one could use to achieve this expected functionality?

Module

other

Version

Frappe Framework: v14.47.2 (version-14) ERPNext: v14.37.1 (version-14)

Installation method

manual install

Relevant log output / Stack trace / Full Error Message.

No response

jeshani commented 5 months ago

@GeorgeDeac You can achieve directly setting the query within the onload_post_render event, eliminating the need for the refresh event.


frappe.ui.form.on("doctype", {
    onload_post_render: function(frm) {
        frm.set_query("field", {
            filters: {
                "field1": 1,
                "field2": 2
            }
        });
    }
});
GeorgeDeac commented 3 months ago

@GeorgeDeac You can achieve directly setting the query within the onload_post_render event, eliminating the need for the refresh event.

frappe.ui.form.on("doctype", {
    onload_post_render: function(frm) {
        frm.set_query("field", {
            filters: {
                "field1": 1,
                "field2": 2
            }
        });
    }
});

Yes, but I need to change the filter dynamically by script interaction, depending on how other field/s are changing in real time, not just set it once at page load. The only workaround I found was using autocomplete fields and a custom script to fetch values from the server depending on the parameters taken from the other fields. Is this also possible directly with the link fields by changing the filter in real time?

krunal1904 commented 1 month ago
frm.set_query('fieldName', 'doctype', function() {
    return {
        filters: { 'filterField': 'value' }
    };
});

is applying filter to link field but when i use order_by,limit etc. after the filters that are not applying on link field filter. here is my code: -

frm.set_query('fieldName', 'doctype', function() {
    return {
        filters: { 'filterField': 'value' },
        order_by='date desc',
        limit = 20
    };
});