如何在Yii 2.0中将jQuery添加到我的页面?
在Yii 1.x中你可以使用:
Yii::app()->clientScript->registerCoreScript('jquery');
Run Code Online (Sandbox Code Playgroud)
我已经尝试用自己的方法覆盖View类,并尝试在那里注册jQuery,但它没有显示在我的html页面中:
namespace frontend\components;
/**
* This is the base view object, it extends the yii\web\View so you can add custom view stuff here.
*/
class BaseView extends \yii\web\View {
public function init()
{
parent::init();
\yii\web\JqueryAsset::register($this);
}
}
Run Code Online (Sandbox Code Playgroud) 在Yii 1.x中,您可以使用CWebLogRoute类在浏览器中记录所有查询和内容.这个选项是否已经可用于Yii 2.0?我只在框架中看到FileTarget,DbTarget和EmailTarget类.
我有一个列airline_id,它是表路由中的varchar,现在我想将该值复制到具有int类型的列airline_id_int中.虽然我无法正确理解语法.
这就是我所拥有的:
UPDATE route SET airline_id_int = CAST(airline_id, int);
Run Code Online (Sandbox Code Playgroud) 我有两个下拉选择下拉列表:一个用于区域,一个用于所选区域中的城市.结果由AJAX加载,在我的响应中,我得到了JSON数组中的所有城市:
{
1709: "Geertruidenberg",
1710: "Netersel",
1711: "Macharen",
1712: "Beers",
1713: "Hank",
1714: "Oudemolen",
1715: "Nistelrode"
}
Run Code Online (Sandbox Code Playgroud)
我正在使用这个小插件来加载选择下拉列表中的数据:
(function($, window) {
$.fn.replaceOptions = function(options) {
var self, $option;
this.empty();
self = this;
$.each(options, function(index, option) {
$option = $("<option></option>")
.attr("value", index)
.text(option);
self.append($option);
});
};
})(jQuery, window);
Run Code Online (Sandbox Code Playgroud)
而这段javascript做的AJAX请求:
$('select#Profile_regionId').change(function() {
$.post('/ajax/getcities', {regionid: $(this).val()}, function(data){
//console.log(data.cities);
$("select#Profile_cityId").replaceOptions(data.cities);
}, 'json');
});
Run Code Online (Sandbox Code Playgroud)
所有工作完全正常,除了城市下拉列表自动按JSON数组键排序.我尝试使用sort()方法,但它不起作用,因为它是一个Object而不是一个数组.然后我尝试创建它的数组:
var values = [];
$.each(data.cities, function(index,value)) {
values[index] = value;
}
Run Code Online (Sandbox Code Playgroud)
但由于某些原因,在下拉列表填充自身高达1到第一个找到城市的ID(数组的键),我不知道它为什么这样做(阵列本身看起来不错).
我如何对事物进行排序,以便我的城市按照字母顺序排列在下拉列表中?
我正在开发一个项目,其中多个网站将从一个 Yii 安装中运行。但每个网站都有自己的数据库,因此数据库连接必须是动态的。
我所做的,我创建了一个 BeginRequestBehavior ,它将在“onBeginRequest”启动。在这里,我将检查哪个 url 已被调用,并确定匹配的数据库(尚未在我的代码中)并创建(或覆盖)“db”组件。
<?php
class BeginRequestBehavior extends CBehavior
{
/**
* Attaches the behavior object to the component.
* @param CComponent the component that this behavior is to be attached to
* @return void
*/
public function attach($owner)
{
$owner->attachEventHandler('onBeginRequest', array($this, 'switchDatabase'));
}
/**
* Change database based on current url, each website has his own database.
* Config component 'db' is overwritten with this value
* @param $event event that is called
* @return …Run Code Online (Sandbox Code Playgroud)