/**
  * Updates an existing Category model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $parent_category = [];
     $get_categorys = [];
     $get_category = Category::getCateory($model->parent_category);
     if (!empty($get_category)) {
         // cek jika parent nya adalah level pertama sperti kategori produk , kategori berita
         $get_categorys = Category::getCategories($get_category->parent_category);
         if (!empty($get_categorys)) {
             $model->sub_category = $model->parent_category;
         }
         if (!empty($get_category) && ($get_category->parent_category !== 0 && $get_category->parent_category !== '')) {
             $parent_category = Category::getParentCategorys($get_category->id);
             $model->parent_category = $get_category->parent_category;
         } else {
             $parent_category = Category::getParentCategorys($model->id);
             //                echo '<pre>';print_r($parent_category);die;
         }
     } else {
         $parent_category = Category::getParentCategorys($model->parent_category);
     }
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('update', ['model' => $model, 'parent_category' => $parent_category, 'sub_categorys' => $get_categorys]);
     }
 }
예제 #2
0
?>

    <?php 
echo $form->field($model, 'id')->hiddenInput()->label(false);
?>
    
    <?php 
echo $form->field($model, 'nama')->textInput(['maxlength' => true]);
?>

    <?php 
echo $form->field($model, 'deskripsi')->textarea(['rows' => 6]);
?>

    <?php 
echo $form->field($model, 'parent_category', ['template' => "{label}{input}\n{hint}\n{error}"])->dropDownList($model->isNewRecord ? ArrayHelper::map(Category::getParentCategorys(), 'id', 'nama') : ArrayHelper::map($parent_category, 'id', 'nama'), ['prompt' => '-- Pilih --', 'onChange' => '
                                        var csrfToken = $("meta[name=\'csrf-token\']").attr(\'content\');
                                        $.ajax({
                                            url :   "' . Yii::$app->urlManager->createAbsoluteUrl('category/ajaxlihatsubcat') . '",
                                            dataType: "html",
                                            type: "POST",
                                            data: {parent : $(this).val(), id: $("#' . Html::getInputId($model, 'id') . '").val() ,_csrf : csrfToken},
                                            beforeSend: function(){
                                                $("input[type=\'submit\']").attr("disabled","disabled");
                                                $("#' . Html::getInputId($model, 'sub_category') . '").addClass(\'loader-icon-dropdown\');
                                                $("#' . Html::getInputId($model, 'sub_category') . '").prop("disabled", true);
                                            },
                                            success: function(data){
                                                $("#' . Html::getInputId($model, 'sub_category') . '").removeClass(\'loader-icon-dropdown\');
                                                $("#' . Html::getInputId($model, 'sub_category') . '").prop("disabled", false);
                                                $("#' . Html::getInputId($model, 'sub_category') . '").html(data);