selimppc

Yii-2-guide-

Dec 27th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.02 KB | None | 0 0
  1. Active Form :: Input Text:
  2. ============================
  3. <?= Html::activeLabel($user, 'email'); ?>
  4. <?= Html::activeTextInput($user, 'email'); ?>
  5. <?= Html::error($user, 'email'); ?>
  6.  
  7.  
  8.  
  9. <?= $form->field($model, 'id',['options' => ['class' => ''],'template' => '{input}',])->input('hidden') ?>
  10.  
  11.  
  12. // with label
  13. <?= $form->field($model,'inserttime')->hiddenInput()->label('')   ?>
  14.  
  15.  
  16. // Placeholder
  17. <?= $form->field($model, 'username')->textInput()->hint('Please enter your name')->label('Name') ?>
  18.  
  19. //HTML5 fields
  20. <?= $form->field($model, 'email')->input('email') ?>
  21.  
  22. //uploading multiple files or selecting multiple items
  23. <?=
  24.     // allow multiple files to be uploaded:
  25.     echo $form->field($model, 'uploadFile[]')->fileInput(['multiple'=>'multiple']);
  26.  
  27.     // allow multiple items to be checked:
  28.     echo $form->field($model, 'items[]')->checkboxList(['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C']);
  29. ?>
  30.  
  31.  
  32. ///customize a label within a view using the following code:/
  33. <?= $form->field($model, 'name')->label('Your Name') ?>
  34. <?= $form->field($model, 'email')->label('Your Email') ?>
  35.  
  36.  
  37. ////// Dropdown Menu with List Model
  38. ===============================================================
  39.  
  40. <?php
  41.  
  42. use app\models\Menupanel;
  43. use yii\helpers\ArrayHelper;
  44.  
  45.  
  46.     $form->field($model, 'c_parentid')->dropDownList(ArrayHelper::map(Menupanel::find()->all(), 'id', 'c_name'), ['prompt'=>'- please select -'])->hint('Please select parent menu')->label('Parent Menu Type')
  47.  
  48. ?>
  49.  
  50.  
  51.  
  52. // Gridview Status -> Active or Inactive
  53. ==============================================================
  54. <?php
  55. [
  56.     'attribute'=> 'c_active',
  57.     'value'=>function ($model){
  58.         if($model->c_active == 1){
  59.             return "Active";
  60.         }else{
  61.             return "No Activity";
  62.         }
  63.     },
  64.     'filter' => true,
  65. ],
  66. ?>
  67.  
  68.  
  69. Resource
  70. ==============================
  71.  
  72.  
  73.  
  74. http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview/
  75.  
  76. http://www.yiiframework.com/wiki/679/filter-sort-by-summary-data-in-gridview-yii-2-0/
  77.  
  78. render-a-form-in-a-modal-popup-using-ajax/
  79. ==========================================
  80. http://www.yiiframework.com/wiki/690/render-a-form-in-a-modal-popup-using-ajax/
  81.  
  82. PJAX:
  83. =====
  84. http://stackoverflow.com/questions/23953659/using-pjax-in-yii2-for-updating-a-div
  85.  
  86. Javascript
  87. ==========
  88. http://pastebin.com/NKTi8KK4
  89.  
  90.  
  91.  
  92. // Date Picker
  93. ================================================
  94.  
  95. <?php  $form->field($model,'c_expdate')->widget(DatePicker::className(),
  96.         [
  97.             'language' => 'en',
  98.             'clientOptions' => [
  99.                 'showAnim'=>'fold',
  100.                 'changeMonth' => 'true',
  101.                 'changeYear' => 'true',
  102.                 'dateFormat' => 'yy-mm-dd',
  103.                 'keyboardNavigation' => false,
  104.             ],
  105.  
  106.         ]) ?>
  107.  
  108.  
  109.  
  110.  
  111. //detailView yii2
  112. =====================
  113.  
  114. <?= DetailView::widget([
  115.         'model' => $model,
  116.         'attributes' => [
  117.             'id',
  118.             'c_name',
  119.             'c_desc',
  120.             [
  121.                 'attribute'=> 'c_active',
  122.                 'value' => $model->c_active,
  123.             ],
  124.  
  125.         ],
  126.     ]) ?>
  127.  
  128.  
  129. find model data by ID
  130. ============================
  131. <?php
  132.  
  133.  $model = User::find()->where(['id' => $id])->all();
  134. ?>
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141. // Set Flash Message in View page:::
  142. =======================================
  143. <?php
  144.   controlller::   Yii::$app->getSession()->setFlash('success', 'New password was saved.');
  145. ?>
  146.  
  147. <?php
  148.     use yii\bootstrap\Alert;
  149. ?>
  150. <?php
  151.  
  152.         if($flash = Yii::$app->session->getFlash('success')){
  153.             echo Alert::widget(['options' => ['class' => 'alert-success'], 'body' => $flash]);
  154.         }elseif($flash = Yii::$app->session->getFlash('error')){
  155.             echo Alert::widget(['options' => ['class' => 'alert-error'], 'body' => $flash]);
  156.         }
  157. ?>
  158.  
  159.  
  160. /// Dependable Dropdown in
  161. ====================================================
  162.  
  163. in View ::
  164. ==========
  165. <?php
  166.  
  167.    <?= $form->field($model, 'c_parentid')->dropDownList([
  168.         'Module'=> ArrayHelper::map(Menupanel::find()->where(['c_type' => 'MODU'])->all(), 'id', 'c_name'),
  169.         'Menu'=> ArrayHelper::map(Menupanel::find()->where(['c_type' => 'MENU'])->all(), 'id', 'c_name'),
  170.         'Sub-Menu'=> ArrayHelper::map(Menupanel::find()->where(['c_type' => 'SUBM'])->all(), 'id', 'c_name')],
  171.         [
  172.             'prompt'=>'- please select -',
  173.             'onchange'=>'
  174.                        $.post( "'.Yii::$app->urlManager->createUrl('/roledt/menu-item&id=').'"+$(this).val(),
  175.                            function( data ) {
  176.                              $( "#'.Html::getInputId($model, 'c_menuid').'" ).html( data );
  177.                            });
  178.                    '])->hint('Please select parent menu')->label('Parent Menu ID'); ?>
  179.  
  180.     <?= $form->field($model, 'c_menuid')->dropDownList(
  181.         ['prompt'=>'- please select -'])->hint('Please select menu item')->label('Menu Item') ?>
  182.  
  183.  
  184.  
  185. ?>
  186.  
  187. in controller::
  188. ===============
  189. <?php
  190.  
  191.     public function actionMenuItem($id){
  192.         $countMenu = Menupanel::find()
  193.             ->where(['c_parentid' => $id])
  194.             ->count();
  195.  
  196.         $menuItem = Menupanel::find()
  197.             ->where(['c_parentid' => $id])
  198.             ->orderBy('id ASC')
  199.             ->all();
  200.  
  201.         if($countMenu>0){
  202.             foreach($menuItem as $post){
  203.                 echo "<option value='".$post->id."'>".$post->c_name."</option>";
  204.             }
  205.         }
  206.         else{
  207.             echo "<option>- Nothing Found -</option>";
  208.         }
  209.  
  210.     }
  211.  
  212. ?>
  213.  
  214.  
  215.  
  216.  
  217. Action Buttons in Gridview :::
  218. =================================================================================
  219. <?php
  220. [
  221.     'class' => 'yii\grid\ActionColumn',
  222.     'header'=>'Action',
  223.     'template'=>'{view}{update}{info}',
  224.     'buttons' => [
  225.         'info' => function ($url, $model) {
  226.             return Html::a('<span class="glyphicon glyphicon-info-sign"></span>', $url, [
  227.                 'title' => Yii::t('app', 'Info'),
  228.             ]);
  229.         }
  230.     ],
  231.     'urlCreator' => function ($action, $model, $key, $index) {
  232.         if ($action === 'info') {
  233.             $url = Yii::t('app', ['user/index', 'id'=>'1']);  // your own url generation logic
  234.             return $url;
  235.         }
  236.     }
  237.  
  238. ]
  239.  
  240. ?>
  241.  
  242.  
  243.  
  244. popup- yii2
  245. ============
  246.  
  247. http://stackoverflow.com/questions/23927579/how-can-i-show-the-default-login-page-as-modal-popup-in-yii2
  248. http://getbootstrap.com/javascript/#modals-usage
  249.  
  250.  
  251. https://toster.ru/q/76516
  252.  
  253.  
  254.  
  255.  
  256. // Modal ::
  257. ========================
  258.  
  259. <!-- Button trigger modal -->
  260. <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal1">
  261.     Launch
  262. </button>
  263.  
  264. <!-- Modal -->
  265. <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  266.     <div class="modal-dialog">
  267.         <div class="modal-content">
  268.             <div class="modal-header">
  269.                 <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
  270.                 <h4 class="modal-title" id="myModalLabel"><?= $this->title; ?></h4>
  271.             </div>
  272.             <div class="modal-body">
  273.                 <?= $this->title; ?>
  274.  
  275.                
  276.  
  277.  
  278.  
  279.             </div>
  280.  
  281.         </div>
  282.     </div>
  283. </div>
  284.  
  285.  
  286. OR
  287. ==
  288. <a href="javascript:void(0);" onclick="login();return false;">Login</a>
  289. <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"></div>
  290. <script>
  291.     function login(){
  292.         $.ajax({
  293.             type:'POST',
  294.             url:'index.php?r=roledt/create',
  295.             success: function(data)
  296.             {
  297.                 $('#myModal').html(data);
  298.                 $('#myModal').modal();
  299.             }
  300.         });
  301.     }
  302. </script>
  303. <span data-dismiss="modal"><span aria-hidden="true"></span> </span>
  304.  
  305.  
  306.  
  307. ////////////////////////////////////////////
  308.  
  309. <?php Modal::begin([
  310.         'id' => 'modal',
  311.         'header' => '<h4>Hello world</h4>',
  312.         'toggleButton' => ['label' => 'click me'],
  313.         'class' => "btn btn-primary btn-lg",
  314.     ]); ?>
  315.  
  316.     <?= $this->render('//site/about', [
  317.         'model' => $model,
  318.     ]) ?>
  319.  
  320. <?php Modal::end(); ?>
  321.  
  322.  
  323.  
  324.  
  325. // Render another file/folder
  326. =============================
  327. <?= $this->render('//site/about', [
  328.     'model' => $model,
  329. ]) ?>
  330.  
  331. <?= \Yii::$app->view->renderFile('@app/views/site/about.php'); ?>
  332.  
  333.  
  334.  
  335.  
  336.  
  337. /// SearchModel with Parameters from Controller
  338. ===============================================
  339. Controller:
  340. ==========
  341. <?php
  342.     $searchModel1 = new RoledtSearch();
  343.     $dataProvider = $searchModel1->searchDt(['RoledtSearch'=>['c_id'=>$id]]);
  344.  
  345. ?>
  346.  
  347. in SearchModel::
  348. ===============
  349. <?php
  350.  
  351.     $this->addCondition($query, 'c_id');
  352.  
  353. OR ::::::::::::::::::::::::::::::::::::
  354.  
  355.     $query->andFilterWhere([
  356.  
  357.         'z_roledt.c_id' => $this->c_id,
  358.  
  359.     ]);
  360.  
  361.  
  362. ?>
  363.  
  364.  
  365.  
  366. /// Readonly in Form
  367. =======================
  368. <?= $form->field($model, 'TYPE')->input('TYPE', $model->isNewRecord ? ['readonly' => true]: ['readonly' => true])->hint('Please enter TYPE')->label('TYPE') ?>
  369.  
  370.  
  371.  
  372. /// User Login System and Tocken peremeters:::
  373. ===============================================
  374.  
  375. http://www.tuicool.com/articles/nMrIFj
  376.  
  377.  
  378. /// For loop for local images::::
  379. ==================================
  380. <?php for ($x=1; $x<=3; $x++) { ?>
  381.     <div class="item">
  382.         <img src="<?= Yii::$app->request->BaseUrl.'/images/banner/'.$x.'.png' ?>" alt="Image 001">
  383.         <div class="carousel-caption">
  384.             Pipeline Racks
  385.         </div>
  386.     </div>
  387.  
  388. <?php   }    ?>
  389.  
  390.  
  391.  
  392. /// Buttom with pop-up Image/content:
  393. ======================================
  394. <?php
  395.    
  396.  
  397. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">Popup image</button>
  398.  
  399. <div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  400.     <div class="modal-dialog">
  401.         <div class="modal-content">
  402.             <div class="modal-body">
  403.                 <img src="<?= Yii::$app->request->BaseUrl.'/images/banner'.'/2.png' ?>" class="img-responsive">
  404.             </div>
  405.         </div>
  406.     </div>
  407. </div>
  408.  
  409.  
  410. ?>
  411.  
  412.  
  413.  
  414.  
  415. /// Bootstrap Photo gallery
  416. ===========================
  417.  
  418. http://fearlessflyer.com/next-and-previous-buttons-bootstrap-photo-gallery/
  419.  
  420. /// Detail View :::
  421. ====================
  422. <?php
  423.     public function getMenuParent()
  424.     {
  425.         return $this->hasOne(Menupanel::className(), ['id' => 'c_parentid']);
  426.     }
  427.    
  428.     in widget ::;
  429.      <?= DetailView::widget([
  430.         'model' => $model,
  431.         'attributes' => [
  432.             'menuParent.c_name',
  433.         ],
  434.     ]) ?>
  435. ?>
  436.  
  437.  
  438. /// Grid View:
  439. =================
  440.  
  441. <?php
  442.  
  443.     public $Rolehd;
  444.    
  445.     public function getRolehd()
  446.     {
  447.         return $this->hasOne(Rolehd::className(), ['id' => 'c_id']);
  448.     }
  449.    
  450.     GridView::widget([
  451.         'dataProvider' => $dataProvider,
  452.         'filterModel' => $searchModel,
  453.         'columns' => [
  454.             ['class' => 'yii\grid\SerialColumn'],
  455.             [
  456.                 'header'=> 'Role Header',
  457.                 'attribute' => 'rolehd',
  458.                 'value' => 'rolehd.c_name'
  459.             ],
  460.         ],
  461.     ]);
  462.  
  463. ?>
Advertisement
Add Comment
Please, Sign In to add comment