Created
October 9, 2018 08:00
-
-
Save fedorovanton/0310aafb52b707720201a80ffd44a82b to your computer and use it in GitHub Desktop.
Асинхронное добавление/удаление записей
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Отправка ajax на удаление записи с id из таблицы mailing_object + удаление строчки tr | |
* @param id | |
*/ | |
function removeRecord(id) { | |
var data = { | |
id: id | |
}; | |
$.ajax({ | |
type: 'POST', | |
url: '/mailing/ajax-remove-object', | |
data: data, | |
success: function(response) { | |
if (response) { | |
$('#mailing-object-id-'+id).remove(); | |
} | |
} | |
}); | |
} | |
/** | |
* Добавление строчки в #table-mailing-objects | |
* | |
* @param objectID | |
* @param mailingObjectID | |
*/ | |
function insertTrInTableMailingObjects(objectID, mailingObjectID) { | |
var data = { | |
object_id: objectID, | |
mailing_object_id: mailingObjectID | |
}; | |
$.ajax({ | |
type: 'POST', | |
data: data, | |
url: '/object/tr-for-mailing', | |
success: function(response) { | |
// добавляем в конец таблицы новую строку | |
$('#table-mailing-objects tr:last').after(response); | |
// вешаем событие onClick на удаление объекта рассылки | |
$('tr#mailing-object-id-'+data.mailing_object_id).on('click', '.ajaxRemoveObject', function(event) { | |
removeRecord(data.mailing_object_id); | |
}); | |
} | |
}); | |
} | |
/** | |
* Добавление объектов в рассылку через ajax | |
*/ | |
$('button.ajaxAddObject').on('click', function(event) { | |
event.preventDefault(); | |
var current = $(this); | |
var mailing_id = current.data('mailing_id'); | |
var object_id = current.data('object_id'); | |
var data = { | |
mailing_id: mailing_id, | |
object_id: object_id | |
}; | |
$.ajax({ | |
type: 'POST', | |
data: data, | |
url: '/mailing/ajax-add-object', | |
success: function(mailingObjectID) { | |
if (mailingObjectID > 0) { | |
current.html('<i class="fa fa-check"></i>'); | |
insertTrInTableMailingObjects(object_id, mailingObjectID); | |
} | |
} | |
}); | |
return false; // for good measure | |
}); | |
/** | |
* Удаление объектов из рассылки через ajax | |
*/ | |
$('button.ajaxRemoveObject').on('click', function(event) { | |
event.preventDefault(); | |
var id = $(this).data('id'); | |
removeRecord(id); | |
return false; // for good measure | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment