Browse Source

Merge pull request #1370 from djhvscf/master

Restore focus of filter-controls after clone headers
文翼 10 years ago
parent
commit
5515ad6238

+ 31 - 5
src/bootstrap-table.js

@@ -1713,10 +1713,12 @@
     };
 
     BootstrapTable.prototype.initSearchText = function () {
-        if (this.options.searchText !== '') {
-            var $search = this.$toolbar.find('.search input');
-            $search.val(this.options.searchText);
-            this.onSearch({currentTarget: $search});
+        if (this.options.search) {
+            if (this.options.searchText !== '') {
+                var $search = this.$toolbar.find('.search input');
+                $search.val(this.options.searchText);
+                this.onSearch({currentTarget: $search});
+            }
         }
     };
 
@@ -1781,7 +1783,9 @@
     BootstrapTable.prototype.fitHeader = function () {
         var that = this,
             fixedBody,
-            scrollWidth;
+            scrollWidth,
+            focused,
+            focusedTemp;
 
         if (that.$el.is(':hidden')) {
             that.timeoutId_ = setTimeout($.proxy(that.fitHeader, that), 100);
@@ -1794,6 +1798,21 @@
             getScrollBarWidth() : 0;
 
         this.$el.css('margin-top', -this.$header.outerHeight());
+
+        focused = $(':focus');
+        if(focused.length > 0) {
+            var $th = focused.parents('th');
+            if($th.length > 0) {
+                var dataField = $th.attr('data-field');
+                if(dataField !== undefined) {
+                    var $headerTh = this.$header.find("[data-field='" + dataField + "']");
+                    if($headerTh.length > 0) {
+                        $headerTh.find(":input").addClass("focus-temp");
+                    }
+                }
+            }
+        }
+
         this.$header_ = this.$header.clone(true, true);
         this.$selectAll_ = this.$header_.find('[name="btSelectAll"]');
         this.$tableHeader.css({
@@ -1802,6 +1821,13 @@
             .html('').attr('class', this.$el.attr('class'))
             .append(this.$header_);
 
+
+        focusedTemp = $('.focus-temp:visible:eq(0)');
+        if(focusedTemp.length > 0) {
+            focusedTemp.focus();
+            this.$header.find('.focus-temp').removeClass('focus-temp');
+        }
+
         // fix bug: $.data() is not working as expected after $.append()
         this.$header.find('th[data-field]').each(function (i) {
             that.$header_.find(sprintf('th[data-field="%s"]', $(this).data('field'))).data($(this).data());

+ 1 - 1
src/extensions/toolbar/README.md

@@ -31,7 +31,7 @@ Use Plugin: [bootstrap-table-toolbar](https://github.com/wenzhixin/bootstrap-tab
 ### idTable
 
 * type: String
-* description: Set the id of the table to create the pop-up form.
+* description: Set the id of the table to create the pop-up form. Required.
 * default: ``
 
 ## Locales

+ 13 - 9
src/extensions/toolbar/bootstrap-table-toolbar.js

@@ -51,8 +51,8 @@
     };
 
     var showAvdSearch = function(pColumns, searchTitle, searchText, that) {
-        if (!$("#avdSearchModal").hasClass("modal")) {
-            var vModal = "<div id=\"avdSearchModal\" class=\"modal fade\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"mySmallModalLabel\" aria-hidden=\"true\">";
+        if (!$("#avdSearchModal" + "_" + that.options.idTable).hasClass("modal")) {
+            var vModal = sprintf("<div id=\"avdSearchModal%s\"  class=\"modal fade\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"mySmallModalLabel\" aria-hidden=\"true\">", "_" + that.options.idTable);
             vModal += "<div class=\"modal-dialog modal-xs\">";
             vModal += " <div class=\"modal-content\">";
             vModal += "  <div class=\"modal-header\">";
@@ -60,7 +60,7 @@
             vModal += sprintf("   <h4 class=\"modal-title\">%s</h4>", searchTitle);
             vModal += "  </div>";
             vModal += "  <div class=\"modal-body modal-body-custom\">";
-            vModal += "   <div class=\"container-fluid\" id=\"avdSearchModalContent\" style=\"padding-right: 0px;padding-left: 0px;\" >";
+            vModal += sprintf("   <div class=\"container-fluid\" id=\"avdSearchModalContent%s\" style=\"padding-right: 0px;padding-left: 0px;\" >", "_" + that.options.idTable);
             vModal += "   </div>";
             vModal += "  </div>";
             vModal += "  </div>";
@@ -72,7 +72,7 @@
             var vFormAvd = createFormAvd(pColumns, searchText, that),
                 timeoutId = 0;;
 
-            $('#avdSearchModalContent').append(vFormAvd.join(''));
+            $('#avdSearchModalContent' + "_" + that.options.idTable).append(vFormAvd.join(''));
 
             $('#' + that.options.idForm).off('keyup blur', 'input').on('keyup blur', 'input', function (event) {
                 clearTimeout(timeoutId);
@@ -81,13 +81,13 @@
                 }, that.options.searchTimeOut);
             });
 
-            $("#btnCloseAvd").click(function() {
-                $("#avdSearchModal").modal('hide');
+            $("#btnCloseAvd" + "_" + that.options.idTable).click(function() {
+                $("#avdSearchModal" + "_" + that.options.idTable).modal('hide');
             });
 
-            $("#avdSearchModal").modal();
+            $("#avdSearchModal" + "_" + that.options.idTable).modal();
         } else {
-            $("#avdSearchModal").modal();
+            $("#avdSearchModal" + "_" + that.options.idTable).modal();
         }
     };
 
@@ -108,7 +108,7 @@
 
         htmlForm.push('<div class="form-group">');
         htmlForm.push('<div class="col-sm-offset-9 col-sm-3">');
-        htmlForm.push(sprintf('<button type="button" id="btnCloseAvd" class="btn btn-default" >%s</button>', searchText));
+        htmlForm.push(sprintf('<button type="button" id="btnCloseAvd%s" class="btn btn-default" >%s</button>', "_" + that.options.idTable, searchText));
         htmlForm.push('</div>');
         htmlForm.push('</div>');
         htmlForm.push('</form>');
@@ -161,6 +161,10 @@
             return;
         }
 
+        if (!this.options.idTable) {
+            return;
+        }
+
         var that = this,
             html = [];