I'm trying to do a delete action with Pjax (without refreshing the page).
The register is deleted properly, page no reloads, BUT, if immediately I try to delete another record, don't load the modal.
So the delete button cannot call to modal for new delete.
I hope you can help me to resolve this. Thanks
Here is my code:
Step 1
file: index view
<?php Pjax::begin(['id' => 'pjax-container']); ?>
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
.....
'columns' => [
...others columns
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view} {delete}',
'buttons' => [
'delete' => function ($url, $model, $key) {
return Html::a(
'<span class="material-icons">delete</span>',
'javascript:void(0)',
[
'data-ruta' => Url::toRoute(['delete', 'id' => $model->id]),
'id' => $model->id,
'class' => 'btn-eliminar-competencia option-danger',
'title' => __('GxP', 'commons.delete'),
'aria-label' => "Eliminar",
'data-pjax' => "0",
'data-method' => "post"
]
) . '</div>';
}
]
]
]
]);
?>
<?php Pjax::end(); ?>
Step 2
file index view
After clicking the delete button, go to open modal
<div class="modal bootstrap-dialog" role="dialog" aria-hidden="true" id="modal-eliminar-
competencia" aria-labelledby="w3_title" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title"><?= __('GxP', 'commons.delete') ?></h4>
</div>
<div class="modal-body text-center">
<div class="bootstrap-dialog-message"><?= __('GxP', 'dialogs.sure_delete_element') ?></div>
<div id="text-modal-competencia"></div>
</div>
<div class="modal-footer text-center">
<button class="btn btn-success" data-dismiss="modal">
<?= __('GxP', 'commons.cancel') ?>
</button>
<button class="btn btn-primary btn-modal-eliminar-competencia">
<?= __('GxP', 'commons.accept') ?>
</button>
</div>
</div>
</div>
</div>
Step 3
file: index.js
$(document).ready(function(){
var ruta
eliminar = 0
redirect = 0
modal = $('#modal-eliminar-competencia')
modal_competencia = $('#modal-competencia')
$('.div2').click(function(){
window.location = $('#tipos-competencias').attr('href')
})
$('.btn-eliminar-competencia').click(function(){
eliminar = $(this).attr('data-ruta')
$('#text-modal-competencia').html('')
id = $(this).attr('id')
$.ajax({
type:'post',
url:'/competences/competences-asociated',
data:{id:id},
dataType:'json'
})
.done(function(r){
console.log(r)
mensaje_text =
'<br>'+
'<p>'+__('commons.info')+':</p>'+
'<small>'+
r.positions+__('dialogs.associated_positions')+
'</small>'+
'<br>'+
'<small>'+
r.reagents+__('dialogs.associated_reagents')+
'</small>';
$('#text-modal-competencia').html(mensaje_text)
})
.fail(function(){
})
modal.modal({backdrop:'static',keyboard:true})
})
// here resolve the delete item and reload container with Pjax
$('.btn-modal-eliminar-competencia').click(function(){
$.ajax({
type:'post',
url:eliminar,
dataType:'json',
success:function(response){
if(response.code == 204){
redirect = response.redirect
$('.message-contenido .message-text').html(__('dialogs.success_delete'))
modal.modal('hide')
$('.capa').show()
$('.message-action').show()
ruta = false
$.pjax.reload({container:'#pjax-container'})
}
else{
modal_alert(__('dialogs.unespected_error'))
}
}
})
})
})
Related
I have a problem with how to upload files excel with phpsreadsheet to the database. I want to put the data into two tables namely:
Beasiswa contain table: beasiswa_nama, beasiswa_id, beasiswa_file (link file), beasiswa_tahun, DATE_CREATED, DATE_UPDATED.
data_mahasiswa table cotain table: id_mhs, nama_mhs, nim_mhs, (FK) id_beasiswa
How to insert a file and read it to the Beasiswa table, and student data to data_mahasiswa?
Relation table:
Error create data:
Beasiswa Controller
public function create()
{
$this->form_validation->set_rules('nama_beasiswa', 'Nama Beasiswa', 'trim|required');
$this->form_validation->set_rules('tahun_beasiswa', 'Tahun Mahasiswa', 'trim|required');
if (empty($_FILES['nama_mahasiswa_beasiswa']['name'])) {
$this->form_validation->set_rules('nama_mahasiswa_beasiswa', 'File Nama Mahasiswa', 'required');
}
if ($this->form_validation->run() == TRUE) {
// true case
$upload_nama_mahasiswa = $this->_upload_nama_mahasiswa();
$data = array(
'beasiswa_nama' => htmlspecialchars($this->input->post('nama_beasiswa', true)),
'beasiswa_tahun' => htmlspecialchars($this->input->post('tahun_beasiswa', true)),
'beasiswa_file' => $upload_nama_mahasiswa,
'date_created' => date("Y-m-d H:i:s")
);
$create = $this->m_beasiswa->create($data);
if ($create == true) {
$this->session->set_flashdata('success', 'Berhasil Upload Data Beasiswa');
redirect('beasiswa/', 'refresh');
} else {
$this->session->set_flashdata('errors', 'Terjadi Galat. Silahkan Periksa Kembali!!');
redirect('beasiswa/create', 'refresh');
}
} else {
$this->render_template('beasiswa/create', $this->data);
}
}
private function _upload_nama_mahasiswa()
{
$filename = 'import-data';
$config['upload_path'] = 'assets/file/beasiswa/';
$config['file_name'] = $filename;
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'xlsx|xls';
$config['max_size'] = '1024';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('nama_mahasiswa_beasiswa')) {
// $error = array('errors' => $this->upload->display_errors('File yang anda masukkan tidak sesuai permintaan, atau file terlalu besar.'));
$this->session->set_flashdata('errors', 'File yang anda masukkan tidak sesuai permintaan, atau file terlalu besar.');
redirect('beasiswa/create', 'refresh');
} else {
$data = array('upload_data' => $this->upload->data());
$img = $data['upload_data']['file_name'];
$excelReader = new PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$loadExcel = $excelReader->load($config['upload_path'] . $img);
$sheet = $loadExcel->getActiveSheet()->toArray(null, true, true, true);
$dataExcel = array();
// $this->data['sheet'] = $sheet;
$numRow = 3;
foreach ($sheet as $row) {
if ($numRow > 3) {
array_push(
$dataExcel,
[
'nama_mhs' => $row['A'],
'nim_mhs' => $row['B']
]
);
}
$numRow++;
}
$this->db->insert_batch('data_beasiswa', $dataExcel);
unlink($config['upload_path'] . $img);
}
}
Beasiswa Model
public function create($data)
{
if ($data) {
$insert = $this->db->insert('beasiswa', $data);
return ($insert == true) ? true : false;
}
}
Beasiswa View
<?php
defined('BASEPATH') or exit('No direct script access allowed');
?>
<!-- Main Content -->
<div class="main-content">
<section class="section">
<div class="section-header">
<div class="section-header-back">
<i class="fas fa-arrow-left"></i>
</div>
<h1>Tambah Beasiswa</h1>
</div>
<div class="section-body">
<div class="row">
<div class="col-md-12 col-xs-12 col-xl-12 col-12">
<div id="messages"></div>
<?php if ($this->session->flashdata('success')) : ?>
<div class="alert alert-success alert-dismissible">
<div class="alert-body">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<?php echo $this->session->flashdata('success'); ?>
</div>
</div>
<?php elseif ($this->session->flashdata('errors')) : ?>
<div class="alert alert-danger alert-dismissible show fade">
<div class="alert-body">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<?php echo $this->session->flashdata('errors'); ?>
</div>
</div>
<?php endif; ?>
<div class="card">
<div class="card-body">
<b style="font-size: 17px">Unduh format excel data mahasiswa</b><br>
Download template
<form action="<?php base_url('beasiswa/create') ?>" method="POST" enctype="multipart/form-data">
<div class="form-group">
<div class="section-title">Nama Program Beasiswa</div>
<input type="text" class="form-control" name="nama_beasiswa" id="nama_beasiswa" placeholder="Nama Program Beasiswa" value="<?= set_value('nama_beasiswa'); ?>">
<?= form_error('nama_beasiswa', '<small class="text-danger">', '</small>'); ?>
</div>
<div class="form-group">
<div class="section-title">Tahun Beasiswa</div>
<input type="text" class="form-control" name="tahun_beasiswa" id="tahun_beasiswa" placeholder="Tahun Beasiswa (contoh: 2020)" value="<?= set_value('tahun_beasiswa'); ?>">
<?= form_error('tahun_beasiswa', '<small class="text-danger">', '</small>'); ?>
</div>
<div class="form-group">
<div class="section-title">Upload File Nama Mahasiswa</div>
<input type="file" name="nama_mahasiswa_beasiswa" id="nama_mahasiswa_beasiswa" class="form-control col-sm-6 col-md-6">
<p class="text-danger" style="font-style:italic"><b>* Upload file sesuai dengan format yang sudah disesuaikan</b></p>
</div>
<button type="submit" class="btn btn-primary">Simpan</button>
Kembali
</form>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
It is because foreign key error probably happening because you insert a null data in data_mahasiswa table.
There was empty space in your excel (that probably have been edited?) that are used in foreach ($sheet as $row). You can fix this with:
foreach ($sheet as $row) {
if ($numRow > 3) {
if($row['A'] != null && $row['B'] != null)
{
$dataExcel[] = array(
'nama_mhs' => $row['A'],
'nim_mhs' => $row['B']
);
}
/*you can also break the function, if no empty row between data in your excel*/
/*else
{
break;
}*/
}
}
you need to get rid of the null value when you input the data to the database. above this line of your code at Beasiswa Controller
$this->db->insert_batch('data_beasiswa', $dataExcel);
add this line of code to delete cell that have null data
foreach ($dataExcel as $data => $val) {
if(empty($val['nama_mhs'])){
unset($dataExcel[$data]['nama_mhs']);
unset($dataExcel[$data]['nim_mhs']);
}
}
$dataExcel = array_filter($dataExcel);
$data = array('data_excel' => $dataExcel);
I had constructed a bootstrap menu and now I want to reproduce it with Yii2 Nav widget. So, here is the initial state:
<nav class="navbar navbar-inverse" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="true">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">BrandLogo</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1" aria-expanded="true">
<ul class="nav navbar-nav">
<li>
simple menu
</li>
<li class="dropdown">
dropdown menu <b class="caret"></b>
<ul class="dropdown-menu">
<li>
Submenu#1
</li>
<li>
Submenu#2
</li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left" action="/action_page.php">
<div class="form-group has-feedback search">
<input type="text" class="form-control" placeholder="Search" />
<i class="glyphicon glyphicon-search form-control-feedback"></i>
</div>
</form>
</div>
</div>
</nav>
And here is how it looks like:
Now I would like to do the same menu with Nav widget. Here is the code:
NavBar::begin([
'brandLabel' => 'BrandLogo',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse',
],
]);
$menuItems = [
[
'label' => 'simple menu',
'url' => ['#1']
],
[
'label' => 'dropdown menu',
'url' => ['#4'],
'items' => [
[
'label' => 'Submenu#1',
'url' => ['#1'],
],
[
'label' => 'Submenu#2',
'url' => ['#2'],
],
]
],
[
'label' => '
<form class="navbar-form navbar-left" action="/action_page.php">
<div class="form-group has-feedback search">
<input type="text" class="form-control" placeholder="Search" />
<i class="glyphicon glyphicon-search form-control-feedback"></i>
</div>
</form>',
'encode' => false,
'url' => false,
],
];
if (count($menuItems)) {
echo Nav::widget([
'options' => ['class' => 'navbar-nav'],
'items' => $menuItems,
]);
}
NavBar::end();
The problem is that the results aren't equal. I found a few problems:
The widget generates dropdown link as <a class="dropdown-toggle" href="/main/#4" data-toggle="dropdown">dropdown menu <span class="caret"></span></a> How can I get rid of data-toggle="dropdown" and class="dropdown-toggle"?
The search form is wrapped into <a></a> tags. That is why the navbar is broken:
How can I get rid of the unnecessary tag?
That is because you are not following the actual HTML structure, and according to that you need to add the form after the ul not inside the li, but that form should be part of the NavBar and if you look into the defination
Any content enclosed between the \yii\bootstrap\begin() and
\yii\bootstrap\end() calls of NavBar is treated as the content of the
navbar. You may use widgets such as yii\bootstrap\Nav or
\yii\widgets\Menu to build up such content.
so just move your form outside the $items after you are calling the Nav::widget() and before you call the NavBar::end().
And you can use the linkOptions to customize or remove the class or any other attribute related to the link
<?php
NavBar::begin([
'brandLabel' => 'BrandLogo',
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse',
],
]);
$menuItems = [
[
'label' => 'simple menu',
'url' => ['#1']
],
[
'label' => 'dropdown menu',
'url' => ['#4'],
'linkOptions' => [
'class' => 'my-class',
'data' => [
'toggle' => ''
]
],
'items' => [
[
'label' => 'Submenu#1',
'url' => ['#1'],
],
[
'label' => 'Submenu#2',
'url' => ['#2'],
],
]
],
];
if( count($menuItems) ){
echo Nav::widget([
'options' => ['class' => 'navbar-nav'],
'items' => $menuItems,
]);
}
?>
<form class="navbar-form navbar-left" action="/action_page.php">
<div class="form-group has-feedback search">
<input type="text" class="form-control" placeholder="Search" />
<i class="glyphicon glyphicon-search form-control-feedback"></i>
</div>
</form>
<?php
NavBar::end();
EDIT
if you are looking to completly remove the class name dropdown-toggle then you might have to override the yii\bootstrap\Nav::renderItems() by extending the widget because it is added by default as the bootstrap class so you have to just copy the renderItems() to your extended class and comment out the line Html::addCssClass ( $linkOptions , [ 'widget' => 'dropdown-toggle' ] ); which adds the class there and then change the namespace where you are calling the Nav from yii\bootstrap\Nav::widget() to common\components\Nav::widget()
Add the following class in your common\components\ or if you plan to copy it somewhere else do update the namespace in the code
<?php
/*
* 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.
*/
namespace common\components;
use yii\bootstrap\Nav as BaseNav;
use yii\bootstrap\Html;
use yii\helpers\ArrayHelper;
use yii\base\InvalidConfigException;
class Nav extends BaseNav {
/**
* Renders a widget's item.
* #param string|array $item the item to render.
* #return string the rendering result.
* #throws InvalidConfigException
*/
public function renderItem($item) {
if( is_string($item) ){
return $item;
}
if( !isset($item['label']) ){
throw new InvalidConfigException("The 'label' option is required.");
}
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
$label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
$options = ArrayHelper::getValue($item, 'options', []);
$items = ArrayHelper::getValue($item, 'items');
$url = ArrayHelper::getValue($item, 'url', '#');
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
if( isset($item['active']) ){
$active = ArrayHelper::remove($item, 'active', false);
} else{
$active = $this->isItemActive($item);
}
if( empty($items) ){
$items = '';
} else{
$linkOptions['data-toggle'] = 'dropdown';
Html::addCssClass($options, ['widget' => 'dropdown']);
// Html::addCssClass ( $linkOptions , [ 'widget' => 'dropdown-toggle' ] );
if( $this->dropDownCaret !== '' ){
$label .= ' ' . $this->dropDownCaret;
}
if( is_array($items) ){
$items = $this->isChildActive($items, $active);
$items = $this->renderDropdown($items, $item);
}
}
if( $active ){
Html::addCssClass($options, 'active');
}
return Html::tag('li', Html::a($label, $url, $linkOptions) . $items, $options);
}
}
Itis the same question as Pass data from a form to a controller in yii2.
I want to get data from user in 3fields and pass it to a controller action.
I'm trying to do this by javascript but it seems that the javascript function is not being called.
Nothing happens when I click on the button. No error in the console as well.
I'm attaching the code of 3 fields,button and javascript below.
index2.php
<?php
use yii\helpers\Html;
//use yii\grid\GridView;
use kartik\grid\GridView;
use kartik\export\ExportMenu;
use frontend\modules\stock\models\Sellitem;
use dosamigos\datepicker\DatePicker;
use dosamigos\datepicker\DateRangePicker;
use kartik\form\ActiveForm;
/* #var $this yii\web\View */
/* #var $searchModel frontend\modules\stock\models\SellitemSearch */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Stock';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="sellitem-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<div class="row">
<div class="form-group">
<div class="col-xs-4 col-sm-4 col-lg-4">
<label for="upc" class="control-label"><p class="text-info">Product Code <i class="icon-star"></i></p></label>
<input type="text" class="form-control" id="upc" class="span3">
</div>
<div class="col-xs-4 col-sm-4 col-lg-4">
<label for="upc" class="control-label"><p class="text-info">Start Date <i class="icon-star"></i></p></label>
<?= DatePicker::widget([
//'label' => 'Startdate',
'name' => 'startdate',
'id' => 'startdate',
//'value' => '02-16-2012',
'template' => '{addon}{input}',
'clientOptions' => [
'autoclose' => true,
'todayHighlight' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
</div>
<div class="col-xs-4 col-sm-4 col-lg-4">
<label for="upc" class="control-label"><p class="text-info">End Date <i class="icon-star"></i></p></label>
<?= DatePicker::widget([
//'label' => 'Startdate',
'name' => 'enddate',
'id' => 'enddate',
//'value' => '02-16-2012',
'template' => '{addon}{input}',
'clientOptions' => [
'autoclose' => true,
'todayHighlight' => true,
'format' => 'yyyy-mm-dd'
]
]);?>
</div>
</div>
</div>
<p>
<div class="form-group pull-right">
<button class="btn btn-primary" onclick="getValue()">Seach</button>
</div>
</p>
</div>
<?php
/* start getting the itemid */
$this->registerJs('
function getValue()
{
var uPc = $(".upc").val();
var startDate = $(".startdate").val();
var endDate = $(".enddate").val();
alert(uPc);
}
');
/* end getting the itemid */
?>
The registerJs use some jQuery assigning so seems that the function getValue is not visible by the button call but you could use jQuery for assign the onclick and code.
Assuming your button has a id named yourButton you could do this way
<div class="form-group pull-right">
<button id="yourButton" class="btn btn-primary" >Seach</button>
</div>
$this->registerJs (
"$('#yourButton').on('click', function() {
var uPc = $('#upc').val();
var startDate = $('#startdate').val();
var endDate = $('#enddate').val();
alert(uPc);
});"
);
In your javascript code you have '.upc' '.startdate' '.enddate' this mean that you are looking for class upc, startdate endate ... but in your html there is not this class associated to the input field .. you have id then you should search in jQuery using #udc #startdate ... an so
you can do it using ajax by serializing your data and send it to controll action like
$('body').on('click','#btn_id',function(){
var data = $('form#id').serialize();
$.ajax({
'type' : 'get/post',
'dataType' : 'html/json',
'url' : 'controller/action',
'data' : {variable:data},
success : function(data){
console.log('Data Pass Successfully');
},
error : function(){
console.log('There is an Error....!!!');
}
});
});
I'm using Kartik Select2 Widget ... but the problem is I can't type anything in the search.
I used this code:
`<?= Html::button('حجز موعد ', ['value'=>URL::to('index?r=event/create'), 'class' => 'btn btn-warning btn-block btn-flat','id'=>'creates']) ?>
<?php
Modal::begin([
'options' => [
'id' => 'kartik-modal',
'tabindex' => false
],
]);
modal::end();
?>`
JS Code:
`$(function(){
$(document).on('click','#creates',function(){
var date = new Date().toJSON().slice(0,10);
$.get('?r=event/create',{'date':date},function(data){
$('#modal').modal('show')
.find('#kartik-modal')
.html(data);
});
});
});`
my problem is : the text disabled
If not in modal it works fine !
i want to echo like this
<li>
<img src="img/h4-slide.png" alt="Slide">
<div class="caption-group">
<h2 class="caption title">some_title
</h2>
<h4 class="caption subtitle">Dual SIM</h4>
<a class="caption button-radius" href="some_route"><span class="icon"></span>check</a>
</div>
</li>
here my code for render image carousel :
$slides = [];
foreach (Slide::find()->orderBy('sortOrder')->all() as $slide) {
/** #var $slide common\models\Slide */
$slides[] = [
'content' => Html::img(Yii::$app->urlManager->baseUrl . '/uploads/slide/' . $slide->id . '.jpg'),
'caption' => Html::tag('content-group', $slide->title)."<br>".$slide->body,
];
}
And my carousel :
<div class="slider-area">
<div class="block-slider block-slider4">
<?= Carousel::widget([
'id' => 'bxlider-home',
'items' => $slides,
'options' => [
'class' => 'slide',
'data-interval' => 3000,
],
'controls' => [
'<span class="bx-next fa fa-angle-left"></span>',
'<span class="bx-prev fa fa-angle-right"></span>',
],
]) ?>
</div>
</div>
how to Slide->title, slide->body, and some links can be in class caption-group ?
I think it would be better to create new partial file.
Create a new file called _slider.php
Call $this->render('_slider') inside configuration of the slider. Please check below code.
$slides = [];
foreach (Slide::find()->orderBy('sortOrder')->all() as $slide) {
/** #var $slide common\models\Slide */
$slides[] = [
'content' => $this->render("_slider"),
'caption' => Html::tag('content-group', $slide->title)."<br>".$slide->body,
];
}
You can write html inside _slider.php easily now. Don't need to use Html::beginTag() etc.
Generating Tags
The code generating a tag looks like the following:
<?= Html::tag('p', Html::encode($user->name), ['class' => 'username']) ?>
<p class="username">samdark</p>
$options = ['class' => ['btn', 'btn-default']];
echo Html::tag('div', 'Save', $options);
// renders '<div class="btn btn-default">Save</div>'
Hyperlinks:
<?= Html::a('Profile', ['user/view', 'id' => $id], ['class' => 'profile-link']) ?>
Images:
<?= Html::img('#web/images/logo.png', ['alt' => 'My logo']) ?>
generates
<img src="http://example.com/images/logo.png" alt="My logo" />
Lists:
<?= Html::ul($posts, ['item' => function($item, $index) {
return Html::tag(
'li',
$this->render('post', ['item' => $item]),
['class' => 'post']
);
}]) ?>
Even more?
visit