From b77479a681b9913392606b1050dc99dd32f39165 Mon Sep 17 00:00:00 2001 From: LoveOrigami Date: Tue, 24 Apr 2018 14:05:47 +0300 Subject: [PATCH] enh #6 - FormData --- README.md | 28 +++++++++++---- composer.json | 2 +- src/assets/js/kb-modal-ajax.js | 62 ++++++++++++++++++++++++---------- 3 files changed, 67 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 4de1c0c..3187083 100644 --- a/README.md +++ b/README.md @@ -109,11 +109,7 @@ echo ModalAjax::widget([ ``` ### Index View - Update (Multi Modal Mode) -Grid example with data-scenario -```html -Hello -Goodbye -``` + Modal Ajax with events ```php use lo\widgets\modal\ModalAjax; @@ -121,7 +117,6 @@ use lo\widgets\modal\ModalAjax; echo ModalAjax::widget([ 'id' => 'updateCompany', 'selector' => 'a.btn', // all buttons in grid view with href attribute - 'ajaxSubmit' => true, // Submit the contained form as ajax, true by default 'options' => ['class' => 'header-primary'], 'pjaxContainer' => '#grid-company-pjax', @@ -146,6 +141,27 @@ echo ModalAjax::widget([ ] ]); + +//Grid example with data-scenario + +Pjax::begin([ + 'id' => 'grid-company-pjax', + 'timeout' => 5000, +]); + +echo GridView::widget([ + 'dataProvider' => $dataProvider, + 'columns' => [ + ...................... + // Action buttons + // Hello + // Goodbye + ...................... + ], +]); + +Pjax::end(); + ``` diff --git a/composer.json b/composer.json index 5dd4f8b..d7fed42 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "ajax-modal" ], "homepage": "https://github.com/loveorigami/yii2-modal-ajax", - "version": "2.1", + "version": "2.2", "type": "yii2-extension", "license": "MIT", "authors": [ diff --git a/src/assets/js/kb-modal-ajax.js b/src/assets/js/kb-modal-ajax.js index b43ae60..fb9407e 100644 --- a/src/assets/js/kb-modal-ajax.js +++ b/src/assets/js/kb-modal-ajax.js @@ -152,27 +152,53 @@ ModalAjax.prototype.formSubmit = function () { var form = jQuery(this.element).find('form'); var self = this; - // Convert form to ajax submit - jQuery.ajax({ - method: form.attr('method'), - url: form.attr('action'), - data: form.serialize(), - context: this, - beforeSend: function (xhr, settings) { - jQuery(self.element).triggerHandler('kbModalBeforeSubmit', [xhr, settings]); - }, - success: function (data, status, xhr) { - var contentType = xhr.getResponseHeader('content-type') || ''; - if (contentType.indexOf('html') > -1) { - // Assume form contains errors if html - this.injectHtml(data); - status = false; + if (form.attr('method') !== 'GET' && window.FormData !== undefined) { + + // Convert form to ajax submit + jQuery.ajax({ + method: form.attr('method'), + url: form.attr('action'), + data: new FormData(form[0]), + processData: false, + contentType: false, + context: this, + beforeSend: function (xhr, settings) { + jQuery(self.element).triggerHandler('kbModalBeforeSubmit', [xhr, settings]); + }, + success: function (data, status, xhr) { + var contentType = xhr.getResponseHeader('content-type') || ''; + if (contentType.indexOf('html') > -1) { + // Assume form contains errors if html + this.injectHtml(data); + status = false; + } + jQuery(self.element).triggerHandler('kbModalSubmit', [data, status, xhr, this.selector]); } - jQuery(self.element).triggerHandler('kbModalSubmit', [data, status, xhr, this.selector]); - } - }); + }); + } else { + // Convert form to ajax submit + jQuery.ajax({ + method: form.attr('method'), + url: form.attr('action'), + data: form.serialize(), + context: this, + beforeSend: function (xhr, settings) { + jQuery(self.element).triggerHandler('kbModalBeforeSubmit', [xhr, settings]); + }, + success: function (data, status, xhr) { + var contentType = xhr.getResponseHeader('content-type') || ''; + if (contentType.indexOf('html') > -1) { + // Assume form contains errors if html + this.injectHtml(data); + status = false; + } + jQuery(self.element).triggerHandler('kbModalSubmit', [data, status, xhr, this.selector]); + } + }); + } return false; + }; $.fn[pluginName] = function (options) {