Yii2:使用 Pjax 更新網(wǎng)格視圖
2023-05-21
php問題
html5模板網(wǎng)
Yii2: Updating Grid-view using Pjax(Yii2:使用 Pjax 更新網(wǎng)格視圖)
本文介紹了Yii2:使用 Pjax 更新網(wǎng)格視圖的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時(shí)送ChatGPT賬號(hào)..
遵循此 Wiki Yii 2.0:ActiveForm 上的 Pjax和 GridView - Yii2
我曾嘗試使用我的 gridview 在不重新加載頁面的情況下在 Ajax 上進(jìn)行更新,但未能成功.
我的_form.php代碼
registerJs('$("文檔").ready(function(){$("#new_medicine").on("pjax:end", function() {$.pjax.reload({容器:"#medicine"});//重新加載GridView});});');?><?php使用 yiihelpersHtml;使用 yiiwidgetsActiveForm;使用kartikgridGridView;//使用yiigridGridview;使用 yiidataActiveDataProvider;/* @var $this yiiwebView *//* @var $model appmodelsMedicine *//* @var $form yiiwidgetsActiveForm */?><!-- <div class="row"><div class="col-lg-6 col-lg-offset-3">--><div class="medicine-form"><?php yiiwidgetsPjax::begin(['id' => 'new_medicine']) ?><?php $form = ActiveForm::begin(['options' => ['data-pjax' => true ]]);?><?= $form->field($model, 'medicine_id')->textInput(['maxlength' => 10]) ?><?= $form->field($model, 'medicine_name')->textInput(['maxlength' => 50]) ?><div class="form-group"><?= Html::submitButton($model->isNewRecord ?'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?><?= Html::submitButton($model->isNewRecord ? 'Save & New' : '',$option=['class' => $model->isNewRecord ? 'btn btn-success' :'btn btn-primary','name'=>'save_and_new']) ?>
<?php ActiveForm::end();?><?php yiiwidgetsPjax::end() ?>
控制器中的代碼
公共函數(shù)actionIndex(){$model = new Medicine();if ($model->load(Yii::$app->request->post()) && $model->save()){$model = new Medicine();//重置模型}$searchModel = new MedicineSearch();$dataProvider = $searchModel->search(Yii::$app->request->queryParams);返回 $this->render('index', ['搜索模型' =>$搜索模型,'數(shù)據(jù)提供者' =>$數(shù)據(jù)提供者,'模型' =>$模型,]);}
index.php 中的代碼
title = '藥物';$this->params['breadcrumbs'][] = $this->title;?><div class="medicine-index"><h1><?= Html::encode($this->title) ?></h1><?php//echo $this->render('_search', ['model' => $searchModel]);?><p><?= Html::a('Create Medicine', ['create'], ['class' => 'btn btn-success']) ?></p><?php yiiwidgetsPjax::begin(['id' => 'medicine']);?><?= GridView::widget(['數(shù)據(jù)提供者' =>$數(shù)據(jù)提供者,'過濾器模型' =>$搜索模型,'列' =>[['類' =>'yiigridSerialColumn'],'ID','medicine_id','藥物名稱',['類' =>'yiigridActionColumn'],],]);?><?php yiiwidgetsPjax::end();?>
我想我已經(jīng)非常仔細(xì)地遵循了說明,但肯??定我遺漏了一些東西,因?yàn)榫W(wǎng)格視圖沒有顯示在沒有重新加載頁面的情況下添加的新記錄.
任何幫助將不勝感激.謝謝.
解決方案
嘗試解釋如何將其作為一個(gè)小部件;這是一個(gè)通用的解決方案,如有問題請(qǐng)聯(lián)系我:
控制器(@your-alias/controllers/yourController):
<代碼>...公共函數(shù) actionManage($param=''){$model = new YourModel();if (Yii::$app->request->isPjax && $model->load(Yii::$app->request->post()) && $model->;保存()){$model = new YourModel();//重置模型}$model->paramId = $param;$queryParams = Yii::$app->request->getQueryParams();$queryParams['YourModelSearch']['param'] = $param;$searchModel = new YourModelSearch();$dataProvider = $searchModel->search($queryParams);return $this->renderAjax('@your-alias/widgets/views/index', ['搜索模型' =>$搜索模型,'數(shù)據(jù)提供者' =>$數(shù)據(jù)提供者,'模型' =>$模型,]);} ... 小部件(@your-alias/widgets/)[表單,視圖]:
_form:
use yiihelpersHtml;使用 yiiwidgetsActiveForm;使用 yiiwidgetsPjax;/*** @var yiiwebView $this* @var yourModule/models/YourModel $model* @var yiiwidgetsActiveForm $form*/?><?php$js = <<<JS//獲取表單id并設(shè)置事件$('form#{$model->formName()}').on('beforeSubmit', function(e) {var $form = $(this);//在這里做什么,看到參數(shù) $form?是表單的 jQuery 元素console.log($form);console.log("MODEL CODE = " + $("#yourmodel-code").val());}).on('提交', 函數(shù)(e){e.preventDefault();});JS;//$this->registerJs($js);$this->registerJs('$("#new-your-model").on("pjax:end", function() {commonLib.divAction("#div_new_model", "隱藏");//隱藏表單$.pjax.reload({容器:"#models"});//重新加載GridView});', yiiwebView::POS_READY);?><div class="model-form"><?php Pjax::begin(['id' => 'new-model', 'timeout' => false, 'enablePushState' => false]) ?><?php $form = ActiveForm::begin(['id' =>$model->formName(),//'方法' =>'郵政','動(dòng)作' =>['/module/controller/manage?param='.$model->code],'選項(xiàng)' =>['數(shù)據(jù)-pjax' =>真的 ],//'布局' =>'默認(rèn)',]);?><?= $form->field($model, 'code')->textInput(['maxlength' => 255]) ?>...<div class="form-group"><?= Html::submitButton($model->isNewRecord ?'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
<?php ActiveForm::end();?><?php yiiwidgetsPjax::end() ?>
索引視圖(網(wǎng)格視圖):
use yiihelpersHtml;使用 yiigridGridView;使用 yiiwidgetsPjax;/*** @var yiiwebView $this* @var yiidataActiveDataProvider $dataProvider* @var yourModulemodelssearchYourModelSearch $searchModel*/?><div class="model-index"><!--h1><!--?= Html::encode($this->title) ?></h1--><?php//echo $this->render('_search', ['model' => $searchModel]);?><p><?= Html::button(Yii::t('bp', 'Add ...'), ['類' =>'btn btn-成功','onclick'=>'js:commonLib.divAction("#div_new_model", "show")'])?></p><div id="div_new_model" style="display:none"><?= Html::button(Yii::t('common', 'Cancel'), ['類' =>'btn btn-成功','onclick'=>'js:commonLib.divAction("#div_new_model", "隱藏")'])?><!-- 渲染創(chuàng)建表單--><?= $this->render('_formModel', ['模型' =>$模型,]) ?>
<?php Pjax::begin(['id' => 'models', 'timeout' => false, 'enablePushState' => false]) ?><?= GridView::widget(['數(shù)據(jù)提供者' =>$數(shù)據(jù)提供者,'過濾器模型' =>$搜索模型,'列' =>[['類' =>'yiigridSerialColumn'],...['類' =>'yiigridActionColumn'],],]);?><?php Pjax::end() ?>
小部件調(diào)用(在視圖中):
echo @your-aliaswidgetsYourWidget::widget(['參數(shù)' =>$model->param,]);
Following this Wiki Yii 2.0: Pjax on ActiveForm and GridView - Yii2
I have tried to use my gridview to update on Ajax without page-reload, but couldn't succeed.
code of my _form.php
<?php
$this->registerJs(
'$("document").ready(function(){
$("#new_medicine").on("pjax:end", function() {
$.pjax.reload({container:"#medicine"}); //Reload GridView
});
});'
);
?>
<?php
use yiihelpersHtml;
use yiiwidgetsActiveForm;
use kartikgridGridView;
//use yiigridGridview;
use yiidataActiveDataProvider;
/* @var $this yiiwebView */
/* @var $model appmodelsMedicine */
/* @var $form yiiwidgetsActiveForm */
?>
<!-- <div class="row">
<div class="col-lg-6 col-lg-offset-3"> -->
<div class="medicine-form">
<?php yiiwidgetsPjax::begin(['id' => 'new_medicine']) ?>
<?php $form = ActiveForm::begin(['options' => ['data-pjax' => true ]]); ?>
<?= $form->field($model, 'medicine_id')->textInput(['maxlength' => 10]) ?>
<?= $form->field($model, 'medicine_name')->textInput(['maxlength' => 50]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
<?= Html::submitButton($model->isNewRecord ? 'Save & New' : '',$option=['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary','name'=>'save_and_new']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php yiiwidgetsPjax::end() ?>
</div>
Code in my controller
public function actionIndex()
{
$model = new Medicine();
if ($model->load(Yii::$app->request->post()) && $model->save())
{
$model = new Medicine(); //reset model
}
$searchModel = new MedicineSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
code in index.php
<?php
use yiihelpersHtml;
use yiigridGridView;
/* @var $this yiiwebView */
/* @var $searchModel appmodelsMedicineSearch */
/* @var $dataProvider yiidataActiveDataProvider */
$this->title = 'Medicines';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="medicine-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Medicine', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?php yiiwidgetsPjax::begin(['id' => 'medicine']); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yiigridSerialColumn'],
'id',
'medicine_id',
'medicine_name',
['class' => 'yiigridActionColumn'],
],
]); ?>
<?php yiiwidgetsPjax::end(); ?>
</div>
I think I have followed the instructions very carefully, but surely I am missing something as the grid-view is not showing the new records added without page-reload.
Any help will be greatly appreciated.
Thanks.
解決方案
try to explain how to do it as a widget; it's a generic solution, so contact me in case of troubles:
Controller (@your-alias/controllers/yourController):
...
public function actionManage($param=''){
$model = new YourModel();
if (Yii::$app->request->isPjax && $model->load(Yii::$app->request->post()) && $model->save())
{
$model = new YourModel(); //reset model
}
$model->paramId = $param;
$queryParams = Yii::$app->request->getQueryParams();
$queryParams['YourModelSearch']['param'] = $param;
$searchModel = new YourModelSearch();
$dataProvider = $searchModel->search($queryParams);
return $this->renderAjax('@your-alias/widgets/views/index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'model' => $model,
]);
}...
widgets (@your-alias/widgets/) [form, view]:
_form:
use yiihelpersHtml;
use yiiwidgetsActiveForm;
use yiiwidgetsPjax;
/**
* @var yiiwebView $this
* @var yourModule/models/YourModel $model
* @var yiiwidgetsActiveForm $form
*/
?>
<?php
$js = <<<JS
// get the form id and set the event
$('form#{$model->formName()}').on('beforeSubmit', function(e) {
var $form = $(this);
// do whatever here, see the parameter $form? is a jQuery Element to your form
console.log($form);
console.log("MODEL CODE = " + $("#yourmodel-code").val());
}).on('submit', function(e){
e.preventDefault();
});
JS;
//$this->registerJs($js);
$this->registerJs(
'$("#new-your-model").on("pjax:end", function() {
commonLib.divAction("#div_new_model", "hide"); //hide form
$.pjax.reload({container:"#models"}); //Reload GridView
});', yiiwebView::POS_READY
);
?>
<div class="model-form">
<?php Pjax::begin(['id' => 'new-model', 'timeout' => false, 'enablePushState' => false]) ?>
<?php $form = ActiveForm::begin([
'id' => $model->formName(),
//'method' => 'post',
'action' => ['/module/controller/manage?param='.$model->code],
'options' => ['data-pjax' => true ],
//'layout' => 'default',
]); ?>
<?= $form->field($model, 'code')->textInput(['maxlength' => 255]) ?>
...
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php yiiwidgetsPjax::end() ?>
</div>
index view (grid view):
use yiihelpersHtml;
use yiigridGridView;
use yiiwidgetsPjax;
/**
* @var yiiwebView $this
* @var yiidataActiveDataProvider $dataProvider
* @var yourModulemodelssearchYourModelSearch $searchModel
*/
?>
<div class="model-index">
<!--h1><!--?= Html::encode($this->title) ?></h1-->
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::button(Yii::t('bp', 'Add ...'), [
'class' => 'btn btn-success',
'onclick'=>'js:commonLib.divAction("#div_new_model", "show")'
])?>
</p>
<div id="div_new_model" style="display:none">
<?= Html::button(Yii::t('common', 'Cancel'), [
'class' => 'btn btn-success',
'onclick'=>'js:commonLib.divAction("#div_new_model", "hide")'
])?>
<!-- Render create form -->
<?= $this->render('_formModel', [
'model' => $model,
]) ?>
</div>
<?php Pjax::begin(['id' => 'models', 'timeout' => false, 'enablePushState' => false]) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yiigridSerialColumn'],
...
['class' => 'yiigridActionColumn'],
],
]); ?>
<?php Pjax::end() ?>
</div>
widget call (in view):
echo @your-aliaswidgetsYourWidget::widget([
'param' => $model->param,]);
這篇關(guān)于Yii2:使用 Pjax 更新網(wǎng)格視圖的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!
相關(guān)文檔推薦
enable SOAP on PHP(在 PHP 上啟用 SOAP)
Get received XML from PHP SOAP Server(從 PHP SOAP 服務(wù)器獲取接收到的 XML)
not a valid AllXsd value(不是有效的 AllXsd 值)
PHP SoapClient: SoapFault exception Could not connect to host(PHP SoapClient:SoapFault 異常無法連接到主機(jī))
Implementation of P_SHA1 algorithm in PHP(PHP中P_SHA1算法的實(shí)現(xiàn))
Sending a byte array from PHP to WCF(將字節(jié)數(shù)組從 PHP 發(fā)送到 WCF)
主站蜘蛛池模板:
中山市派格家具有限公司【官网】 |
Boden齿轮油泵-ketai齿轮泵-yuken油研-无锡新立液压有限公司
|
杭州厂房降温,车间降温设备,车间通风降温,厂房降温方案,杭州嘉友实业爽风品牌
|
北京中创汇安科贸有限公司 |
周口市风机厂,周鼓风机,河南省周口市风机厂
|
天津试验仪器-电液伺服万能材料试验机,恒温恒湿标准养护箱,水泥恒应力压力试验机-天津鑫高伟业科技有限公司
|
对辊破碎机-液压双辊式,强力双齿辊,四辊破碎机价格_巩义市金联机械设备生产厂家
|
篮球架_乒乓球台_足球门_校园_竞技体育器材_厂家_价格-沧州浩然体育器材有限公司
|
淘气堡_室内儿童乐园_户外无动力儿童游乐设备-高乐迪(北京)
|
POS机办理_个人pos机免费领取-银联pos机申请首页
|
全自动真空上料机_粉末真空上料机_气动真空上料机-南京奥威环保科技设备有限公司
|
世界箱包品牌十大排名,女包小众轻奢品牌推荐200元左右,男包十大奢侈品牌排行榜双肩,学生拉杆箱什么品牌好质量好 - Gouwu3.com
|
预制舱-电力集装箱预制舱-模块化预制舱生产厂家-腾达电器设备
|
食药成分检测_调料配方还原_洗涤剂化学成分分析_饲料_百检信息科技有限公司
|
长沙广告公司_制作,长沙喷绘_发光字_招牌制作_长沙泓润广告官网
长城人品牌官网
|
聚氨酯保温钢管_聚氨酯直埋保温管道_聚氨酯发泡保温管厂家-沧州万荣防腐保温管道有限公司
|
威实软件_软件定制开发_OA_OA办公系统_OA系统_办公自动化软件
|
蔡司三坐标-影像测量机-3D扫描仪-蔡司显微镜-扫描电镜-工业CT-ZEISS授权代理商三本工业测量
|
武汉高温老化房,恒温恒湿试验箱,冷热冲击试验箱-武汉安德信检测设备有限公司
|
脱硝喷枪-氨水喷枪-尿素喷枪-河北思凯淋环保科技有限公司
|
非标压力容器_碳钢储罐_不锈钢_搪玻璃反应釜厂家-山东首丰智能环保装备有限公司
|
河南档案架,档案密集架,手动密集架,河南密集架批发/报价
|
专业广州网站建设,微信小程序开发,一物一码和NFC应用开发、物联网、外贸商城、定制系统和APP开发【致茂网络】
|
阿米巴企业经营-阿米巴咨询管理-阿米巴企业培训-广东键锋企业管理咨询有限公司
|
铝合金线槽_铝型材加工_空调挡水板厂家-江阴炜福金属制品有限公司
|
加中寰球移民官网-美国移民公司,移民机构,移民中介,移民咨询,投资移民
|
高压分散机(高压细胞破碎仪)百科-北京天恩瀚拓
|
台式恒温摇床价格_大容量恒温摇床厂家-上海量壹科学仪器有限公司
|
液压升降平台_剪叉式液压/导轨式升降机_传菜机定做「宁波日腾升降机厂家」
|
石膏基自流平砂浆厂家-高强石膏基保温隔声自流平-轻质抹灰石膏粉砂浆批发-永康市汇利建设有限公司
|
香港新时代国际美容美发化妆美甲培训学校-26年培训经验,值得信赖!
|
◆大型吹塑加工|吹塑加工|吹塑代加工|吹塑加工厂|吹塑设备|滚塑加工|滚塑代加工-莱力奇塑业有限公司
|
一级建造师培训_一建培训机构_中建云筑建造师培训网校
|
过滤器_自清洗过滤器_气体过滤器_苏州华凯过滤技术有限公司
|
炭黑吸油计_测试仪,单颗粒子硬度仪_ASTM标准炭黑自销-上海贺纳斯仪器仪表有限公司(HITEC中国办事处)
|
包塑丝_高铁绑丝_地暖绑丝_涂塑丝_塑料皮铁丝_河北创筹金属丝网制品有限公司
|
分光色差仪,测色仪,反透射灯箱,爱色丽分光光度仪,美能达色差仪维修_苏州欣美和仪器有限公司
|
整车VOC采样环境舱-甲醛VOC预处理舱-多舱法VOC检测环境仓-上海科绿特科技仪器有限公司
|
东莞螺丝|东莞螺丝厂|东莞不锈钢螺丝|东莞组合螺丝|东莞精密螺丝厂家-东莞利浩五金专业紧固件厂家
|
江苏皓越真空设备有限公司 |
SF6环境监测系统-接地环流在线监测装置-瑟恩实业
|