我正在开发一个 ps 模块,它可以让您对产品附件进行分类并将其显示在首页产品页面中。
我使用带有附件的可拖动列表,当您将它们拖放到类别时,它会变成选项标签,每个类别都有一个选择标签来放置附件。
我想保存附件和它们被删除的类别,所以我想进行一个ajax调用将数据带入我的模块类,但我是ajax新手,无法接近它。
这就是我所做的:
js 代码(在正确的 .tpl 内):
<script>
$( ".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> which 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",
});
</script>
还有我的类(class):
class CategoryAttachment extends Objectmodel
{
//other functions
public function ajaxProcessCategoryArray()
{
$CategoryAttachmentArr = Tools::getValue('cat_array')
}
}
最佳答案
您无法直接连接到任何类。您必须使用 Controller 来执行此操作。
Ajax发送数据到 Controller
Controller 使用类保存数据
Controller 将结果返回给浏览器(javascript)
关于javascript - 在模块类中使用 ajax 处理 <select> 值,prestashop 1.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45590961/