Editor form column filtering

Editor form column filtering

menashemenashe Posts: 212Questions: 49Answers: 3

How can I accomplish the following?

My Editor form has two (or more) fields, both pulldowns (using Selectize). Since the field arrays are loaded at startup, I cannot implement it via SQL in the PHP file.

Depending on the option that the user selects in pulldown A, I want to filter (based on a FK) the entries shown for pulldown B.

Thanks!

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,889Questions: 26Answers: 5,057
    Answer ✓

    See if this thread helps you get started.

    Kevin

  • menashemenashe Posts: 212Questions: 49Answers: 3

    Hi Kevin,

    Most definitely! And I've used dependent before, just couldn't remember.

    How, though, would I filter the array/json for the second pulldown, using the key/value entered in the first pulldown?

  • menashemenashe Posts: 212Questions: 49Answers: 3

    filter() should work.

  • menashemenashe Posts: 212Questions: 49Answers: 3

    On second thought (after tracing through the code in DevTools), since the JSON for pulldown B is already loaded at that point, it seems that I need to use ajax in the dependent call.

  • allanallan Posts: 64,273Questions: 1Answers: 10,611 Site admin

    Yes, you could do that if you want to get the filtered data from the server. If Selectize offers a way to filter already loaded options, that might work, but I don't immediately recall such an option.

    Allan

  • menashemenashe Posts: 212Questions: 49Answers: 3

    Allan,

    I added the following, which is returning the correct values.

            discountEditor.dependent('crosswalk_prices_to_discounts.discount_id', function(val, data, callback, e) {
                let discount_id = discountEditor.field('crosswalk_prices_to_discounts.discount_id').val();
    
                $.ajax({
                        async: false, // without this, new value is added twice to the items table
                        serverSide: true,
                        type: 'POST',
                        url: 'server_side/scripts/discount_descriptions.php',
                        data: '&discount_id=' + data.row.crosswalk_prices_to_discounts.discount_id,
                        success: function(data) {
                            // console.log('Received data:', data);
                            images = data;
                        }
                    }),
    
                    callback({});
            });
    

    How can I connect it to the field, which currently looks like this?

                }, {
                    label: 'Discount Description:',
                    name: 'crosswalk_prices_to_discounts.discount_description_id',
                    type: 'selectize',
                    opts: {
                        searchField: ['id', 'discount_description', 'store_id'],
                        placeholder: 'Please select a Discount Description...',
                    },
                }, {
    
  • allanallan Posts: 64,273Questions: 1Answers: 10,611 Site admin
    Answer ✓

    Using field().update() in your success callback would be the way to update the options that Selectize has.

    Allan

  • menashemenashe Posts: 212Questions: 49Answers: 3

    Allan,

    Thank you; works great!

    One remaining issue. In the code:

                $.ajax({
                        async: false, // without this, new value is added twice to the items table
                        serverSide: true,
                        type: 'POST',
                        url: 'server_side/scripts/discount_descriptions.php',
                        data: '&discount_id=' + data.row.crosswalk_prices_to_discounts.discount_id,
                        success: function(json) {
                            const filteredDiscountDescriptions = $.grep(json.data, function(description) {
                                return description.discount_id === parseInt(discount_id);
                            }).map(function(description) {
                                return {
                                    label: description.discount_description,
                                    value: description.id,
                                };
                            });
    
                            discountEditor.field('crosswalk_prices_to_discounts.discount_description_id').update(filteredDiscountDescriptions);
                        }
                    }),
    
                    callback({});
            });
    

    On the line with the success function, the parameter 'json' is not refreshed. I placed a breakpoint and the code is executed and the value of discount_id is changed, but json retains its value from the first/previous call; therefore, there is nothing new to filter.

    How do I force a refresh?

  • kthorngrenkthorngren Posts: 21,889Questions: 26Answers: 5,057

    Use the browser's network inspector to see what the response JSON is. It sounds like the server might be returning the same JSON each time.

    Kevin

  • menashemenashe Posts: 212Questions: 49Answers: 3

    Never mind! I had a bad field.

Sign In or Register to comment.