ソースを参照

Fix caret position

djhvscf 4 年 前
コミット
dd54d8951d

+ 3 - 1
src/extensions/filter-control/bootstrap-table-filter-control.js

@@ -135,7 +135,9 @@ $.BootstrapTable = class extends $.BootstrapTable {
           UtilsFilterControl.createControls(this, $controlContainer)
         })
         .on('post-header.bs.table', () => {
-          UtilsFilterControl.setValues(this)
+          setTimeout(() => {
+            UtilsFilterControl.setValues(this)
+          }, 2)
         })
         .on('column-switch.bs.table', () => {
           UtilsFilterControl.setValues(this)

+ 19 - 5
src/extensions/filter-control/utils.js

@@ -110,10 +110,6 @@ export function getCursorPosition (el) {
   return -1
 }
 
-export function setCursorPosition (el) {
-  $(el).val(el.value)
-}
-
 export function cacheCaretAndFocus (that) {
   const searchControls = getSearchControls(that)
 
@@ -136,6 +132,24 @@ export function cacheCaretAndFocus (that) {
   })
 }
 
+export function setCaretPosition (elem, caretPos) {
+  try {
+    if (elem) {
+      if (elem.createTextRange) {
+        const range = elem.createTextRange()
+
+        range.move('character', caretPos)
+        range.select()
+      } else {
+        elem.setSelectionRange(caretPos, caretPos)
+      }
+    }
+  }
+  catch (ex) {
+    // ignored
+  }
+}
+
 export function setValues (that) {
   let field = null
   let result = []
@@ -159,7 +173,7 @@ export function setValues (that) {
             // Closure here to capture the field and cursor position
             const closedCallback = () => {
               fieldToFocus.focus()
-              setCursorPosition(fieldToFocus, carretPosition)
+              setCaretPosition(fieldToFocus, carretPosition)
             }
 
             return closedCallback