mercredi 17 février 2021

How to toggle a list element according to its checkbox?

I have a list of items in my React project with checkboxes and I'm unable to toggle the list item's background color according to whether the checkbox is checked. I keep track of a variable (apNumber) when a checkbox has been checked and tried using that to set a value selected=true in my data. It works when I check a box but when unselecting it the item's 'select' value doesn't get set back to false. Here's my code:

const [plots, setPlots] = useState([]);
const [checkedPlots, setCheckedPlots] = useState({})

useEffect(() => {
        getContent('farmPlot', {}).then((data) => {
            if (Array.isArray(data)) {
                data.map((p) => {
                    p.selected = false
                    return p
                })
            }
            setPlots(data);
        });

        let trueValues = Object.keys(checkedPlots).filter(p => checkedPlots[p] === true)

        setPlots(plots.map(plot => trueValues.includes('' + plot.apNumber) ? { ...plot, selected: 
        true } : { ...plot, selected: false }))

    }, [checkedPlots]);

const handleCheckboxSelection = (e) => {
        setCheckedPlots({ ...checkedPlots, [e.target.name]: e.target.checked })
} 

inside return:

                    <ListGroup>
                        {(plots) ? plots.map(p =>
                            <div style={plotRowStyle} key={p.apNumber}>
                                <Form.Check
                                    type='checkbox'
                                    style={checkboxStyle}
                                    value={checkedPlots}
                                    name={p.apNumber}
                                    checked={!!checkedPlots[p.apNumber]}
                                    onChange={(e) => handleCheckboxSelection(e)}
                                />
                                <ListGroup.Item
                                    style=
                                    key={p.apNumber}
                                    action onClick={() => farmPlotClickHandler(p)}>{p.name} - {p.apChar}, {p.area} ha ({p.plantName})
                                </ListGroup.Item> 
                            </div>
                        ) : ''}
                    </ListGroup> 



mardi 16 février 2021

Multiple checkbox with button

What I'm trying to do is like selecting topics what the user likes before it proceeds using the next button but the problem is I don't have any idea on how will I do it with the multiple checkbox. It's like the button is disable at first then it will be enable after the user pick their like topics but all I can do as of now is for just one checkbox. Here are some of the codes that I'm doing, I'm sorry but I only just starting in jquery. Thanks in advance.

HTML

<a href="search.html"><input type="submit" value="Next" id="next" disabled="disabled"></a>

<ul>
  <li>
    <input class="checkbox" type="checkbox" id="chkPlant1" unchecked="checked"/>
    <label for="chkPlant1"><img src="img/plant1.jpg" /></label>
  </li>

  <li>
    <input class="checkbox" type="checkbox" id="chkPlant2" />
    <label for="chkPlant2"><img src="img/plant2.jpg" /></label>
  </li>

  <li>
    <input class="checkbox" type="checkbox" id="chkPlant3" />
    <label for="chkPlant3"><img src="img/plant3.png" /></label>
  </li>

  <li>
    <input class="checkbox" type="checkbox" id="chkPlant4" />
    <label for="chkPlant4"><img src="img/plant4.jpg" /></label>
  </li>
</ul>
<script type="text/javascript">
    
    $(function () {
      $("#chkPlant1").change(function () {
        if($('#chkPlant1').is(':checked')) { 
          $("#next").prop("disabled", false);
        }
        else {
          $("#next").prop("disabled", true);
        }
    });
  });

  </script>



How can I change the way of selecting in a checkbox?

I'm using react and I have a checkbox created with Autocomplete where I select items. It is the same as that provided by material:

The problem is that the function "OnChange" runs only if I select the square. I would like it to work also by selecting the row. How can I do it? Here's my code:

<AutoComplete
          multiple
          id="checkboxes-tags-demo"
          options={props.roles}
          // disableCloseOnSelect
          // getOptionLabel={(options) => `${options}`}
          renderOption={(options, { selected }) => (
            <React.Fragment>
              <Checkbox
                icon={icon}
                checkedIcon={checkedIcon}
                style=
                value={options}
                checked={selected}
                onChange={updateNewRoles}
              />
              {options}
            </React.Fragment>
          )}
          style=
          renderInput={(params) => (
            <TextField
              {...params}
              variant="outlined"
              label=""
              placeholder="Available Roles"
            />
          )}
        />
        <button type="submit" onClick={onSubmitChecked}>
          Save
        </button>






lundi 15 février 2021

How Can I Uncheck a Checkbox When a Modal Closed With Alpine Js?

I'm triyng something with Livewire and Alpine Js. I've a checkbox that is opening a modal when checked:

<div x-data="{ showModal: false }" class="flex flex-col w-full md:w-full px-3 mb-6">
    <input wire:model="projectOptions" x-on:click="showModal = true" name="projectOptions" value="1" type="checkbox" class="opacity-0 absolute">
    <div x-show="showModal"
         x-cloak
         x-transition:enter="transition ease-out duration-100"
         x-transition:enter-start="opacity-0 transform"
         x-transition:enter-end="opacity-100 transform"
         x-transition:leave="transition ease-in duration-300"
         x-transition:leave-start="opacity-100"
         x-transition:leave-end="opacity-0"
         class="fixed inset-0 z-50 bg-black bg-opacity-50 flex items-center justify-center px-4 md:p-0">
        <div @click.away="showModal = false" class="flex flex-col bg-white rounded-xl shadow-lg py-4 w-3/12">
            <div class="flex mb-4 justify-around">
                <h3 class="text-2xl font-semibold">Title</h3>
            </div>
            <div wire:ignore class="editor-layout" id="privacyContract">
                <div x-data
                     x-ref="quillEditor"
                     x-init="toolbarOptions = ['bold', 'italic', {'list': 'bullet'}, 'align'];
                         quill2 = new Quill($refs.quillEditor, 
                         {
                             theme: 'snow',
                             placeholder: 'Content here...',
                             modules: {
                                 toolbar: toolbarOptions
                             }
                         });
                         quill2.on('text-change', function () 
                         { 
                             $dispatch('input', quill2.root.innerHTML);
                         });"
                     wire:model.debounce.2000ms="privacyContract">
                </div>
            </div>
            <div class="flex justify-end mr-4">
                <button wire:loading.attr="disabled" class="">Send</button>
            </div>
        </div>
    </div>
</div>

When I click outside modal it closing (with @click.away) but checkbox stays checked. How can I uncheck that checkbox when I click outside of modal?




how can I put elements of an array in a checkbox?

I am using react hooks and have a dynamically filled array of values ​​after a backend call. How can I then insert these values ​​(which I don't know a priori) in a checkbox?




Android - How to create CheckBox drawable?

When using CheckBox, some Drawable for this "check" icon is created - AnimatedStateListDrawable. Is there any way to create this drawable programmatically without CheckBox? The only thing I've found inside CompoundButton class is this resource - com.android.internal.R.styleable.CompoundButton_button.




EventListener JavaScript multiple Checkbox by class name [duplicate]

I have multiple Buttons ("contact-accordion") inside a PHP for loop. Each button contains a checkbox ("checkbox"), which should change the CSS style of the button element when checked. The following code does not seem to work, why?

<input type="checkbox" class="contact-checkbox" name="test">
var acc = document.getElementsByClassName("contact-accordion");
var checkbox = document.getElementsByClassName("contact-checkbox");
    for (i = 0; i < checkbox.length; i++){
        checkbox[i].addEventListener('change', function() {
            if (this.checked) {
                acc[i].style.color = "red";
                alert("test");
            }
        });
            
    }