nobleclem/jQuery-MultiSelect

Is there any way to be able to load the items in one step, and later in another indicate which ones should be checked?

Closed this issue · 3 comments

I am creating an app in asp.net web forms,

  1. In Code Behind the items are loaded,
  2. The user marks the ones he wants and submits,
  3. Postback occurs; Since I can't mark the items in code behind (at least I haven't seen how to do it), I pass my state variable with the selection to a label and with JQuery I detect when its value changes, in that case the following JS code is executed:
 if ($("#txtSeleccionEstados").text() != "0") {
            //alert($("#txtSeleccionEstados").text());
            var seleccion = $("#txtSeleccionEstados").text().split(',');     
            

            seleccion.forEach(function (selActual) {
                for (var i = 0; i < $('#comboEstadoCliente')[0].length; i++) {
                    if ($('.ms-options-wrap ul li label input')[i].value == selActual) {
                        $('.ms-options-wrap ul li label input')[i].checked = true;
                        $('.ms-options-wrap ul li')[i].className = "default selected";
                    }
                }
            });
            

            let cuentaSels = $("#txtSeleccionEstados").text().split(',').length; 
            $('#ms-list-1 > button > span').text(cuentaSels + ' seleccionados');

            //$('"#txtSeleccionEstados').multiselect('reload');

        }
  #comboEstadoCliente it's my select[multiple] and #txtSeleccionEstados it's my hidden tag with the list of selected items

This works, but only halfway; If after the postback the user continues to select items, even though the previous ones are marked, only the new selection counts when he submits again.

Then, is there any way to be able to load the items in one step, and later in another indicate which ones should be checked?

Thank you very much for your attention and development, I have tried many 'multiselect' controls and I think this is the best

I am not 100% sure what you are attempting to accomplish. I suspect some things are being lost in translation.

Are you saying in asp.net web forms you are unable to set the items in the select list that should be selected? That seems odd and there should be a way to handle that. That is a basic need for a form. Anyway if you need to do selection in javascript you can simple do something like:

$('select[multiple]').val( [1, 2, 3] ); // LOAD SELECTED VALUES INTO NATIVE LIST
$('select[multiple]').multiselect('reload'); // RELOAD JavaScript Multiselect List

If possible; I used a web translator. But the solution you offer is just what I needed. I didn't see that use of "val" in the documentation or in the examples; but that's how much I need.

Thank you very much

No worries. Glad the information I provided was able to resolve your issue.