How can I keep a checkbox always selected?

Viewed 104

There is this modal in which I have a group of checkboxes being generated by the directive x-for from AlpineJS (v2.8.2), and the .sapId checkbox is the one that needs to be always selected no matter what. I partially got what I wanted here using jQuery: $('.sapId').prop('checked', 'checked').attr('disabled', 'disabled');.

This made the checkbox checked and unclickable, and that is exactly what I want. Problem: Whenever I click another checkbox from the .selectColumns list, the .sapId loses the selected status and I can't select it anymore. Till now I've done some searches and tried to do this using jQuery, but would be nice if there was a way of doing via Alpine or Livewire itself.

Here is the modal:

<div x-data="{
        data:columns,
        selectedColumns: [],
    }" 
    wire:ignore class="modal fade" id="selectColumnsModal" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Selecionar Colunas</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <i aria-hidden="true" class="ki ki-close"></i>
                    </button>
                </div>
                <p class="mx-10 mt-4">Selecione até 9 colunas para exibir...</p>
                <div class="text-center mx-10">
                    <div class="input-icon">
                        <input type="text" class="form-control" placeholder="Nome da coluna..." id="searchColumns">
                        <span>
                            <i class="flaticon2-search-1 text-muted"></i>
                        </span>
                    </div>
                </div>
                <div class="modal-body">
                    <div class="mt-6 text-center">
                        <table id="selectColumnsTable" class="table table-hover gy-5">
                            <thead>
                                <th class="text-left">
                                    Coluna
                                </th>
                                <th class="text-left">
                                    <i class="la la-eye"></i>
                                </th>
                            <thead>
                            <tbody>
                                <tr>
                                    <td class="text-left ml-4"> 
                                        <span x-html="columns[1].title"></span> 
                                    </td>
                                    <td class="text-left">
                                        <input x-model="selectedColumns" class="sapId" id="sapId" type="checkbox" :value=columns[1].field>
                                    </td>
                                </tr> 
                                <template x-for="(column, index) in data" :key="index">                          
                                    <tr x-show="column.field != 'id' && column.field != 'sap_id' &&column.title != '' && column.title != 'CÓDIGO'">
                                        <td class="text-left ml-4"> 
                                            <span x-html="column.title"></span> 
                                        </td>
                                        <td class="text-left">
                                            <input x-model="selectedColumns" id="selectColumns" class="selectColumns" type="checkbox" :value=column.field>
                                        </td>
                                    </tr>
                                </template>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer d-flex justify-content-around">
                    <button @click="displaySelected(selectedColumns)" type="button" class="btn btn-primary col-5" data-target="click">Exibir selecionadas</button>
                    <button type="button" class="btn btn-danger col-5" data-dismiss="modal">Cancelar</button>
                </div>
            </div>
        </div>
    </div>

And just in case, this is the jQuery I'm using to control the limit of boxes that can be selected:

$(".selectColumns").change(function () {
            var displayLimit = 8;
            var selected = $('.selectColumns:checked').length;

            if (selected > displayLimit) {
                $('.selectColumns').not(':checked').attr('disabled', 'disabled');
            } else {
                $('.selectColumns').not(':checked').removeAttr('disabled');
            }       
        });

All suggestions are welcome, thanks in advance!

2 Answers

<td class="text-left">
  <input x-model="selectedColumns" class="sapId" id="sapId" type="checkbox" disabled="disabled" :value=columns[1].field checked>
</td>
<label for="checkbox">checkbox</label>

you can simply include "checked" and "disabled" in the input tag.

Finally I got solution for your problem, you can do something like this to limit the number of checkbox checked using apline:

<input x-model="selectedColumns" type="checkbox" :disabled="selectedColumns.lenght > 8 && !$el.checked">

SelectedColumns.length returns the length off the array and $el is a magic property that can be used to retrieve the current DOM node.

And for the .sapId you can do something like this:

<input x-model="selectedColumns" type="checkbox" checked disabled>

But you have to hardcode the value of .sapId in order to keep it checked that how x-model works. x-model allows you to bind the value of an input element to Alpine data.

Related