Jump to content

Handle <select> values with ajax in module class, prestashop 1.6


Recommended Posts

Im developing a ps module wich let you classidy the product attachment in categories an display them in the front product page.

Im using a draggable list with the attachment, and when you dropp them to the category it turns to an option tag, each category has a select tag where to drop the attachment.

I wanna save the attachments and the category where they was dropped, so I thought make an ajax call to bring the data to my module class but I'm new with ajax and cant approach it.

this is wha I've made:

the js code (inside the proper .tpl):

 $( ".droptrue" ).droppable({
                    
                    drop: function( event, ui ) {
                        //add <option> tag when an attachment is dropped to category's select
                        $(event.target).append('<option value="' + ui.draggable.attr('id') + '" selected>'+ui.draggable.text()+'</option>');
                        //remove the <li> wich contained the attachment data
                        ui.draggable.fadeOut("slow").remove();
                        
                        var val = $('#categoryAttachmentArr').val();
                        var tab = val.split(',');
//                        for (var i=0; i < tab.length; i++)
//                            if (tab[i] == $(this).val())
//                                return false;
                        //create an array with the next format: 'id_category(1)'-'id_attachment(1)','id_category(2)'-'id_attachment(2)'[....]
                        //the comma will be the main character that will be splitted
                        $('#categoryAttachmentArr').val(val + ui.doppable.attr('id') + '-' + ui.draggable.attr('id') +',');
                    }
            });
            
            $('#submitAddProduct').click(function(e){
                $.ajax({
                  type: 'POST',
                  url: baseDir + 'modules/adjuntos/classes/CategoryAttachment.php',
                  data: {
                            ajax: true,
                            action: \'CategoryArray\',
                            cat_array: $('#categoryAttachmentArray').val(),
                        }
                  dataType: 'json',
                  success: function(json) {
                    console.log($('#categoryAttachmentArray').val());
                  }
            });
            })
        
            $( ".ui-state-default" ).draggable({ 
                    
                    revert: "valid",
                
            });

And my class:

class CategoryAttachment extends Objectmodel
{
     //other functions
     public function ajaxProcessCategoryArray()
    {
        $CategoryAttachmentArr = Tools::getValue('cat_array')
    }
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...