Kaynağa Gözat

feat:主动设置某个tab为active

jesse 4 yıl önce
ebeveyn
işleme
edba204ea4
1 değiştirilmiş dosya ile 36 ekleme ve 3 silme
  1. 36 3
      src/Form/Tab.php

+ 36 - 3
src/Form/Tab.php

@@ -118,6 +118,39 @@ class Tab
         return $layout;
     }
 
+    /**
+     * Set true for some one tab by title or id.
+     *
+     * @param string $value
+     * @param string $field
+     */
+    public function activeTab(string $value, string $field = 'title')
+    {
+        if ($this->tabs->where($field, $value)->isNotEmpty()) {
+            $this->tabs = $this->tabs->map(function ($item, $key) use ($value, $field) {
+                $item['active'] = $item[$field] === $value;
+
+                return $item;
+            });
+        }
+    }
+
+    /**
+     * Set true for some one tab by key.
+     *
+     * @param int $key
+     */
+    public function activeTabByKey(int $key = 0)
+    {
+        if ($this->tabs->offsetExists($key)) {
+            $this->tabs = $this->tabs->map(function ($item, $itemKey) use ($key) {
+                $item['active'] = $itemKey === $key;
+
+                return $item;
+            });
+        }
+    }
+
     /**
      * Get all tabs.
      *
@@ -159,18 +192,18 @@ class Tab
     if (hash) {
         $('#$elementId .nav-tabs a[href="' + hash + '"]').tab('show');
     }
-    
+
     // Change hash for page-reload
     $('#$elementId .nav-tabs a').on('shown.bs.tab', function (e) {
         history.pushState(null,null, e.target.hash);
     });
-    
+
     if ($('#$elementId .has-error').length) {
         $('#$elementId .has-error').each(function () {
             var tabId = '#'+$(this).closest('.tab-pane').attr('id');
             $('li a[href="'+tabId+'"] i').removeClass('hide');
         });
-    
+
         var first = $('#$elementId .has-error:first').closest('.tab-pane').attr('id');
         $('li a[href="#'+first+'"]').tab('show');
     }