簡體   English   中英

在prestashop模塊中使用select2

[英]using select2 in a prestashop module

我想在我自定義的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)然后,我嘗試通過幾種方式初始化選擇:-就像文檔在模塊的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;
    }
}   

如果你正在使用的管理控制器隨后,添加使用addJQueryPluginselect2.jssetMedia()函數

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
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM