1

So really I have 2 question in 1 here. I am building a site that pulls information from a database table and places it into an HTML table, where I will be able to click a cell and edit it. Im still working on the click and edit yet but I just added a Jquery hover to change the color of a cell when it is hovered over. I am also trying to highlight the date (first cell in the row) and the product (top cell in the column) so when the spreadsheet gets larger and you hover over a cell it is easy to see the product and date also. My thoughts where to assign each cell and id, have the id based on a number to identify where it is in the cell. For example if there were 4 rows and 4 columns the ids would look as follows and then pull the id apart to see what else needed highlighted:

t11 t12 t13 t14
t21 t22 t23 t24
t31 t32 t33 t34
t41 t42 t43 t44

for example if the over was t33, then t31 and t13 would also be highlighted. I coded the id system into the loop and it seems to work fine. One Issue I realized though when looking in to picking the apart was that there will be over 300 rows, and at least 30 columns once everything is added into the database, and both rows and columns can be added at any given time. this means it isnt as simple as looking at t44 because realisticly we could be looking at t25324. My thought to solve this is instead of having t for table, to use a format like r253c24 so that if you pick the id apart you know exactly the row and column. This sniplet of code works great to make the t44 id, but when I try to add the c between the variables the code gets messed up, how do I fix this?

<?php
                if (($a == 1) or ($b == 1)) {

                    $scheduletext.="<th id=t$b$a>".$row[$a]."</th>";

                } else {

                    $scheduletext.="<td id=t$b$a>".$row[$a]."</td>";   

                }
?>

when I try it obviously doesnt work because php then thinks $bc is the variable which is blank so I just end up with r$a. How do I piece them together so it gets outputted correctly when $scheduletext prints in HTML later in the file?

The second part of my question then becomes once I have the id saved into string (idvalue) (see below Jquery code) how do I pick it apart to find out the row and column?

<script type="text/javascript">

    $("td").hover(function(){

        var idvalue=($(this).attr('id'));

        $(this).addClass("highlight");

        $()

    },function(){

        $(this).removeClass("highlight");

    });

</script>

I tried to google the answer but couldnt figure out how to word correctly what I was trying to do to have any success googling. Thank you to everyone in advance for your help!

Brandon
  • 465
  • 3
  • 10
  • 19
  • 2
    You should look into CSS and the `:hover`-selector. There's no need for id's or javascript at all. You can do stuff like: `tr:hover td { ... }` etc. Some info: http://www.w3schools.com/cssref/sel_hover.asp – M. Eriksson Oct 21 '16 at 18:29
  • I am using CSS and started with the hover feature, but is there a way to highlight the fist cell in the row highlighted also and the first cell in the column? Is there a way in CSS to change the cell to be editable on :click ? Keep in mind I will also be figuring out a way to push the value back to the DB after its changed. I believe id be able to push the value back into the database with ajax. – Brandon Oct 21 '16 at 19:01
  • You are asking too many questions at the same time. Break it up and ask one specified question. When that's answered, create a new question with the next issue and so on.. It will be easier for everyone.First question: You can just give the first td a class: `...` and then use: `tr:hover td.first-cell { ... }` or use the pseudo selector `tr:hover td:first-child { ... }` – M. Eriksson Oct 21 '16 at 19:04
  • Im not sure you are totally following what I am saying as far as highlighting the first cell in the row and in the column of the cell i am highlighted over. Take the 4x4 example I used in the original post, if I hover over t43 I also want t13(the product name) and t41(the date) to be highlighted. It might look pointless in such a small sample but imagine a 100 x 100 table and it will make more sense. giving the first cell in every row the same class of "first-cell" wouldnt help in this case would it? Im not sure either of the CSS codes above would help then either? – Brandon Oct 21 '16 at 19:31

2 Answers2

0

I'll try to help with your second question, because I know noting about PHP (Jon neither).

First of all, I think that your idea of highlight is nice, but when the table is large, highlight only one cell is not enough (for example, to see the product). I propose you to highlight all the row and all the column, and even the cell (darker).

To do that, asign classes c1, c2, c3, c4 for the columns; and r1, r2, r3... for the rows. Then, you just have to add the hover option.

The script can be as follows:

<script>

    $('.cell').hover(
        function mouse_in () {
            // items 1 and 2 must be the column and row
            // I would use 0 for 'cell' class

            var selects_rows = $(this.classList.item(1));
            for(var i = 0; i < selects_rows.length; i++){
                selects_rows[i].className += ' with_highlight';
            }

            var selects_columns = $(this.classList.item(2));
            for(var i = 0; i < selects_columns.length; i++){
                selects_columns[i].className += ' with_highlight';
            }
            this.classList.toggle('w_highlight_darker');
        },
        function mouse_out () {

            var selects = $('with_highlight');
            for(var i = 0; i < selects_rows.length; i++){
                selects_rows[i].className = '';
            }

            this.classList.toggle('w_highlight_darker');
    });

</script>

Thanks to this post. Is where I get the way to modify elements' classes.

Community
  • 1
  • 1
mhSangar
  • 457
  • 3
  • 12
  • is this JavaScript? When I try to alert var selects_rows = $(this.classList.item(1)); I just get [Object Object] in the alert? – Brandon Oct 21 '16 at 20:30
  • Yes, it is. But I'm using a library called [JQuery](http://www.w3schools.com/jquery/). You can add it to your HTML with ``. – mhSangar Oct 21 '16 at 20:42
  • @Brandon Use `console.log()` instead of `alert()`, then open the console in your Dev Tools (F12) to see object properties. Alert will just show you `[Object Object]` for any object. Alternatively, you can use `alert(JSON.stringify(object))` to print a string of the object properties and values in the alert prompt. @MarioSanGar he's already using jQuery in his question. –  Oct 21 '16 at 22:50
0

It may be easier to do this using indexes, instead of giving the cells ID's based on columns and rows. For example, you could use this to achieve highlighting the column and row headers when hovering over a cell in the table:

//when hovering over a cell that's not the first cell in the row
$('td:not(:first-child)').hover(
    function() {//on mouse in
    //get the current row number (add one for use with nth-child)
    var row = $(this).closest('tr').index() + 1;
    //add hover class to the row's first cell (date column)
    $('tbody tr:nth-child(' + row + ')').find('td').first().addClass('hover');

    //get the current column number (add one for use with nth-child)
    var col = $(this).index() + 1;
    //add hover class to the header row's corresponding cell
    $('thead th:nth-child(' + col + ')').addClass('hover');
    },
  function() {//on mouse out
    //same as mouse in - gather row index
    var row = $(this).closest('tr').index() + 1;
    //remove hover class from row's first cell
    $('tbody tr:nth-child(' + row + ')').find('td').first().removeClass('hover');

    //same as mouse in - gather cell index in row
    var col = $(this).index() + 1;
    //remove hover class from header row's corresponding cell
    $('thead th:nth-child(' + col + ')').removeClass('hover');
  }
);

You can see it in action on this fiddle: https://jsfiddle.net/8Loeob4o/

Edit: Really didn't need to find the row name first to highlight the first cell... just reviewed the code. I updated the fiddle: https://jsfiddle.net/8Loeob4o/1

Edit 2: Even tidier, and specifically for your html output (https://jsfiddle.net/qLjvvct2/1/):

$("td").hover(
  function(){
    $(this).addClass('highlight').siblings().first().addClass('highlight');
    $('tr:first-child th:nth-child(' + ($(this).index() + 1) + ')').addClass('highlight');
  },
  function(){
    $(this).removeClass("highlight").siblings().first().removeClass('highlight');
    $('tr:first-child th:nth-child(' + ($(this).index() + 1) + ')').removeClass('highlight');
  }
);
  • This seems like it is very close to working for me, im still very new to programming an am not sure if maybe my table HTML isnt the best and that could be the issue? I am seeing some extra highlighting just not exactly where I want it at. I dumped my code with your code added in ( tweeked it to try to get it to work for me. http://jsbin.com/wagosesiyi/edit?html,output also here is a link to the live output so you can see exactly what I am seeing with this jsbin code. http://www.kr3ativedesign.com/GnS/Index.php take note that both the date and product cells are – Brandon Oct 22 '16 at 00:55
  • im still new to this site also, not sure if I tagged you the first time. – Brandon Oct 22 '16 at 00:56
  • @Brandon Check out `Edit 2` in my answer. Also, you normally have to type in the `@` character, then start with `mark` and it should let you select my name from an autocomplete-type list, in order to tag me. If that list doesn't appear, it's probably because it knows I'll get a notification anyway (since you're commenting on my answer). –  Oct 22 '16 at 06:44
  • This is perfect man! Thank you so much! I tried to give you rep for it but cant since im less than 15 rep myself. You are awesome though man! – Brandon Oct 22 '16 at 14:49