在prestashop模块中使用select2

问题描述 投票:0回答:1

我想在我自定义的prestashop模块中使用select2.js。每次我尝试初始化selec2时,我试过的每个解决方案都给出了一个空白页面,告诉他我如何选择。

我找到了一个使用select2的管理页面:AdminModulesPositionController.php,但如果我复制其中的内容失败了。

有人可以帮我找到在我的模块管理页面中有一个select2列表的正确方法吗?

在此先感谢您的帮助

编辑:我做了什么

1)我想修改的模块使用AdminController.php,它调用插件,所以我修改了它:

    $this->addJquery();
    $this->addjQueryPlugin(array('scrollTo', 'alerts', 'chosen', 'autosize', 'fancybox', 'select2' ));

.../...

$this->addJS(array(
    _PS_JS_DIR_.'admin.js?v='._PS_VERSION_,
    _PS_JS_DIR_.'tools.js?v='._PS_VERSION_,
    _PS_JS_DIR_.'jquery/plugins/timepicker/jquery-ui-timepicker-addon.js',
    _PS_JS_DIR_.'jquery/plugins/select2/select2_locale_'.$this->context->language->iso_code.'.js'
));

我检查了调试器,并且模块页面调用了带语言环境的select2:好

2)我在选择中添加了一个类我想要增强3)然后我尝试通过几种方式初始化select: - 就像我的模块的html部分中的文档所说:

<script type="text/javascript">
  $('select').select2();
</script>

所有商店的空白页

- 尝试执行与AdminModulesPositionController.php / modulePosition.js相同的操作:

var show_modules = $("#show_modules");
        show_modules.select2();

同样的问题。

事实上,我是一个糟糕的开发者,所以我不知道prestashop机器是如何工作的,而且我没有找到办法做一个简单的php事情(我在我的“简单”php网站中使用select2)系统..

这是我尝试使用select2的完整功能:

    public function displayinputid($return=0){
   if ($this->psversion()==5 || $this->psversion()==6){
       $verps="";
   } else {
       $verps="14";
   }

      $resultemp= Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('SELECT * FROM `ps_employee` WHERE id_profile=4');

    //idemploye
    $selectemp="<select name=\"idemp\" id=\"idemp\">";

     $selectemp .= "<option value =\"0\" hidden>Employee</option>";
     foreach ($resultemp as $k) {
     $selectemp .= "<option value=".$k["id_employee"].">".$k["firstname"]." ".$k["lastname"]."</option>" ;
    }

     $selectemp.="</select >";
     if (Shop::getContextShopID() !=0) {
        $resultcustomer = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('SELECT * FROM `ps_customer` WHERE id_shop='.Shop::getContextShopID().'');

     }
     else {
        $resultcustomer = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('SELECT * FROM `ps_customer`');

     }

    //idcustomer
    $selectCustomer="<select name=\"idcus\" id=\"idcus\" class=\"idcus\">";

      $selectCustomer .= "<option value =\"0\" hidden>Client</option>";
     foreach ($resultcustomer as $k) {
     $selectCustomer .= "<option value=".$k["id_customer"].">".$k["firstname"]." ".$k["lastname"]." - ".$k["company"]."</option>" ;
    }

     $selectCustomer.="</select >";

      //idcustomer
    $selectCustomerdel="<select name=\"delcus\" id=\"delcus\">";
    $selectCustomerdel .= "<option value =\"0\" hidden>Client</option>";       

     foreach ($resultcustomer as $k) {
     $selectCustomerdel .= "<option value=".$k["id_customer"].">".$k["firstname"]." ".$k["lastname"]."</option>" ;
    }

     $selectCustomerdel.="</select >";






      $resultlistcusemp = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('SELECT  pe.firstname as nomemploi ,
        pe.lastname as nomemploi2 ,
        ps.id_customer as idclient,
        ps.firstname as nomclien ,
        ps.lastname as nomcl2 
        FROM ps_personalsalesmen p
        INNER JOIN ps_customer ps 
        ON p.id_customer=ps.id_customer
        INNER JOIN ps_employee pe 
        ON pe.id_employee  = p.id_employee');

    //idcus
    $listecusemp="<table id=\"example\" class=\"table table-striped table-bordered\" cellspacing=\"0\" width=\"100%\"> <tr><thead><td style=\"color:green\">Employee</td> <td style=\"color:green\">Customer</td> <td style=\"color:green\">ID Customer</td></tr></thead>";


     foreach ($resultlistcusemp as $k) {
     $listecusemp .= "<tr><td>".$k["nomemploi"]."".$k["nomemploi2"]."</td> <td>".$k["nomclien"]." ".$k['nomcl2'] ." </td><td>".$k['idclient']."</td></tr>" ;
    }

     $listecusemp .="</table>



     ";





    $ret= "
      <style>
                  table {
            border-collapse: collapse;
        }

        table, th, td {
            border: 1px solid ;
            padding: 5px;
            width: 400px;
        }
        select {
            width: 288px!important;
            text-align: center!important;
        }
        select:invalid { color: gray; }

    </style>
        <script>
                     $(document).ready(function() {
            $('#example').DataTable();
        } );

        function linkcustomerbyid(id,msg){


                    document.getElementById(\"linkbyid\").submit();

        }           
    </script>
    <fieldset style = \" width: 800px;\">
        <div align=\"center\" style=\"margin-bottom:20px;\">
        <h3>".$this->l('Link a customer to a employee.')."</h3>
            <form action=\"index.php?tab=personalsalestab$verps&token={$_GET['token']}\" method=\"post\" id=\"linkbyid\" name=\"linkbyid\">

            <strong>".$this->l('Select customer :')."<br/></strong><br>
            ".$selectCustomer."<br/><br/>

            <strong>".$this->l('Select employee:')."<br/></strong><br>
            ".$selectemp."<br/><br/>

            <img src=\"../modules/personalsalesmen/save.png\" onClick=\"linkcustomerbyid(document.getElementById('idcus'),'".$this->l('Are you sure you want link this customer:')." #"."'+document.getElementById('idcus').value+'"." ".$this->l('to this employee: ')." #"."'+document.getElementById('idemp').value+'"."');\" style=\"cursor:pointer;\" >
            <br> <br> <br> <br>
            <strong>".$this->l('Delete all of the linked employees of a customer.')."<br/></strong>
            <br> 

            ".$selectCustomerdel."<br/><br/>

            <img src=\"../modules/personalsalesmen/delete.gif\" onClick=\"linkcustomerbyid(document.getElementById('delcus'),'".$this->l('Are you sure you want delete this customers links:')." #"."'+document.getElementById('delcus').value+'"."');\" style=\"cursor:pointer;\" >            
            </form>
        </div>
        <br>
        <br>
        <br>
        <center>
            <h3>List of linked customers/employees .</h3>
           ".$listecusemp."
           </center>
    </fieldset>
    ";

    if ($return==0){
        echo $ret;
    } else {
        return $ret;
    }
}   
php smarty prestashop jquery-select2 prestashop-1.6
1个回答
0
投票

如果您正在使用管理控制器,那么使用setMedia()函数中的addJQueryPlugin添加select2.js

public function setMedia()
{
    parent::setMedia();
    $this->context->controller->addJQueryPlugin('select2');
}

在下面的示例中,我在select字段上应用了select2

 <select name="country[]" multiple id="country_select">
      <option value="us">US</option>
      <option value="france">France</option>
      <option value="italy">Italy</option>
      <option value="india">India</option>
 </select>

在JS文件中添加以下代码以应用select2:

$(document).ready(function(){
   $('#country_select').select2({
        placeholder: 'select',
        formatNoMatches: 'no match found',
        allowClear: true
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.