在prestashop模块中使用select2

时间:2016-08-29 18:24:26

标签: php smarty prestashop jquery-select2 prestashop-1.6

我想在我自定义的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:good

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

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

所有商店的空白页

- 尝试与AdminModulesPositionController.php / modulePosition.js相同:

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

同样的问题。

事实上,我是一个糟糕的开发者,所以我不知道prestashop机器是如何工作的,我也找不到做一个简单的php事情的方法(我在我的&使用select2) #34;简单&#34; php网站)在这个复杂的系统中..

这是我尝试使用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;
    }
}   

1 个答案:

答案 0 :(得分:0)

如果您正在使用管理控制器,则使用 setMedia()函数

中的 addJQueryPlugin 添加 select2.js >
public function setMedia()
{
    parent::setMedia();
    $this->context->controller->addJQueryPlugin('select2');
}

在下面的示例中,我在选择字段上应用了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
    });
});