Estoy trabajando en una función de actualización que usa tom-select ( https://tom-select.js.org/ ).
El proceso es el siguiente: 1- el usuario hace clic en el botón editar 2- se abrirá un modal de arranque y se mostrarán los datos de la fila seleccionada para que pueda actualizarlos.
el problema: después de renderizar el selector, quiero cambiar su valor para seleccionar el valor del elemento seleccionado, probé este código: $('#select-beast-update').val(response.data.book.author_id).change()
pero no funciona, luego intenté usar onInitialize:function() -consulte este enlace en la sección de devoluciones de llamada para obtener más detalles https://tom-select.js.org/docs/ - y tampoco funcionó.
esta es mi función AJAX:
$.ajax({ url: "{{ route('books.edit',['%book%']) }}".replace('%book%',id), type: "GET", success: (response)=>{ $('#book-id-update').val(response.data.book.id) $('#book-name-update').val(response.data.book.name) // $('#book-pdf-update').val(response.data.book.pdf_file) // $('#book-mobi-update').val(response.data.book.mobi_file) let authors = response.data.authors.map(item => { return { value: item.id, text: item.name }; }); var input = document.getElementById('select-beast-update'); new TomSelect(input,{ create: true, plugins: ['change_listener'], sortField: { field: "text", direction: "asc" }, options:authors, }) input.value = response.data.book.author_id; var evt = document.createEvent('HTMLEvents'); evt.initEvent('change', false, true); input.dispatchEvent(evt); $('#select-beast-update').attr('name','author_id') }, error: (e)=>{ Swal.fire('Error','Please Try Again Later','error' ) } }).done(function(){ $('#edit_book_modal').modal('show') }); ``` note: I'm using Laravel as my backend and vanilla JS and bootstrap along with the tom-select. I'm also open to changing tom-select and using other libraries that implement search in its options **other than select 2**.¿Por qué no inicializar TomSelect con el valor seleccionado?
new TomSelect(input,{ create: true, plugins: ['change_listener'], sortField: { field: "text", direction: "asc" }, options:authors, items:[response.data.book.author_id] })