How to add or modify my jquery for multiple selection to this javascript? -


issue

i want able touch screen , drag finger across surface , select multiple boxes. want same thing mouse, hold down mouse , drag across whatever want select. because of this, thinking implement in jquery/jquery mobile have behavior out of box?

code

here working sample of have working far.

  • built table , able select specific elements color (and ignore another)
  • able select row or colum. able unselect.
  • able select entire table or unselect (by color).

attempts

i tried using jquery ui. specifically, jquery selectable api, breaks existing code , buggy. took @ found here, dependent on desktop approach using shift , control. tried add select attribute <td> elements , use multiple select. didn't think hack work, @ least wanted try it. finally, looked @ stackoverflow , seems wants checkboxes or keyboard.

again, need way able select multiple boxes/grids aka elements in grid touching screen , dragging across whatever wish select or same mouse.

edit question, similar not need. same use case, applied both mouse event + touch events.

any suggestions, clues, hints, or more appreciated have thrown , kitchen sink @ this. feel this.

i combined this answer this one , seems work both on desktop , on mobile (code bit ugly, sorry that).

how works

every <td> on table listens both normal mouse events (up/down/move) , touch events (start/end/move).

on mousedown/touchstart motion becomes "active", selection reset (removing .highlight class) , current event element selected.

the trick in touchmove event: since $(this) refers element touch event started, have see user touching passing event coordinates highlighthoveredobject, select right element.

jsfiddle

javascript

function highlighthoveredobject(x, y) {     $('td').each(function() {       // check if inside boundaries       if (!(           x <= $(this).offset().left || x >= $(this).offset().left + $(this).outerwidth() ||           y <= $(this).offset().top  || y >= $(this).offset().top + $(this).outerheight()       )) {          $(this).addclass('highlight');       }     }); }  // if using jquery mobile replace next line // $("#yourpage").on("pagecreate", function() {  $(document).ready(function() {        var active = false;      $("td").on("mousedown", function(ev) {         active = true;         $(".highlight").removeclass("highlight"); // clear previous selection         ev.preventdefault(); // prevents text selection happening         $(this).addclass("highlight");     });      $("td").on("mousemove", function(ev) {         if (active) {             $(this).addclass("highlight");         }     });      $(document).on("mouseup", function(ev) {         active = false;     });      $("td").on("touchstart", function(ev) {         active = true;         $(".highlight").removeclass("highlight"); // clear previous selection         ev.preventdefault(); // prevents text selection happening         $(this).addclass("highlight");     });      $("td").on("touchmove", function(ev) {         if (active) {             var touch = ev.originalevent.touches[0];             highlighthoveredobject(touch.clientx, touch.clienty);         }     });      $(document).on("touchend", function(ev) {         active = false;     });  }); 

html

<table border="1" width="100%">     <tbody><tr>         <td>1</td>         <td>2</td>         <td>3</td>     </tr>     <tr>         <td>1</td>         <td>2</td>         <td>3</td>     </tr>     <tr>         <td>1</td>         <td>2</td>         <td>3</td>     </tr> </tbody></table> 

css

.highlight { background-color:#ccffcc; } 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -