2

I have a custom module where in the managerial interface, the user can select products from a Grid (rendered in a modal) to add to a list specific to the Module's functionality. This part is working great!

I am encountering a bit of a weird issue with how the serializer works in that I can't seem to figure out how to "reset" it along with the Grid. This is a very simple problem but hard to explain, so bear with me!

Steps to reproduce:

  1. User opens Product Chooser Modal Window and selects a few products (for example, lets say product ids 8, 10 and 15 are selected) via the checkboxes in the grid. The user clicks "Add Products" and the products are added to their list, the modal window closes. All good.

  2. The user needs to add another set of products, so they re-open the modal window. I programmatically reload the Grid before the modal appears, and manually empty the serializer's hidden selected_products input before the modal window is rendered, as so:

    var varienGrid      = this.getVarienGridJsObject(),
        serializerInput = $('input[name="selected_products"]');
    
    // logs "8&10&15" i.e., previously selected product id's
    console.info('before grid reload products', serializerInput.val());
    
    // reload the grid with empty params as to not re-select products
    varienGrid.reloadParams = {};
    varienGrid.reload();
    
    // attempting to "reset" the serializer by null'ing the input field
    serializerInput.val(null);
    
    // now this will log out empty, which is intended
    console.info('after grid reload products', serializerInput.val());
    
  3. Now the Grid is reloaded for the User to select their new set of products to add to the Module's product list. However, as soon as the select a single product in this freshly loaded grid, the serializer input field "magically" contains all of the previously selected product ids! So if the user selected product id "20", the serializer's input would contain "8&10&15&20" i.e., the new selection, plus all of the product id's selected before the Grid was reloaded and the hidden input was emptied.

What I've Tried

Aside from the above "solution" that didn't end up working, I dug into the varienGrid JS object and found that in the setCheckboxChecked method a callback is invoked which is, as far as I can tell, what handles updating the selected_products input field with new values, as commenting this callback out results in the input field not getting updated. I thought the solution would be to override the getCheckboxCheckCallback method to provide my own method (e.g., MyJsObject.handleCheckboxChange) in my Grid class which extends Mage_Adminhtml_Block_Catalog_Product_Widget_Chooser but doing so still results in the hidden field being updated.

Any help regarding how to "reset" the Mage_Adminhtml_Block_Catalog_Product_Widget_Chooser serializer would be awesome, thank you and please let me know if I can clarify anything or provide any additional information!

1

Although this may not work for everyone, my solution was to overload the getCheckboxCheckCallback() method in my Grid class which extends Mage_Adminhtml_Block_Catalog_Product_Widget_Chooser, as-so:

public function getCheckboxCheckCallback()
{
    return "function(grid,element,checked){
        MyModuleJsObject.productChooser.handleCheckboxChange(element, checked).bind(MyModuleJsObject.productChooser);
    }";
}

Since all I need to do is keep track of what checkboxes are checked in the Grid so they can be added to my module's Product List when the user clicks the 'Add Products to List' button, the JS is simple:

handleCheckboxChange: function(element, checked){
    var productId = $(element).val();
    if(checked){
        this.selectedProductIds.push(productId);
    }else{
        this.selectedProductIds.splice(this.selectedProductIds.indexOf(productId), 1);
    }
}

Then, when the user clicks the 'Add Products to List' button, the handler simply POST's the productId's in this.selectedProductIds to the action controller which takes care of persisting the data.

I feel like this is a fairly graceful and non intrusive approach, I'm happy with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.