- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我已经试过了:yii2 dependent autocomplete widget
但我不知道为什么它不起作用。这是我的带有脚本的html:
<?= $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.');}
'])?>
自动完成:
<?= $form->field($model, 'name')->widget(\yii\jui\AutoComplete::classname(), [
'options' => ['class' => 'form-control', 'placeholder' => 'Enter Name/ID'],
'clientOptions' => [
'source' => $s_data,
'autoFill' => true,
'minLength' => '1',
'select' => new yii\web\JsExpression("function( event, ui ) {
$('#libraryborrowtransaction-lbt_holder_id').val(ui.item.id);
}")
],
])?>
我想根据下拉列表值更改自动完成源,如果 S 则加载 $s_data 否则加载 $e_data。对此有任何帮助。谢谢。
这是我的数据,
$s_data = (new \yii\db\Query())
->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();
和,
$e_data = (new \yii\db\Query())
->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();
最佳答案
好吧,我已将您的代码片段添加到我的测试 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”的引号符号应用了一些转义,并且您在浏览器中的代码看起来像 "S"
。
接下来,jui 自动完成插件向 jquery 原型(prototype)添加一个名为 "autocomplete"
但不是 "autoComplete"
的属性。至于js是区分大小写的,这两个名字看起来不一样。
所以我的解决方案是将所有 js 从 onchange
属性移动到单独的 js 脚本,如下所示(出于测试目的,您可以将其添加到您的 yii View 文件中,您可以在其中使用提供的代码你的问题)
<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>
并将这个新事件处理程序的名称粘贴到您的下拉列表的 onchange 属性中,如下所示:
['onChange' => 'holderTypeChangeHandler']
更新:---------
由于 Yii2 Autocomplete 基于 JQuery UI 自动完成小部件和 Yii2 Autocomplete clientOptions
包含 settings for JUI autocomplete widget ,那么我们可以引用 JUI API 文档对 source
option 的解释。 .如您所见,此选项可以是字符串(在这种情况下,它用作 JSON 数据的 URI)、函数或 js 数据数组或 js 对象数组。
在您的问题中,您使用 \yii\db\Query
在 method all()
的帮助下从 db 获取一些数据,它返回一个数据数组。因此,最后您需要将使用 \yii\db\Query->all
获得的数据数组转换为 js 对象数组。为此,请使用 php json functions ,特别是您的情况,您需要使用 json_encode()
函数:
// Let's say this is a result of your query to db with use of `\yii\db\Query->...->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 ?> });
$e_data
也是一样。请注意,您使用 PHP 从 db 获取数据,但将其与 JS 一起使用,因此需要将 php 数组转换为字符串表示 js 对象数组,并且您可以使用 json_encode
进行此转换.
关于php - Yii2 DropDownList Onchange更改自动完成小部件 "source"属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31425942/
当我构建 2 个下拉菜单并从数据库中填充它们时,当从第一个下拉菜单中选取一个值时,会创建第二个下拉菜单。但是,当我在第二个选项中选择一个选项时,我的 ajax 正在执行。但是当我在第二个下拉列表中只有
所以我有一个用户脚本来解析页面并创建一个选择框并用选项填充它。 现在我想在用户更改选择框上的选定索引时调用脚本。不幸的是,用户脚本看起来只运行一次,并且无法读取选择上的 future 更改。 我试过:
我有一个文件选择器,当点击按钮时,文件选择器出现: 现在我想使用另一个按钮来执行此操作,触发上面的输入: { var element = document.getElementById('
有两种方法可以在 React 应用程序中获取输入更改。 一种是使用 另一个是 ... 什么时候应该使用第一个,什么时候应该使用另一个。 最佳答案 之所以有两种方式,是因为方式不止这两种。您
当子组件的 select 元素更改时,我尝试做两件事 - 更新子状态(用于显示更多字段以及其他特定于子项的决策等), 调用传入 props 的父处理程序。 只有第一个可以工作,如何添加第二个?正确的方
我创建了一个显示用户输入结果的自定义搜索应用程序。在应用程序中,我为用户提供了一种控制显示的结果数量的方法。 HTML 10 25 50 100 jQuery $(
我有一个带有多个选项的选择元素,如下所示: .. .... .. 我正在使用 django (不知道这是否真的很重要..)并且在 select 标记的 onChange 事件中我应
我有一个使用的 xsl 页面 在 onchange 事件中 onchange="updateDropdown({$pos});someElement_{$pos}.value = {$pos}";
我试图根据条件清除 onChange 事件中选择的值。它是否已经存在于选定的列表中。但是我无法使用 清除该值$select[name][0].selectize.clear(); 来自 onChang
我正忙于自学 FXML。我通过关注 this 来做到这一点示例。 这是一个简单的文本编辑器。但是,在本教程中,一切都是 Java 代码。 我自己正在使用 FXML 来分离逻辑 View 。 我目前
我在jsp页面中有如下代码... 如果我在 a4j 中使用 event="onchange" 它不会调用我的 generatePrescriptionQuantity 但如果我使用 event
我的页面上有一堆输入元素。每个输入元素都有一个 onchange 事件,该事件会触发对服务器的 ajax 调用。我想禁用所有这些 onchanges,然后对元素进行一些编程更改(重置它们),然后重新启
我有这个代码: $(document).ready(function(){ var country; $('#selectCountry').on('change', function (e)
我有这段代码: 但它并没有按照我想要的方式运行。当我离开盒子时它会运行该函数,我想每次该输入字段的内容发生变化时都执行该操作,我该怎么做? (我正在尝试为用户输入创建一个建议框)。 我也尝试过 jq
我有一个绑定(bind)到更改事件的选择,以便在进行选择时将用户带到新页面。鼠标没问题,但是当我尝试使用键盘的箭头键进行选择时,更改事件会在我按下箭头时立即触发,而不是等待我跳出,所以我只能选择第一个
所以我已经为此工作了大约一天......我有一个表单,其中有一个选择菜单,其选择会触发另一个相关的选择菜单。 此时我要做的是根据第二个选择菜单中的选择填写一个默认值。基本上,如果用户在 Labor 菜
我在 jQuery 中有一个 input text 我想知道是否可以获取该 input text 的值(type=number和 type=text) 在 onchange 发生之前,并且在 onch
简介我在 create-react-app 应用程序中有一个用户输入元素。它允许用户键入数字,或使用箭头向上或向下递增数字。然后输出显示该数字。 问题用户想要达到某个数字,无论他们如何输入数字,都会有
这个问题已经有答案了: val() doesn't trigger change() in jQuery (11 个回答) 已关闭 7 年前。 我有一个选择列表。 Toyota Subaru Fia
所以我的工作场所在一个组件内使用一个 SelectField,该组件在多个地方使用。说得通。现在,当我在当前正在开发的组件中使用该组件时。我创建的 onChange 事件未传递正确完成所需的值。现在,
我是一名优秀的程序员,十分优秀!