lundi 1 mai 2017

Move focus to checkbox after _onleave event

I have a textbox with an _onleave event. I check the text in the textbox and generate a messagebox. In most cases focus moves, to whatever was clicked, once the messagebox clears, but not when it was a checkbox. Is there any way I can capture what was clicked that generated the _onleave event so that I can assign focus to it ( or in this case click the checkbox) once _onleave code is completed. I have tried:

Control control = (Control)sender;
control.Select();

Unfortunately, this captures the initial textbox and returns focus to it. Thanks




How to show sql query with checkboxes with ajax and php

i have 3 checkboxes and when i click on it, it should change the sql string and show me a new sql query.

my HTML code:

<div id="#check_article">
<input type="checkbox" value="Iphone">Iphone
<input type="checkbox" value="Samsung">Samsung
<input type="checkbox" value="Nokia">Nokia
</div>

my script:

var arr = [];
$('input[type=checkbox]').change(function () {
        if ($(this).prop("checked")) {
            var value = $(this).val();
            arr.push(value);
        }
        else {
            arr.splice($.inArray($(this).val(),arr),1);
            }
    var new_string = arr.toString();
    console.log(new_string);
});

so when i click on the checkbox my console.log shows me the right value in string. when i uncheck the box it deletes me the value from the string.

now i have to pass it to the server with ajax.

$.ajax({
        url:"query.php",
        method:"POST",
        data: {'qry':new_string},
                success:function(data) {
                ('#result').html(data);
                }
        });

on php side ... i wolud like to search for a checked article with this

$sql = "SELECT * FROM Store WHERE Name LIKE 'mobilePhone' AND Article='....' 
AND Article='....'" AND Article='....'; 

but i realized that i have to format my string. My string is right now : "Iphone,Nokia,Samsung" but i need it to change to AND Article= 'Iphone' AND Article = 'Nokia' AND Article='Samsung'

is this a right way do to it like this or is there a better way ?




Javascript - Unorthodox way to get value of checkbox's checked attribute?

I am currently utilizing a third party web-based application that allows for UI script injection.

Within the page I am trying to inject my UI script into are a list of elements with associated checkboxes. The following are checkboxes generated by the webpage by the original developers, I cannot change these:

<input type="checkbox" value="1" class="selectionCheckbox" checked="checked" onclick="foo(this)"> // For True (Checked)
<input type="checkbox" value="1" class="selectionCheckbox" onclick="foo(this)"> // For False (Unchecked)

I am trying to load their values into my own UI Script's checkboxs after the document is ready. The nature of my script is basically to inject a X/Y Grid of combinations instead of a list.

I have tried:

function loadCheckOne(vobj, objValue) {
    var containerObj = document.getElementById(objValue);
    var row = containerObj.getElementsByClassName("checkbox");
    // row[0] === the above mentioned input element

    if (row[0].checked){
        vobj.setAttribute("checked", "true");
        console.log("Marked.");
    }
    console.log(row[0].checked); // Prints: "undefined"
}

function loadCheckTwo(vobj, objValue) {
    var containerObj = document.getElementById(objValue);
    var row = containerObj.getElementsByClassName("checkbox");
    // row[0] === the above mentioned input element

    if (row[0].hasAttribute("checked")){
        vobj.setAttribute("checked", "true");
        console.log("Marked.");
    }
    console.log(row[0].hasAttribute("checked")); // Prints: "false"
}


function loadCheckThree(vobj, objValue) {
    var containerObj = document.getElementById(objValue);
    var row = containerObj.getElementsByClassName("checkbox");
    // row[0] === the above mentioned input element

    if (row[0].getAttribute("checked")){
        vobj.setAttribute("checked", "true");
        console.log("Marked.");
    }
    console.log(row[0].getAttribute("checked")); // Prints: "null"
}

The problem is all methods return false or null in some fashion, even for checkboxes that are checked at runtime of the script. Not sure where to go from here.




how to check the checkbox when row was clicked in ListView

I have created a custom listView with my adapter. Each row contains Checkbox, TextView, TextView. Checking option is eneble onLong press (it is connected with CAB) I want to set the checkbox check when user click on the row. It simply works but when I return from cab i could enter there one more time. Here is my adapter code

/**
 * Created by Michał on 26.04.2017.
 */

public class RowAdapter extends ArrayAdapter<ProductList>{
    private Context context;
    private int resource;
    private ArrayList<ProductList> data = null;
    private boolean longPress=false;
    RowBeanHolder holder = null;


    public RowAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<ProductList> data) {
        super(context, resource, data);
        this.context=context;
        this.resource=resource;
        this.data=(ArrayList<ProductList>)data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;



        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(resource, parent, false);

            holder = new RowBeanHolder();
            holder.txtTitle=(TextView) row.findViewById(R.id.name);
            holder.indicator=(TextView) row.findViewById(R.id.indicator);
            holder.checkBox=(CheckBox) row.findViewById(R.id.checkBoxDelete);
            row.setTag(holder);
        }
        else
        {
            holder = (RowBeanHolder)row.getTag();
            row.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(holder.checkBox.isChecked()){
                        holder.checkBox.setChecked(false);
                    }
                    else{
                        holder.checkBox.setChecked(true);
                    }

                }
            });
        }

        ProductList object = data.get(position);
        holder.txtTitle.setText(object.getName());
        holder.indicator.setText(object.getToBuy() + "/" + object.getBought());

        if(longPress){
            holder.checkBox.setVisibility(View.VISIBLE);
        }
        else{
            holder.checkBox.setVisibility(View.GONE);
            holder.checkBox.setChecked(false);
        }

        return row;
    }

    static class RowBeanHolder
    {
        TextView txtTitle;
        TextView indicator;
        CheckBox checkBox;

    }

    public void showCheckBox() {
        longPress=true;
        notifyDataSetChanged();
    }

    public void hideCheckBox(){
        longPress=false;

        notifyDataSetChanged();
    }
}




Any operation with Checkbox is giving Errors in Android

I want to detect whether if a checkbox is checked or not.

MainActivity.java

package com.example.vivek.trackblue;

import android.bluetooth.BluetoothAdapter;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import static android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
import static android.bluetooth.BluetoothAdapter.ACTION_REQUEST_ENABLE;



public class MainActivity extends AppCompatActivity{
    final int Request_code = 1;
    BluetoothAdapter  bt = BluetoothAdapter.getDefaultAdapter();
    private static int  flag_bt_state = 0;



        Button button2;
        ImageView cold2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // All local views declared here
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button = (Button)findViewById(R.id.button5);
        final Toast bt_disabled = Toast.makeText(getApplicationContext(), "Bluetooth Disabled", Toast.LENGTH_SHORT);
        button2 = button;
        final ImageView cold = (ImageView)findViewById(R.id.imageView);
        cold2 = cold;
        CheckBox cb1 = (CheckBox)findViewById(R.id.checkbox);

        //Bluetooth Adapter State
        if(bt.isEnabled()) {
                flag_bt_state = 1;
                button.setText("Stop Bluetooth");
            cold.setImageResource(R.color.green);
            }
            else if(!bt.isEnabled()){
                flag_bt_state=0;
                button.setText("Start Bluetooth");
            cold.setImageResource(R.color.red);



}
//Bluetooth Button Click Listener
    button.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {

           if(bt.isEnabled()) {
               flag_bt_state = 1;
               button.setText("Stop Bluetooth");
               cold.setImageResource(R.color.green);
           }
           else if(!bt.isEnabled()){
               flag_bt_state=0;
               button.setText("Start Bluetooth");
               cold.setImageResource(R.color.red);

           }

            if(flag_bt_state==0 ){


                Intent enableBtIntent = new



Intent(ACTION_REQUEST_ENABLE);
                        startActivityForResult(enableBtIntent, Request_code);


                        button.setText("Stop Bluetooth");
                        flag_bt_state=1;
                        cold.setImageResource(R.color.green);


                    }

                    else if(flag_bt_state==1){

                        button.setText("Start Bluetooth");
                        bt.disable();
                        bt_disabled.show();
                        flag_bt_state=0;
                        cold.setImageResource(R.color.red);
                    }
               }
           });

            cb1.setChecked(true);
      // cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
       //     @Override
         //   public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
           //     if(b){
             //       Toast.makeText(getApplicationContext(), "checked", Toast.LENGTH_SHORT).show();
//
  //              }else{
    //                Toast.makeText(getApplicationContext(), "unchecked", Toast.LENGTH_SHORT).show();
      //          }
        //    }
        //});






 }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Toast bt_enabled = Toast.makeText(getApplicationContext(), "Bluetooth Enabled", Toast.LENGTH_SHORT);
        Toast bt_Permission_denied = Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT);
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == Request_code){
            if(resultCode == RESULT_OK){

                bt_enabled.show();
                cold2.setImageResource(R.color.green);
            }
            else
            {
               bt_Permission_denied.show();
                flag_bt_state = 0;
                button2.setText("Start Bluetooth");
                cold2.setImageResource(R.color.red);
            }

        }

}

}

Layoutfile.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:app="http://ift.tt/GEGVYd"
    xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.vivek.trackblue.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">


<ImageView
    android:id="@+id/imageView"
    android:layout_width="155dp"
    android:layout_height="39dp"
    android:src="@color/red"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.034" />

<Button
    android:id="@+id/button5"
    android:layout_width="152dp"
    android:layout_height="40dp"
    android:text="Start Bluetooth"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.034"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintLeft_creator="1" />

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:text="Make Device Discoverable"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.471"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.125" />

</android.support.constraint.ConstraintLayout>

logcat

05-01 18:42:17.017 12720-12720/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.example.vivek.trackblue, PID: 12720
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vivek.trackblue/com.example.vivek.trackblue.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setChecked(boolean)' on a null object reference
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3163)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3273)
                                                       at android.app.ActivityThread.access$1000(ActivityThread.java:219)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1735)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:145)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6959)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setChecked(boolean)' on a null object reference
                                                       at com.example.vivek.trackblue.MainActivity.onCreate(MainActivity.java:100)
                                                       at android.app.Activity.performCreate(Activity.java:6609)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3116)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3273) 
                                                       at android.app.ActivityThread.access$1000(ActivityThread.java:219) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1735) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:145) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:6959) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at java.lang.reflect.Method.invoke(Method.java:372) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

 

when there is no checkbox operations like setOncheckedListener(), .onChecked(), etc, the application runs correctly and displays the checkbox in the Activity. But when I add a method to the checkbox variable the app starts and immediately stops with the message "Unfortunately, TrackBlue stopped responding". Also I am unable to figure out the source of the error.




Implementation of checkbox in angularjs

I like to use checkbox in AngularJs for user's decision. I can't catch checkbox's state change in my AngularJs code.

My html code is

<div ng-show="acceptrequest">
     <label class="switchnmn">
        <input type="checkbox" ng-change="stateChanged()" checked>
        <div class="slidernmn round"></div>
    </label>
    <b style="font-size:15px">I am able to accept customer's any request date.</b>
</div>

AngularJs code is

    $scope.stateChanged = function (    ) {
        if(){ //If it is checked
          $scope.acceptrequest = true;
       }
       else
          $scope.acceptrequest = false;
    }

When checkbox is checked, $scope.acceptrequest should be true and unchecked $scope.acceptrequest should be false. How can I do that?

I implemented checkbox is slider in css

.switchnmn {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switchnmn input {display:none;}

.slidernmn {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slidernmn:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slidernmn {
  background-color: #2196F3;
}

input:focus + .slidernmn {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slidernmn:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slidernmn.round {
  border-radius: 34px;
}

.slidernmn.round:before {
  border-radius: 50%;
}

label, b {
  vertical-align: middle;
}




How to get class array index for getElemetByClass name in JavaScript

everyone. I'm still new using JavaScript, I need your help to fix my problem, I try to disable checkbox after submitted. Because my checkbox value is from DB and the data will show as long as the DB have it so I'm using class="select_DB[]" in my input tag. From my knowledge class is using array index so I want to disable the checkbox that I choose I have to get the array index of the class. So how I can get the array index of the checkbox ? Need you help

//this code for disable checkbox after submit need improve I don't know how to get array index of the class
        function closeCB() {
            document.getElementByClassName("select_DB").disabled = true;
        }
        <tbody>


          <?php
              include "db_connect.php";

              $sql_select = $dt_bas->prepare("SELECT id_pegawai, nm_pegawai, tmp_lahir FROM pegawai");
              $sql_select->execute();

              while($row = $sql_select->fetch(PDO::FETCH_ASSOC)){
                        $id =  $row["id_pegawai"]; // id
                        $nm =  $row["nm_pegawai"]; // employe name
                        $tmp = $row["tmp_lahir"]; // birthday
        ?>
            <tr>
            <td><input type="checkbox" class="select_DB[]" class="select_DB[]" value=<?php echo"$id";?> <?=was_checked($id)?>/></td> <!-- This is the checkbox-->
            <td><?php echo "$id";?></td>
            <td><?php echo "$nm";?></td>
            <td><?php echo "$tmp";?></td>
            <td>
                <a href="#">Ubah</a>
                <a href="#">Detail</a>
            </td>
            </tr>

        <?php    } ?>