浏览代码

Init counter based on the highest form collection child id

Sullivan SENECHAL 10 年之前
父节点
当前提交
7f076ce1be
共有 1 个文件被更改,包括 9 次插入1 次删除
  1. 9 1
      Resources/public/Admin.js

+ 9 - 1
Resources/public/Admin.js

@@ -337,9 +337,17 @@ var Admin = {
         var counters = [];
 
         // Count and save element of each collection
+        var highestCounterRegexp = new RegExp('_([0-9])+$');
         jQuery(subject).find('[data-prototype]').each(function() {
             var collection = jQuery(this);
-            counters[collection.attr('id')] = collection.children().length-1;
+            var counter = 0;
+            collection.children().each(function() {
+                var matches = highestCounterRegexp.exec(jQuery(this).find('.form-control').attr('id'));
+                if (matches && matches[1] && matches[1] > counter) {
+                    counter = matches[1];
+                }
+            });
+            counters[collection.attr('id')] = counter;
         });
 
         jQuery(subject).on('click', '.sonata-collection-add', function(event) {