GalleryWidgetView.php 3.38 KB
<?php
use yii\helpers\Url;
use common\components\GalleryManager;

if (method_exists($model, 'getGalleryLink') && $model->getGalleryLink()) : ?>
    <div class="table-responsive">
        <table class="table table-bordered table-stripped" id="galleryTable">
            <thead>
            <tr>
                <th>
                    <?= Yii::t('app', 'Image') ?>
                </th>
                <th>
                    <?= Yii::t('app', 'Image title') ?>
                </th>
                <th>
                    <?= Yii::t('app', 'Position') ?>
                </th>
                <th>
                    <?= Yii::t('app', 'Actions') ?>
                </th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($model->getGalleryItems() as $imageParam) : ?>
                <tr>
                    <td>
                        <img src="<?= $imageParam[GalleryManager::NAME_IMAGE_PATH] ?>" width="250">
                        <input type="hidden" name="GalleryManager[<?= GalleryManager::NAME_IMAGE ?>][]"
                               class="form-control" value="<?= $imageParam[GalleryManager::NAME_IMAGE] ?>">
                    </td>
                    <td>
                        <input type="text" name="GalleryManager[<?= GalleryManager::NAME_IMAGE_TITLE ?>][]"
                               class="form-control" value="<?= $imageParam[GalleryManager::NAME_IMAGE_TITLE] ?>">
                    </td>
                    <td>
                        <input type="text" name="GalleryManager[<?= GalleryManager::NAME_SORT ?>][]"
                               class="form-control" value="<?= $imageParam[GalleryManager::NAME_SORT] ?>">
                    </td>
                    <td>
                        <button type="button"
                                class="gallery-file-remove btn btn-xs btn-default"
                                title="Удалить файл"
                                data-url="<?= Url::to(['filedelete', 'id' => Yii::$app->getRequest()->get('id', null)]) ?>"
                                data-key="<?= $imageParam[GalleryManager::NAME_IMAGE] ?>">
                            <i class="glyphicon glyphicon-trash text-danger"></i></button>
                        <!--                    <button class="btn btn-white"><i class="fa fa-trash"></i> </button>-->
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>

<?php endif; ?>

<?php
$this->registerJs('

   $(function() {
       
       $(document).on(\'click\', \'.gallery-file-remove\', function() {
           var th = $(this);
           console.log(th);
           
           $.ajax({
               method: \'post\',
               url: th.data(\'url\'),
               data : {
                    key: th.data(\'key\'),
                    ' . Yii::$app->getRequest()->csrfParam . ': \'' . Yii::$app->getRequest()->getCsrfToken() . '\',
               },
               success: function(data) {
                    if (data.error == \'file deleted\') {
                      removeRow(th.closest(\'tr\'));  
                    }
                
               }
       });
       
       
       function removeRow(tr)
       {
            tr.remove();
            if ($(\'#galleryTable tr\').length === 0) {
                $(\'#galleryTable\').remove();
            }
       }
       
    })
   })

');