<small id='t5wBe'></small><noframes id='t5wBe'>

    <legend id='t5wBe'><style id='t5wBe'><dir id='t5wBe'><q id='t5wBe'></q></dir></style></legend>

      <i id='t5wBe'><tr id='t5wBe'><dt id='t5wBe'><q id='t5wBe'><span id='t5wBe'><b id='t5wBe'><form id='t5wBe'><ins id='t5wBe'></ins><ul id='t5wBe'></ul><sub id='t5wBe'></sub></form><legend id='t5wBe'></legend><bdo id='t5wBe'><pre id='t5wBe'><center id='t5wBe'></center></pre></bdo></b><th id='t5wBe'></th></span></q></dt></tr></i><div id='t5wBe'><tfoot id='t5wBe'></tfoot><dl id='t5wBe'><fieldset id='t5wBe'></fieldset></dl></div>
    1. <tfoot id='t5wBe'></tfoot>
        <bdo id='t5wBe'></bdo><ul id='t5wBe'></ul>

      1. Yii2 DropDownList Onchange 更改自动完成小部件“源"属性?

        时间:2023-10-15
        <tfoot id='RIBHJ'></tfoot>
                <tbody id='RIBHJ'></tbody>

              <legend id='RIBHJ'><style id='RIBHJ'><dir id='RIBHJ'><q id='RIBHJ'></q></dir></style></legend>

                  <bdo id='RIBHJ'></bdo><ul id='RIBHJ'></ul>

                  <i id='RIBHJ'><tr id='RIBHJ'><dt id='RIBHJ'><q id='RIBHJ'><span id='RIBHJ'><b id='RIBHJ'><form id='RIBHJ'><ins id='RIBHJ'></ins><ul id='RIBHJ'></ul><sub id='RIBHJ'></sub></form><legend id='RIBHJ'></legend><bdo id='RIBHJ'><pre id='RIBHJ'><center id='RIBHJ'></center></pre></bdo></b><th id='RIBHJ'></th></span></q></dt></tr></i><div id='RIBHJ'><tfoot id='RIBHJ'></tfoot><dl id='RIBHJ'><fieldset id='RIBHJ'></fieldset></dl></div>

                  <small id='RIBHJ'></small><noframes id='RIBHJ'>

                  本文介绍了Yii2 DropDownList Onchange 更改自动完成小部件“源"属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经尝试过这个:yii2 依赖的自动完成小部件

                  但我不知道为什么它不起作用.这是我的 html 脚本:

                  field($model, 'lbt_holder_type')->dropDownList(['prompt' => '--- Select Holder Type ---', 'S' => '学生', 'E' => '员工'],['onChange' =>'JS: var value = (this.value);if(value == "S"){$(#libraryborrowtransaction-name).autoComplete({source: '.$s_data.');}if(value == "E"){$(#libraryborrowtransaction-name).autoComplete({source: '.$e_data.');}'])?>

                  自动完成:

                  field($model, 'name')->widget(yiijuiAutoComplete::classname(), ['选项' =>['类' =>'表单控件', '占位符' =>'输入姓名/ID'],'clientOptions' =>['来源' =>$s_data,'自动填充' =>真的,'minLength' =>'1','选择' =>new yiiwebJsExpression("function( event, ui ) {$('#libraryborrowtransaction-lbt_holder_id').val(ui.item.id);}")],])?>

                  我想根据下拉列表值更改自动完成源,如果 S 则加载 $s_data 否则加载 $e_data.任何帮助.谢谢.

                  这是我的数据,

                  $s_data = (new yiidbQuery())->select(["CONCAT(stu_unique_id,'-',stu_first_name,'',stu_last_name) 作为值","CONCAT(stu_unique_id,'-',stu_first_name,'',stu_last_name) 作为标签","s_info.stu_info_stumaster_stumaster作为 id"])->from('stu_master stu')->join('join','stu_info s_info','s_info.stu_info_id = stu_master_stu_info_id')-> where('is_status = 0')-> 全部();

                  和,

                  $e_data = (new yiidbQuery())->select(["CONCAT(emp_unique_id,'-',emp_first_name,'',emp_last_name) 作为值","info.emp_info_emp_master_id 作为 id"])->from('emp_master emp')->join('join', 'emp_info info', 'info.emp_info_id = emp_info_emp_master_id')-> where('is_status = 0')-> 全部();

                  解决方案

                  好吧,我已经将您的代码片段添加到我的测试 yii2 环境中以测试出了什么问题.所以你的代码有一些问题:

                  <预><代码>['onChange' =>'JS: var value = (this.value);if(value == "S"){$(#libraryborrowtransaction-name).autoComplete({source: '.$s_data.');}if(value == "E"){$(#libraryborrowtransaction-name).autoComplete({source: '.$e_data.');}']

                  首先,我注意到 yii 为您的S"和E"应用了一些转义引号符号,并且您在浏览器中的代码看起来像 &quot;S&quot;.

                  接下来,jui 自动完成插件向 jquery 原型添加一个属性,名称为 "autocomplete" 但不是 "autoComplete".至于js是区分大小写的,所以这两个名字看起来不一样.

                  所以我的解决方案是将所有 js 从 onchange 属性移动到单独的 js 脚本,如下所示(出于测试目的,您可以将它添加到您的 yii 视图文件中,您可以在其中使用提供的代码你的问题)

                  并将这个新事件处理程序的名称粘贴到您的 dropdownList 的 onchange 属性中,如下所示:

                  ['onChange' =>'holderTypeChangeHandler']

                  更新:--------------------

                  由于基于 JQuery UI 自动完成小部件和 Yii2 自动完成的 Yii2 自动完成 clientOptions 包含 JUI 自动完成小部件的设置,然后我们可以参考 JUI API 文档来解释 source 选项.如您所见,此选项可以是字符串(在本例中用作 JSON 数据的 URI)、函数或 js 数据数组或 js 对象数组.

                  在您的问题中,您正在使用 yiidbQuery 在 方法all(),它返回一个数据数组.所以最后你需要将你用 yiidbQuery->all 得到的数据数组转换为 js 对象数组.为此,请使用 php json 函数,特别是对于您需要使用的情况json_encode() 函数:

                  //假设这是您使用 `yiidbQuery->...->all();` 查询 db 的结果$some_array = [[价值"=>值 1",标签" =>"标签 1",身份证"=>1],[价值"=>Val 2",标签" =>标签2",身份证"=>2]]//只需将其转换为 json 字符串$s_data = json_encode($some_array);...//当连接这个 json 字符串作为 Yii 自动完成的源属性值时$('#libraryborrowtransaction-name').autocomplete({source: <?= $s_data ?> });

                  如果您的 $e_data 也是如此.请注意,您使用 PHP 从 db 获取数据,但使用 JS,因此需要将 php 数组转换为字符串表示的 js 对象数组,而这种转换可以使用 json_encode.

                  I have already tried this : yii2 dependent autocomplete widget

                  but i don't know why it's not working. here my html with script:

                  <?= $form->field($model, 'lbt_holder_type')->dropDownList(['prompt' => '--- Select Holder Type ---', 'S' => 'Student', 'E' => 'Employee'], 
                                      ['onChange' => 'JS: var value = (this.value); 
                                                  if(value == "S"){$(#libraryborrowtransaction-name).autoComplete({source: '. $s_data.');}
                                                  if(value == "E"){$(#libraryborrowtransaction-name).autoComplete({source: '. $e_data.');}
                  
                                      '])?>
                  

                  Autocomplete:

                  <?= $form->field($model, 'name')->widget(yiijuiAutoComplete::classname(), [
                                  'options' => ['class' => 'form-control', 'placeholder' => 'Enter Name/ID'],
                                  'clientOptions' => [
                                      'source' => $s_data,
                                      'autoFill' => true,
                                      'minLength' => '1',
                                      'select' => new yiiwebJsExpression("function( event, ui ) {
                                          $('#libraryborrowtransaction-lbt_holder_id').val(ui.item.id);
                                      }")
                                  ],
                              ])?>
                  

                  i want to change autocomplete source according to dropdownlist value, if S then load $s_data else load $e_data. Any help with this. Thanks.

                  Here's my data,

                  $s_data = (new yiidbQuery())
                  ->select(["CONCAT(stu_unique_id,' - ',stu_first_name,' ',stu_last_name) as value","CONCAT(stu_unique_id,' - ',stu_first_name,' ',stu_last_name) as label","s_info.stu_info_stu_master_id as id"])
                  ->from('stu_master stu')
                  ->join('join','stu_info s_info','s_info.stu_info_id = stu_master_stu_info_id')
                  ->where('is_status = 0')
                  ->all();
                  

                  and,

                  $e_data = (new yiidbQuery())
                      ->select(["CONCAT(emp_unique_id, ' - ',emp_first_name,' ',emp_last_name) as value","info.emp_info_emp_master_id as id"])
                      ->from('emp_master emp')
                      ->join('join', 'emp_info info', 'info.emp_info_id = emp_info_emp_master_id')
                      ->where('is_status = 0')        
                      ->all();
                  

                  解决方案

                  Well, I've added your code snippets to my test yii2 environment to test what's wrong. So there are some problems with your code:

                  [
                     'onChange' => 
                         'JS: var value = (this.value); 
                  
                         if(value == "S"){$(#libraryborrowtransaction-name).
                             autoComplete({source: '. $s_data.');}
                  
                         if(value == "E"){$(#libraryborrowtransaction-name).
                             autoComplete({source: '. $e_data.');}
                  
                  ']
                  

                  First of all I noticed what yii apply some escaping for quotation mark symbols for your "S" and "E", and your code in browser looks like &quot;S&quot;.

                  Next, jui autocomplete plugin adds a property to jquery prototype with name "autocomplete" but not "autoComplete". As for as js is case sensitive, this two names looks different for it.

                  So my solution was to move all js from onchange property to separate js script, as it shown below (for testing purposes you can add it right in your yii view file where you use code provided in your question)

                  <script>
                      function holderTypeChangeHandler(ev) {
                          var value = (this.value); 
                          if(value == 'S'){
                              $('#libraryborrowtransaction-name').autocomplete({source: ' . $s_data . '});
                          }
                          if(value == 'E'){
                              $('#libraryborrowtransaction-name').autocomplete({source: ' . $e_data . '});
                          } 
                      }
                      window.onload = function(){
                          $('#libraryborrowtransaction-lbt_holder_type').on('change', holderTypeChangeHandler);
                  
                      };
                  </script>
                  

                  And paste name of this new event handler to your dropdownList's onchange property like this:

                  ['onChange' => 'holderTypeChangeHandler']
                  

                  UPDATE: ---------------------

                  Since Yii2 Autocomplete based on JQuery UI autocomplete widget and Yii2 Autocomplete clientOptions contains settings for JUI autocomplete widget, then we can refer to JUI API docs for explanation of the source option. As you can see, this option can be a string (in this case it used as URI to JSON data), a function or an js array of data or js array of objects.

                  In your question you are using yiidbQuery to fetch some data from db with help of method all(), which returns an array of data. So finally you need to convert the array of data which you get with yiidbQuery->all to js array of objects. To do it, use php json functions, particulary for your case you need to use json_encode() function:

                  // Let's say this is a result of your query to db with use of `yiidbQuery->...->all();`
                  $some_array = [                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
                      [                                                                                                                                                    
                          "value" => "Val 1",                                                                                                                                
                          "label" => "Label 1",
                          "id" => 1
                      ],
                      [
                          "value" => "Val 2",
                          "label" => "Label 2",
                          "id" => 2
                      ]
                  ]
                  
                  // Just convert it to json string
                  $s_data = json_encode($some_array);
                  ...
                  // When concat this json string as a value of source attribute for Yii Autocomplete
                  $('#libraryborrowtransaction-name').autocomplete({source: <?= $s_data ?> });
                  

                  The same if for your $e_data. Just pay attention, your get your data from db with PHP, but use it with JS, so php array needs to be converted to a string presentation js array of objects, and this conversion you can do with json_encode.

                  这篇关于Yii2 DropDownList Onchange 更改自动完成小部件“源"属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 yii2 Gridview Widget 中渲染 html 下一篇:在 Yii 2.0 中添加 jquery

                  相关文章

                  <tfoot id='7lsbU'></tfoot>

                  1. <legend id='7lsbU'><style id='7lsbU'><dir id='7lsbU'><q id='7lsbU'></q></dir></style></legend>
                      <i id='7lsbU'><tr id='7lsbU'><dt id='7lsbU'><q id='7lsbU'><span id='7lsbU'><b id='7lsbU'><form id='7lsbU'><ins id='7lsbU'></ins><ul id='7lsbU'></ul><sub id='7lsbU'></sub></form><legend id='7lsbU'></legend><bdo id='7lsbU'><pre id='7lsbU'><center id='7lsbU'></center></pre></bdo></b><th id='7lsbU'></th></span></q></dt></tr></i><div id='7lsbU'><tfoot id='7lsbU'></tfoot><dl id='7lsbU'><fieldset id='7lsbU'></fieldset></dl></div>

                      <small id='7lsbU'></small><noframes id='7lsbU'>

                      • <bdo id='7lsbU'></bdo><ul id='7lsbU'></ul>