samedi 2 janvier 2021

I want to improve this JavaScript code which Filters a list of CheckBoxes by ''value="

I have a code similar to this, in the View of my Asp.Net Core app:

@model Color.Models.AM

<div class="container">
    <div class="card level-3">
        <h4>AM</h4>
        <hr />
        <div class="row">
            <div class="col-md-4">
                <form asp-action="Create">
                    <div>
                        <input id="txt" type="text" value="" />
                        <input id="btn" type="button" value="Search" />
                    </div>
                    <div class="form-group">
                        <input id="cboAll" type="checkbox" value="CheckAll / unCheck" />
                        ALL<br />
                        <div class="wrapper">
                            @foreach (var cm in Model)
                            {
                                <div><input id=@cm.Name type="checkbox" name="selectedCdos" value=@cm.Id class="tipoo" />@cm.Name Roo: @cm.Roo</div>
                            }
                        </div>
                    </div>
                    <div class="form-group">
                        <input type="submit" value="Create" class="btn btn-primary" />
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<style>
    .wrapper {
        width: 500px;
        height: 200px;
        overflow-y: scroll;
        overflow-x: hidden;
    }

    .card {
        background-color: #fff;
        border-radius: 10px;
        margin: 10% auto;
        position: relative;
        padding: 34px;
        color: #444;
        cursor: pointer;
        overflow-x: auto;
    }
        .card.level-3:hover:before {
            box-shadow: 0 0 80px #999999;
        }
</style>

The important thing is simply to understand that, this generates a quantity N of CheckBoxes equal to the quantity of AM entities in my Database, which uses the attribute Id of AM as the value of each CheckBox.

The Model's code is something like this:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace Color.Models
{
    public class AM
    {
        [key]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public int Roo { get; set; }
    }
}

And finally here a pic of what the view looks like: pic of the view

All this works completely fine and without problems, the CheckBoxes that I check are sent to the Controller, which does things with the chosen values.

What I want help with, and I have problems, however, it is with the following JavaScript code that is at the end of the view:

<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script type="text/javascript">
    $(function () {
        $("#cboAll").click(function () {
            if ($(this).is(":checked")) {
                $(".tipoo").prop('checked', true);
            }
            else {
                $(".tipoo").prop('checked', false);
            }
        });
        $("input[id*=btn]").click(function () {
            var searchvalue = $("input[id*=txt]").val();
            if (searchvalue != "") {
                $(".tipoo").each(function () {
                    if ($(this).val() == searchvalue) {
                        $(this).show();
                    }
                    else {
                        $(this).hide();
                        $(this.nextSibling).wrap('<span style="display:none"></style>');
                    }
                })
            }
            else {
                $("#cboAll,.tipoo").show();
            }
        })
    })
</script>

This code generates the ALL Checkbox that selects all the values, and the search button that allows filtering them, but it has the following problems (because basically the javascript code doesn't work the way I would like it to).

1) This filter the Checkboxes by its Value, when I would like it to filter it by Id, or another label from the Checkbox (since what I have in "value= " is what I need to send to controller, while "Id" can be the text which the view displays).

Now let's say I change "value=@cm.Id" to "value=@cm.Name" to test how the search filter works, in that case the following problems arise:

2) The code only filters the data under exact match, when that isn't what I would like.

Pic of the filter working (search button) and showing how it finds me the Checkbox with the value "Pablona"

Pic that shows that if I search for "Pabl" it finds nothing.

If for example write "Mateo" and press the search button; Ok the view only shows me the CheckBox for Mateo, the same if I write Pablo, but this doesn't find me either of the two if I write for example "Mat" or "Pabl"; When I would like that when I search for "Pabl" it shows to me all the CheckBox that contain something written with the sequence of characters "Pabl", the only exception to this is that the script considers spaces, so if I look for Jan, it finds me "Jan Dos", because there is a space separation between the words 'Jan' and 'Dos'. But as I said, the way I would like it to work, is to show me all the CheckBoxes that contain the sequence "Pabl" when I search for "Pabl".

3) The ALL button always selects everything.

The way the JavaScript code works now, is that if I filter the data, and I press ALL, all the Checkboxes are selected, even those which were hidden, when I would like that if click on ALL and the data is filtered, this only select all the Checkboxes, that went through the filter (the not hidden ones).

4) The text of the CheckBox disappears if I click the search button a Second time.

Pic showing this problem, because the code changes the value of a div to hidden, and then doesn't return it to how it was before.

Could someone really help me solve these 4 problems, and improve my Javascript code?

I have looked for alternative Javascript codes on the Internet for do all this, but none include all the functions that I mention, and what I like about the simplicity of my original code is that it allows me an easy control of the data, since it is really easy for me to get the Checkbox value in the controller; If the Div code is like this.




Checkbox list without using submit

if i have this code:

<div><input type="checkbox" name="lista[]" value="3424"> 3424<br><input type="checkbox" name="lista[]" value="3333"> 3333<br></div>

I would like to have the checkbox value in the variable $valore without using any submit button

help me?




if I check one or more checkboxes, then more checkboxes should open. After checking, the record should be store in db in CI.But I m unable to do this?

enter image description here

I have two checkboxes named as inspection_element_id and datatype id. both checkbox values are come from database because they are dynamic. when I click on one inspection_element_id, all checboxes of datatype should open and I check some of them. then I check second checkbox of inspection_element_id, all checkboxes of datatype should open respectively and I check checkboxes of datatype. I want to store data in database in codeigniter like below:

Tbl_inspection:

 |Inspection Element ID| |   Datatype ID  | 
 |  (Parent Checkbox)  | |(Child Checkbox)|
 |_____________________| |________________|
 |         4           | |        3       |
 |         4           | |        5       |
 |         6           | |        8       |
 |         6           | |        12      |

In the table above, when I check 4th checkbox of inspection element, and from datatype I check 3rd and 5th checkbox. after that, I check 6th checkbox of inspection_element_id and from datatype checkboxes, I check 8th and 12th checkbox, it should be store in database like this. Please help me to solve this.

please check my code below:

View:

<?php echo form_open_multipart('inspection/add', array("class" => "form-horizontal")); ?>
  <div>
    <div class="form-group" >
        <label class="control-label col-xs-12 col-sm-3 no-padding-right"
            for="last_name">Assign Inspection:</label>
        <div class="col-xs-12 col-sm-9">
            <div class="clearfix">
                <?php foreach($aInspection as $a)
                {?> 
                    <input type="checkbox" name="inspection_element_id[]" class="checkbox"  value = " 
                    <?php echo $a->id;?>"  />
                    <label><?php echo $a->name;?></label>      
                <?php
                } 
                ?>
            </div>
        </div>
    </div>
</div>
<div class="form-group">
    <label class="control-label col-xs-12 col-sm-3 no-padding-right" for="datatype">Select Datatype: </label><br>
    <div class="col-xs-12 col-sm-9">
      <div class="clearfix">
        <div id="dvDatatype">
        </div>
      </div>
    </div>
  </div>

jQuery / AJAX Code:

 <script type='text/javascript'>
    $(document).ready(function(){
    $(".checkbox").click(function(){
    if($(this).is(':checked'))
    {
    var value=$(".checkbox:checked").val();
    loadPagination(value);
    }
    else
    {
    console.log("NO");
    $('#dvDatatype').empty();
    }
    });
    function loadPagination(pagno){
    console.log('pagno',pagno)
    $.ajax({
    url: '<?=base_url()?>index.php/Inspection/show_inspection/'+pagno,
    type: 'get',
    dataType: 'json',
    data:{pagno:pagno},
    success: function(data){
    createTable(data);
    }
    });
    }
    function createTable(result)
    {
    console.log(result);
    for(index in result)
    {
    console.log(index); 
    var id = result[index].id;
    var ins_id = result['inspection_id'];
    var datatype=result[index].datatype_name;
    var input;
    input+= '<input type="hidden" name="inspection_id[]" value ="'+ins_id+'">';
    input+= '<input type="checkbox" name="datatype_id[]" value ="'+id+'">';
    input+='&nbsp;<label>'+datatype+'</label>';
    input+='<br>';    
    }
    $('#dvDatatype').append(input);
    }
    });
    </script>

Controller:

function show_inspection($pageno)
{
$data['datatype'] = $this->dtModel->get_all_datatype();
$data['inspection_id'] = $pageno;
echo json_encode($data['datatype']);
}
function add()
{   
    $data['designation'] = $this->dModel->get_all_designation();
    $data['aInspection'] = $this->iModel->get_all_inspection_element(); 
    $data['datatype'] = $this->dtModel->get_all_datatype(); 
    $this->load->library('form_validation');
    $this->form_validation->set_rules('designation_id','Designation ID','required');
    
    if($this->form_validation->run())     
    {    

        $checkbox = $this->input->post('inspection_element_id');
        $checkbox1 = $this->input->post('datatype_id');
        foreach ($checkbox as $a) 
        {
            foreach ($checkbox1 as $b)  
            {
            
            $params_checkbox1 = array(
                'inspection_element_id'=>$a,
                'datatype_id'=>$b,
                'frequency'=>$this->input->post('frequency'),
                'inspection_id'=>$inspection_id,
            );
            $this->Tbl_inspection_model->add_inspection_id($params_checkbox1);
            }
        }
        $this->session->set_flashdata('type', 'success');
        $this->session->set_flashdata('msg', 'Inspection Added Successfully!');
       redirect('inspection/index');
    
    }
    else
    {            
        $data['_view'] = 'inspection/add';
        $this->load->view('layouts/main',$data);
    }
}

Model:

function add_inspection_id($params_checkbox1)
{
    $this->db->insert('tbl_inspection',$params_checkbox);
    
}



vendredi 1 janvier 2021

Vue can't check the recently unchecked, checkbox even its binding

I have a list of checkboxes, I want when I click on input 4th all previous boxes being checked and the rest unchecked. I almost did it, but there is an issue that when I back to the those checked boxes and uncheck one of them, its still okay and the next being unchecked, but when again going forward and check another one from those unchecked the recently unchecked item being still unchecked and not changed.

here is the demo, and I think the GIF will describe my issue better. enter image description here

Demo https://jsfiddle.net/j5dpkut8/1/

<script src="https://unpkg.com/vue"></script>

<div id="app">
  <input type="checkbox" v-model="checked[0]" value="0" @change="printState(0)"> checked 0 <br>
  <input type="checkbox" v-model="checked[1]" value="1" @change="printState(1)"> checked 1 <br>
  <input type="checkbox" v-model="checked[2]" value="2" @change="printState(2)"> checked 2 <br>
  <input type="checkbox" v-model="checked[3]" value="3" @change="printState(3)"> checked 3 <br>
  <input type="checkbox" v-model="checked[4]" value="4" @change="printState(4)"> checked 4 <br>
  <input type="checkbox" v-model="checked[5]" value="5" @change="printState(5)"> checked 5 <br>
  <input type="checkbox" v-model="checked[6]" value="6" @change="printState(6)"> checked 6 <br>
  <input type="checkbox" v-model="checked[7]" value="7" @change="printState(7)"> checked 7 <br>
</div>

new Vue({
  el: '#app',
  data: {
    message: 'Hello Vue.js!',
    checked: [
      false,false,false,false,false,false,false,false,
    ],
  },
  methods: {
    printState(x) {
      this.checked = [
      false,
      false,
      false,
      false,
      false,
      false,
      false,
      false,
    ],
    for (let i = 1; i < this.checked.length; i++) {
        if (i <= x) {
          this.checked[i] = true;
        }
      }
     console.log(this.checked);
    
    },
  }
})



Checkboxes in React Hook Form do not display values set through useForm(defaultValues)

I'm converting the forms in an app from Formik to React Hook Form. Some of the forms are of the CRUD variety and contain fields that are strings and booleans. The booleans are represented in the form using checkboxes. The initial values for these strings and booleans come from a database record and are injected into the form using the initialValues / defaultValues feature of Formik / RHF. Everything worked perfectly in RHF version 5.7.2. However, when I upgraded to version 6.14.0 the checkboxes display as unchecked no matter what value is set as the initial value. Here is the RHF example from their website that I modified to reproduce the problem.

import React from 'react';
import Avatar from '@material-ui/core/Avatar';
import Button from '@material-ui/core/Button';
import CssBaseline from '@material-ui/core/CssBaseline';
import TextField from '@material-ui/core/TextField';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Checkbox from '@material-ui/core/Checkbox';
import Link from '@material-ui/core/Link';
import Grid from '@material-ui/core/Grid';
import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';
import Container from '@material-ui/core/Container';
import { useForm, Controller } from 'react-hook-form';

const useStyles = makeStyles((theme) => ({
  paper: {
    marginTop: theme.spacing(8),
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
  },
  avatar: {
    margin: theme.spacing(1),
    backgroundColor: theme.palette.secondary.main,
  },
  form: {
    width: '100%', // Fix IE 11 issue.
    marginTop: theme.spacing(1),
  },
  submit: {
    margin: theme.spacing(3, 0, 2),
  },
}));

export default function SignIn() {
  const classes = useStyles();
  const { register, handleSubmit, control } = useForm({
    defaultValues: { email: 'email@email.net', password: 'password', remember: true },
  });

  return (
    <Container component="main" maxWidth="xs">
      <CssBaseline />
      <div className={classes.paper}>
        <Avatar className={classes.avatar}>
          <LockOutlinedIcon />
        </Avatar>
        <Typography component="h1" variant="h5">
          Sign in
        </Typography>
        <form className={classes.form} noValidate onSubmit={handleSubmit((data) => alert(JSON.stringify(data)))}>
          <TextField
            variant="outlined"
            margin="normal"
            inputRef={register}
            required
            fullWidth
            id="email"
            label="Email Address"
            name="email"
            autoComplete="email"
            autoFocus
          />
          <TextField
            variant="outlined"
            margin="normal"
            inputRef={register}
            required
            fullWidth
            name="password"
            label="Password"
            type="password"
            id="password"
            autoComplete="current-password"
          />
          <FormControlLabel
            control={<Controller as={Checkbox} control={control} name="remember" color="primary" />}
            label="Remember me"
          />
          <Button type="submit" fullWidth variant="contained" color="primary" className={classes.submit}>
            Sign In
          </Button>
          <Grid container>
            <Grid item xs>
              <Link href="#" variant="body2">
                Forgot password?
              </Link>
            </Grid>
            <Grid item>
              <Link href="#" variant="body2">
                {"Don't have an account? Sign Up"}
              </Link>
            </Grid>
          </Grid>
        </form>
      </div>
    </Container>
  );
}

If this code is run under RHF version 5.7.2, the remember checkbox will be checked on the first render. Under RHF version 6.14.0 the checkbox is clear. Is this a problem with the latest version of RHF, or do I need to initialize checkboxes in a different way?




Add optional controls to default dialogs?

How to add checkbox to the folder selection dialog created by the InnoSetup which is shown below in the image?

enter image description here

This is not a custom dialog. It is created by the InnoSetup automatically.




How can I add an agree to terms and conditions checkbox before users can add items to cart on specific pages?

This question may seem long, but on my website, I have add to cart buttons so people can purchase my products. I have 2 sections of products, digital downloads, and physical products. Both products digital and physical have the same add-to-cart buttons.

What I want to do is add the agree to terms checkbox below the add to cart button ONLY for digital products. Is there a way to add this checkbox on a specific page basis that connects with that specific page add to cart button?

I would be happy to show you my website if that helps! Thank you, guys!