我是Yii2和网络编程的新手,也许是我缺乏知识甚至是愚蠢的请求的借口。
我将我的网站划分为4个div容器。在一个容器中(#untenrechts(编辑:lowerright))我使用网格视图,如果我点击“customer_name”(已编辑)我想刷新另一个容器(#obenlinks(编辑:#upperleft))的详细信息视图选择客户(已编辑)而不重新加载整个页面,只是容器。
目前它仅适用于手动重新加载以显示所选客户(已编辑)的选定详细信息。
我在匿名函数的返回值中获取语法时遇到问题。我只是不明白,如何在这里使用onclick事件。
我搜索了很多,我得到的最好的结果是这一个: Yii2: Assigning javascript event to anchor tag in yii2
但那是我的问题。此解决方案不是返回值的一部分。所以我希望这里有些友好的人能帮助我找到正确的方法或理解它。
先谢谢
编辑:我要道歉,因为我发布了关于寻找project_detail的问题,但我需要customer_detail。代码没有太大的区别,但是为了理解我的问题。为了更好地理解,我还将div容器名称更改为英语容器名称。我更新了我的问题。
我现在尝试实施给定的建议并发布(希望)所有相关代码。现在我得到一个#404错误。
可悲的是,我需要更多的帮助来理解这一行:
return Html::a($model->company_name, ['#.'], ['data'=>['customers'=>$model->id],'class' => 'customer_details']);
链接始终尝试调用“/ site /#。”。我尝试了其他地址,但总是得到同样的错误。
_LIST.PHP
<?php
use yii\widgets\ActiveForm;
use yii\grid\GridView;
use yii\widgets\ListView;
use yii\widgets\DetailView;
use yii\widgets\Pjax;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use backend\models\Customers;
use yii\db\Expression;
use yii\helpers\Url;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
$this->registerJs("
$(document).on('click','a.customer_details',function(){
$.ajax({
url:'/customers/view',
data:{id:id},
success:function(data){($'#obenlinks').html(data)},
error:function(xhr,statusCode,errorText){alert(errorText)}
});
});
", yii\web\View::POS_READY);
?>
<div class="row" style="position:relative; left: 30px;">
<div id="upperleft" class="col-md-6" style="background-color:lightgreen; overflow: scroll; width:550px; height:300px; border: 1px solid black">
<?php Pjax::begin(); ?>
<?= DetailView::widget([
'model' => $model2,
'attributes' => [
'id',
'customer_number',
'company_name',
'customer_type',
'firstname',
'lastname',
'customer_since',
'street_name',
'street_number',
'zipcode',
'city',
],
]) ?>
<?php Pjax::end();?>
</div>
<div id="upperright" class="col-md-6" style="background-color:lightblue; overflow: scroll; width:550px; height:300px;border: 1px solid black">
EMPTY DIV CONTAINER
</div>
</div>
<div class="row" style="position:relative; left: 30px;">
<div id="lowerleft" class="col-md-6" style="background-color:lightgrey; overflow: scroll; width:550px; height:300px;border: 1px solid black">
EMPTY DIV CONTAINER
</div>
<div id="lowerright" class="col-md-6" style="background-color:orange; overflow: scroll; width:550px; height:300px;border: 1px solid black">
GRIDVIEW ALL CUSTOMERS
<?php Pjax::begin(); ?> <?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'customer_number',
[
'label' => 'Firmenname',
'attribute' => 'company_name',
'format' => 'raw',
'value' => function ($model) {
if ($model->company_name == "Privat"){
return " ";
}
else {
return Html::a($model->company_name, ['#.'], ['data'=>['customers'=>$model->id],'class' => 'customer_details']);
}
},
],
[
'label' => 'Privatkunde',
'attribute' => 'firstname',
'format' => 'raw',
'value' => function ($model) {
return Html::a($model->firstname . ' ' . $model->lastname, ['customers/view', 'id' => $model->id]);
}
],
[
'label' => 'Projekte',
'attribute' => 'projects.project_name',
'format' => 'raw',
'value' => function ($model) {
$selectproject='<ul id="selectproject">';
foreach ($model->projects as $row) {
$selectproject.= "<li value='$row->id'>".Html::a($row->project_name, ['/projects/view', 'id' => $row->id])."</li>";
}
$selectproject.='</ul>';
return $selectproject;
},
],
],
]); ?>
<?php Pjax::end(); ?>
</div>
</div>
PRODUCTIONSITE.PHP
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\widgets\ActiveForm;
use backend\models\Customers;
use backend\models\Projects;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
/* @var $this yii\web\View */
$this->title = 'Produktivumgebung';
?>
<div>
<?php echo $this->render('/subviews/_customerssearch', ['model' => $searchModel]);
//var_dump ($model->attributes);
?>
<div id="listenboxaussen" style="background-color:blue; position:relative">
<?=$this->render('//site/frames/_list.php', array(
'model' => $model,
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
'query' => $query,
'modelProjects' => $modelProjects,
'searchModelProjects' => $searchModelProjects,
'dataProviderProjects' => $dataProviderProjects,
'queryProjects' => $queryProjects,
'model2' => $model2,
))?>;
</div>
SITECONTROLLER.PHP
<?php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use backend\models\Customers;
use backend\models\Projects;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;
/**
* Site controller
*/
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'actions' => ['logout', 'index','productionsite','start','stop','pause'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
/**
* Displays homepage.
*
* @return string
*/
public function actionIndex()
{
return $this->render('index');
}
public function actionProductionsite($id=0)
{
$this->layout = '/production';
if ($id!=0){
$model2 = Customers::findOne($id);
}else{
$model2 = false;
}
// Get customer data
$model = new Customers;
$searchModel = new \backend\models\CustomersSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$query = Customers::find()->with('projects')->all();
// Get project data
$modelProjects = new Projects;
$searchModelProjects = new \backend\models\ProjectsSearch();
$dataProviderProjects = $searchModelProjects->search(Yii::$app->request->queryParams);
$queryProjects = Projects::find()->with('parent','tasks')->all();
return $this->render('productionsite', [
'searchModel' => $searchModel,
'searchModelProjects' => $searchModelProjects,
'dataProvider' => $dataProvider,
'dataProviderProjects' => $dataProviderProjects,
'query' => $query,
'queryProjects' => $queryProjects,
'model' => $model,
'modelProjects' => $modelProjects,
'model2' => $model2,
]);
}
/**
* Login action.
*
* @return string
*/
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
if ($model->username == '****' or $model->username =='****') {
return $this->render('index', [
'model' => $model,
]);
}
else {
return $this->redirect(['productionsite',
'model' => $model,
]);
}
} else {
return $this->render('login', [
'model' => $model,
]);
}
}
/**
* Logout action.
*
* @return string
*/
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
public function actionDetails()
{
$request = Yii::$app->request;
if ($request->isAjax && $request->isPost) {
$id = $request->post('id');
//add your model code here
$customer = Customers::find()->where(['=', 'id', $id])->one();
return $this->renderAjax('//site/frames/_list', ['model' => $customer]);
}
Yii::$app->end();
}
}
答案 0 :(得分:0)
这是options
的第三个参数,它将以name=>value
对提供的所有选项呈现为您在url
参数{{3}中添加的结果标记的属性}。
应该是
return Html::a(
$model->project_name,
['/projects/view', 'id' => $model->id],
['onclick'=>"YourJavascriptStatement()"]
);
因此,如果您要将onclick
属性添加到锚点,则应将Html::a()
函数的第三个参数作为name=>value
对,并使用属性名称(onclick)和值(javascript函数或语句)。
现在找你需要的问题
HTML
div
1}},其中显示DetailView
。将projektname
中的GridView
列定义更新为以下
[
'label' => 'Projektname',
'attribute' => 'project_name',
'format' => 'raw',
'value' => function ($model) {
return Html::a($model->project_name, '#.' , ['data'=>['project'=>$model->id],'class'=>'project_details']);
},
],
将以下内容添加到视图文件_list.php
的顶部。将网址更新为相关路径
$this->registerJs("
$(document).on('click','a.project_details',function(e){
e.preventDefault();
$.ajax({
url:'/controller/action', //change this to the relevant controller and action names
data:{project_id:id},
success:function(data){$('#yourDetailViewDivId').html(data)},
error:function(xhr,statusCode,errorText){alert(errorText)}
});
});
", yii\web\View::POS_READY);
return $this->renderAjax('viewname')
而不是render $this->render('viewname')
。有关差异,请参阅See HTML::a() API Documentation
。示例动作
public function actionDetails()
{
$request = Yii::$app->request;
if ($request->isAjax && $request->isPost) {
$project_id = $request->post('project_id');
//add your model code here
$project = Projects::find()->where(['=', 'id', $project_id])->one();
return $this->renderAjax('viewname', ['model' => $project]);
}
Yii::$app->end();
}
DetailView
的视图,并显示所选模型的详细信息。应该类似于以下添加表属性
<?=
DetailView::widget([
'model' => $model,
'options' => ['class' => 'table'],
'attributes' => [
//add your model attributes here you want to show
],
])
?>