我目前正在尝试在yii2中创建的新闻模块有问题。
我有多个与动物产品或植物产品有关的领域。与动物无关的项目将不会显示,反之亦然。
现在,它在创建时效果很好,但在更新中却没有显示。
newsscript.js
$(document).ready(function () {
function cleardrop() {
$('#newscommodity').html("<option value=''>------- Select --------</option>");
$('#newsgroups').html("<option value=''>------- Select --------</option>");
$('#newsorigin').html("<option value=''>------- Select --------</option>");
$('#newsmanufacture').html("<option value=''>------- Select --------</option>");
$('#newsintendeduse').html("<option value=''>------- Select --------</option>");
$('#newsfinality').html("<option value=''>------- Select --------</option>");
$('#newsspecies').html("<option value=''>------- Select --------</option>");
}
var dept= $('#newsdepartment').val();
if(dept=='Animal Health'){
$("#newsimportcategory").html(
["<option value=''>------- Select --------</option>",
"<option value='Animal and Animal Products'>Animal and Animal Products</option>",
"<option value='Animal Feed, Biologics and Veterianry Drugs'>Animal Feed, Biologics and Veterianry Drugs</option>"
]);
$('#newsimportcategory').change(function () {
var impcat= $(this).val();
if(impcat=='Animal and Animal Products'){
$('#newscategorylabel').removeClass("hide");
$('#newscommoditylabel').removeClass("hide");
$('#newsspecieslabel').removeClass("hide");
$('#newsoriginlabel').removeClass("hide");
$('#newsfinalitylabel').removeClass("hide");
$('#newsmanufacturelabel').addClass("hide");
$('#newsgroupslabel').addClass("hide");
$('#newsintendeduselabel').addClass("hide");
$.post("/import-conditions/admin/animal/listsimptcat",function(data) {
$( "select#newscategory").html(data)
});
$('#newscategory').change(function () {
cleardrop();
$.post("/import-conditions/animal/listscommodity?name=" +$(this).val(),function(data) {
$("select#newscommodity").html(data)
})
});
$('#newscommodity').change(function () {
$.post("/import-conditions/animal/listsspecies?name=" +$(this).val(),function(data) {
$("select#newsspecies").html(data)
})
$.post("/import-conditions/animal/listsorigin?name=" +$(this).val(),function(data) {
$("select#newsorigin").html(data)
})
$.post("/import-conditions/animal/listsfinality?name=" +$(this).val(),function(data) {
$("select#newsfinality").html(data)
});
});
$('#newsorigin').change(function () {
$.post("/import-conditions/animal/listsfinalitysinglesearch?name=" +$(this).val(),function(data) {
$("select#newsfinality").html(data)
}) ;
});
} else if(impcat=='Animal Feed, Biologics and Veterianry Drugs'){
$('#newscategorylabel').removeClass("hide");
$('#newscommoditylabel').removeClass("hide");
$('#newsgroupslabel').removeClass("hide");
$('#newsoriginlabel').removeClass("hide");
$('#newsmanufacturelabel').removeClass("hide");
$('#newsintendeduselabel').addClass("hide");
$('#newsfinalitylabel').addClass("hide");
$('#newsspecieslabel').addClass("hide");
cleardrop();
$.post("/import-conditions/admin/vetdrugsbiologicsfeed/listsimptcat",function(data) {
$( "select#newscategory").html(data)
});
$('#newscategory').change(function () {
cleardrop();
$.post("/import-conditions/vetdrugsbiologicsfeed/listscommodity?name=" +$(this).val(),function(data)
{
$( "select#newscommodity").html(data)
}) ;
});
$('#newscommodity').change(function () {
$.post("/import-conditions/vetdrugsbiologicsfeed/listsgroups?name=" +$(this).val(),function(data)
{
$( "select#newsgroups").html(data)
})
$.post("/import-conditions/vetdrugsbiologicsfeed/listsorigin?name=" +$(this).val(),function(data)
{
$( "select#newsorigin").html(data)
})
$.post("/import-conditions/vetdrugsbiologicsfeed/listsmanufacture?name=" +$(this).val(),function(data)
{
$( "select#newsmanufacture").html(data)
}) ;
});
}
else if(impcat==''){
$('#newscategorylabel').addClass("hide");
$('#newscommoditylabel').addClass("hide");
$('#newsgroupslabel').addClass("hide");
$('#newsoriginlabel').addClass("hide");
$('#newsmanufacturelabel').addClass("hide");
$('#newsintendeduselabel').addClass("hide");
$('#newsfinalitylabel').addClass("hide");
$('#newsspecieslabel').addClass("hide");
cleardrop();
}
});
}
else if(dept=='Plant Health')
{
$("#newsimportcategory").html(
["<option value=''>------- Select --------</option>",
"<option value='Beneficial Organisms'>Beneficial Organisms</option>",
"<option value='Plant and Plant Products'>Plant and Plant Products</option>"
]);
$('#newsimportcategory').change(function () {
var impcat= $(this).val();
if(impcat=='Beneficial Organisms'){
$('#newscategorylabel').removeClass("hide");
$('#newsspecieslabel').removeClass("hide");
$('#newsoriginlabel').removeClass("hide");
$('#newscommoditylabel').addClass("hide");
$('#newsfinalitylabel').addClass("hide");
$('#newsmanufacturelabel').addClass("hide");
$('#newsgroupslabel').addClass("hide");
$('#newsintendeduselabel').addClass("hide");
cleardrop();
$.post("/import-conditions/admin/beneficialorganisms/listsimptcat",function(data)
{
$( "select#newscategory").html(data)
});
$('#newscategory').change(function () {
cleardrop();
$.post("/import-conditions/beneficialorganisms/listsspecies?name=" +$(this).val(),function(data)
{
$( "select#newsspecies").html(data)
})
$.post("/import-conditions/beneficialorganisms/listsorigin?name=" +$(this).val(),function(data)
{
$( "select#newsorigin").html(data)
})
});
}
else if(impcat=='Plant and Plant Products'){
$('#newscategorylabel').removeClass("hide");
$('#newscommoditylabel').removeClass("hide");
$('#newsspecieslabel').removeClass("hide");
$('#newsintendeduselabel').removeClass("hide");
$('#newsoriginlabel').removeClass("hide");
$('#newsmanufacturelabel').addClass("hide");
$('#newsfinalitylabel').addClass("hide");
$('#newsgroupslabel').addClass("hide");
cleardrop();
$.post("/import-conditions/admin/plants/listsimptcat",function(data)
{
$( "select#newscategory").html(data)
});
$('#newscategory').change(function () {
cleardrop();
$.post("/import-conditions/plants/listscommodity?name=" +$(this).val(),function(data)
{
$( "select#newscommodity").html(data)
})
});
$('#newscommodity').change(function () {
$.post("/import-conditions/plants/listsspecies?name=" +$(this).val(),function(data)
{
$( "select#newsspecies").html(data)
})
$.post("/import-conditions/plants/listsintendeduse?name=" +$(this).val(),function(data)
{
$( "select#newsintendeduse").html(data)
})
$.post("/import-conditions/plants/listsorigin?name=" +$(this).val(),function(data)
{
$( "select#newsorigin").html(data)
}) ;
});
$('#newsorigin').change(function () {
$.post("/import-conditions/plants/listsfinalitysinglesearch?name=" +$(this).val(),function(data)
{
$( "select#newsorigin").html(data)
});
});
}
else if(impcat==''){
$('#newscategorylabel').addClass("hide");
$('#newscommoditylabel').addClass("hide");
$('#newsgroupslabel').addClass("hide");
$('#newsoriginlabel').addClass("hide");
$('#newsmanufacturelabel').addClass("hide");
$('#newsintendeduselabel').addClass("hide");
$('#newsfinalitylabel').addClass("hide");
$('#newsspecieslabel').addClass("hide");
cleardrop();
}
});
}
else if(dept=='Food Safety')
{
$("#newsimportcategory").html(
["<option value=''>------- Select --------</option>",
"<option value='Process Food Products'>Process Food Products</option>"
]);
$('#newsimportcategory').change(function () {
var impcat= $(this).val();
$.post("/import-conditions/admin/processfood/listsimptcat",function(data)
{
$( "select#newscategory").html(data)
});
if(impcat=='Process Food Products'){
$('#newscategorylabel').removeClass("hide");
$('#newscommoditylabel').removeClass("hide");
$('#newsmanufacturelabel').removeClass("hide");
$('#newsoriginlabel').removeClass("hide");
$('#newsspecieslabel').addClass("hide");
$('#newsfinalitylabel').addClass("hide");
$('#newsgroupslabel').addClass("hide");
$('#newsintendeduselabel').addClass("hide");
$('#newscategory').change(function () {
cleardrop();
$.post("/import-conditions/processfood/listscommodity?name=" + $(this).val(),function(data)
{
$('#newscommodity').html("<option value=''>------- Select --------</option>");
$('#newsmanufacture').html("<option value=''>------- Select --------</option>");
$('#newsorigin').html("<option value=''>------- Select --------</option>");
$( "select#newscommodity").html(data);
});
});
$('#newscommodity').change(function () {
$.post("/import-conditions/processfood/listsmanufacture?name=" +$(this).val(),function(data)
{
$( "select#newsmanufacture").html(data)
});
$.post("/import-conditions/processfood/listsorigin?name=" +$(this).val(),function(data)
{
$( "select#newsorigin").html(data)
});
});
}
else if(impcat==''){
$('#newscategorylabel').addClass("hide");
$('#newscommoditylabel').addClass("hide");
$('#newsgroupslabel').addClass("hide");
$('#newsoriginlabel').addClass("hide");
$('#newsmanufacturelabel').addClass("hide");
$('#newsintendeduselabel').addClass("hide");
$('#newsfinalitylabel').addClass("hide");
$('#newsspecieslabel').addClass("hide");
cleardrop();
}
});
}
});
_form.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
/* @var $this yii\web\View */
/* @var $model backend\models\News */
/* @var $form yii\widgets\ActiveForm */
?>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.3.min.js" ></script>
<div class="news-form">
<?php $form = ActiveForm::begin(['options'=>['enctype'=>'multipart/form-data']]); ?>
<div id="pagecontent">
<h1 align="center">
Add New News Form
</h1>
<br />
<table width="90%" border="0" cellspacing="0" cellpadding="8">
<tr>
<td width="30%" align="Right"><h2><strong>News Heading: </strong></h2></td>
<td width="70%"><label>
<?= $form->field($model, 'headline')->textInput(['style' => 'width: 900px'])->label(false) ?>
</label></td>
</tr>
<tr>
<td align="right"><h2><strong>Content: </strong></h2></td>
<td><label>
<?= $form->field($model, 'story')->textarea(['rows' => 6,'style' => 'width: 900px; height: 380px;'])->label(false) ?>
</label></td>
</tr>
<tr>
<td width="30%" align="Right"><h2><strong>Department: </strong></h2></td>
<td width="70%"><label>
<?= $form->field($model, 'department')->textInput(['style' => 'width: 400px','readonly'=>true,'value'=>Yii::$app->user->identity->department,'id'=>'newsdepartment'])->label(false) ?>
</label></td>
</tr>
<tr>
<td width="30%" align="Right"><h2><strong>Import Category: </strong></h2></td>
<td width="70%"><label>
<?= $form->field($model, 'importcategory')->dropDownList(
ArrayHelper::map(['empty'=>'Empty string'], 'id', 'value'),
[
'id'=>'newsimportcategory',
'style' => 'width:400px',
])->label(false);?>
</label></td>
</tr>
<tr>
<td align="right"><h2><strong>Items: </strong></h2></td>
<td><label>
<div class="hide" id="newscategorylabel"><label class="control-label">Category: </label>
<?= $form->field($model, 'category')->dropDownList( ArrayHelper::map(['empty'=>'Empty string'], 'id', 'value'),
['id'=>'newscategory',
'style' => 'width:400px',
])->label(false);?>
</div>
<div class="hide" id="newscommoditylabel"><label class="control-label">Commodity: </label>
<?= $form->field($model, 'commodity')->dropDownList(
ArrayHelper::map(['empty'=>'Empty string'], 'id', 'value'),
[ 'id'=>'newscommodity',
'style' => 'width:400px',
])->label(false);?>
</div>
<div class="hide" id="newsgroupslabel"><label class="control-label" >Groups: </label>
<?= $form->field($model, 'groups')->dropDownList(
ArrayHelper::map(['empty'=>'Empty string'], 'id', 'value'), ['id'=>'newsgroups',
'style' => 'width:400px',
])->label(false);?>
</div>
<div class="hide" id="newsspecieslabel"><label class="control-label" >Species: </label>
<?= $form->field($model, 'species')->dropDownList(
ArrayHelper::map(['empty'=>'Empty string'], 'id', 'value'), ['id'=>'newsspecies',
'style' => 'width:400px',
])->label(false);?>
</div>
<div class="hide" id="newsintendeduselabel"><label class="control-label" >Intended Use: </label>
<?= $form->field($model, 'intendeduse')->dropDownList(
ArrayHelper::map(['empty'=>'Empty string'], 'id', 'value'),
['id'=>'newsintendeduse',
'style' => 'width:400px',
])->label(false);?>
</div>
<div class="hide" id="newsoriginlabel"><label class="control-label" >Country of Origin: </label>
<?= $form->field($model, 'origin')->dropDownList(
ArrayHelper::map(['empty'=>'Empty string'], 'id', 'value'),
[
'id'=>'newsorigin',
'style' => 'width:400px',
])->label(false);?>
</div>
<div class="hide" id="newsmanufacturelabel"><label class="control-label" >Manufacture: </label>
<?= $form->field($model, 'manufacture')->dropDownList(
ArrayHelper::map(['empty'=>'Empty string'], 'id', 'value'),
[
'style' => 'width:400px',
])->label(false);?>
</div>
<div class="hide" id="newsfinalitylabel"><label class="control-label" >Finality: </label>
<?= $form->field($model, 'finality')->dropDownList(
ArrayHelper::map(['empty'=>'Empty string'], 'id', 'value'),
[ 'id'=>'newsfinality',
'style' => 'width:400px',
])->label(false);?>
</div>
</label></td>
</tr>
<tr>
<td align="right"><h2><strong>Status</strong></h2></td>
<td><?= $form->field($model, 'status')->dropDownList([ 'Active' => 'Active', 'Restricted' => 'Restricted','Prohibited' => 'Prohibited' ], ['prompt' => 'Select Status','style' => 'width: 400px'])->label(false) ?></td>
</tr>
<tr>
<td align="right"><h2><strong>News Picture</strong></h2></td>
<td><label>
<?=$form->field($model, 'img')->fileInput()->label(false);?>
</label></td>
</tr>
<tr>
<td> </td>
<td align="center"><label>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Add News' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
</label></td>
</tr>
</table>
<br />
<br />
</div>
<?php ActiveForm::end(); ?>
</div>
Controller.php
<?php
namespace backend\controllers;
use Yii;
use backend\models\News;
use backend\models\NewsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
class NewsController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
public function actionIndex()
{
$searchModel = new NewsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single News model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new News model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$timezone = "America/Belize";
date_default_timezone_set($timezone);
$model = new News();
$model->created_date= date('d-m-Y h:i:s a');
$model->input_user= Yii::$app->user->identity->username;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
protected function findModel($id)
{
if (($model = News::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
Model.php
<?php
namespace backend\models;
use Yii;
class News extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public $importcategory;
public static function tableName()
{
return 'news';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['headline', 'story', 'status', 'department', 'input_user', 'created_date'], 'required'],
[['headline', 'story','importcategory'], 'string'],
[['img', 'name', 'category', 'commodity', 'manufacture', 'groups', 'species', 'intendeduse', 'origin', 'finality', 'status', 'department', 'input_user', 'created_date'], 'string', 'max' => 255],
[['department'], 'exist', 'skipOnError' => true, 'targetClass' => Loginusers::className(), 'targetAttribute' => ['department' => 'department']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'headline' => 'Headline',
'img' => 'Img',
'story' => 'Story',
'name' => 'Name',
'category' => 'Category',
'commodity' => 'Commodity',
'manufacture' => 'Manufacture',
'groups' => 'Groups',
'species' => 'Species',
'intendeduse' => 'Intendeduse',
'origin' => 'Origin',
'finality' => 'Finality',
'status' => 'Status',
'department' => 'Department',
'input_user' => 'Input User',
'created_date' => 'Created Date',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getDepartment0()
{
return $this->hasOne(Loginusers::className(), ['department' => 'department']);
}
}
我知道只学习javascript和jquery的代码很乱。关于如何进行的任何建议?或有更好的方法我们非常感谢您的帮助。
答案 0 :(得分:0)
我会在每个函数的关键位置添加很多console.log(“我在函数x中,正在执行y”)键入注释。接下来,在Chrome中按F12键,以查看控制台调试器,并查看是否要撤消多次隐藏操作。
示例:JavaScript控制台
function makeIconFromHex() {
console.log('makeIconFromHex..............');
console.log(matrix);
var width = matrix[0].length;
var height = matrix.length;
}
另外,也许像这样更好:
<?= $form->field($model, 'somefield')
->textInput(['hidden'=> $model->isNewRecord ? false : true])
->label(false) ?>