1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- (function ($) {
- $.fn.addCollection = function() {
- function requete(chemin, handleData) {
- $.ajax({
- url: chemin,
- type: 'POST',
- dataType: 'json',
- success: function (reponse) {
- let reponseArray = parseJson(reponse);
- handleData(reponseArray);
- }
- })
- }
-
- function parseJson(data) {
- let tableau = [];
- for (let i = 0; i < data.length; i++) {
- let UnObjetJson = data[i];
- let obj = JSON.parse(UnObjetJson);
- sortie = Object.values(obj)
- tableau.push(sortie[0]);
- }
- return tableau;
- }
- function addDeleteLink($prototype) {
-
- let $deleteLink = $('<button type="button" class="btn btn-outline-secondary"><i class="fa fa-trash fa-lg text-danger"></i></button>');
-
-
- $prototype.find(":input").after($deleteLink);
-
-
- $deleteLink.on("click", function(e) {
- $prototype.remove();
-
- e.preventDefault();
- return false;
- });
- }
- this.each(function() {
-
- var chemin = $(this).data('path');
-
- var $container = $($(this).attr("data-list-selector"));
- $li = $container.find('li');
- $li.each(function() {
- $(this).find(':input').prop("readonly", true);
- addDeleteLink($(this));
- })
-
- $(this).on("click", function(e) {
- e.preventDefault();
- var list = jQuery(jQuery(this).attr('data-list-selector'));
-
- var counter = list.data('widget-counter') | list.children().length;
-
- var newWidget = list.attr('data-prototype');
-
-
-
- newWidget = newWidget.replace(/__name__/g, counter);
-
- counter++;
-
- list.data('widget-counter', counter);
-
- var newElem = jQuery(list.attr('data-widget-tags')).html(newWidget);
-
- requete (chemin, function(output) {
- newElem.find(':input').autocomplete({
- source: output
- });
- })
-
- addDeleteLink(newElem);
- newElem.appendTo(list);
- })
- })
- }
- })(jQuery);
|