Jolan
Jolan

Reputation: 711

Highlight selected jsGrid row

I found this example which highlights a row after it has been selected but the problem with it is that it keeps the previous row(s) highlighted after another one has been selected.

Here's part of the code

//js
rowClick: function(args) {
        var $row = this.rowByItem(args.item);

      $row.toggleClass("highlight");
    }, 

//css
tr.highlight td.jsgrid-cell {
    background-color: green;
}

I can't find a solution to unhighlight the previously selected row

Upvotes: 3

Views: 8559

Answers (3)

JonhCanedo
JonhCanedo

Reputation: 1

If you came looking for a solution in which only 1 line is selected and which also deselects the same line, here is the solution:

selectedVal = null;

$(document).ready(function(){
jsGrid.Grid.prototype.rowByIndex = function(arg) {
         //this._content.find("tr")[arg] returns a DOM element instead of a jQuery object
         //Pass the DOM element to the find method to get a jQuery object representing it
         return this._content.find(this._content.find("tr")[arg]);
     };
});

rowClick: function (args) {

             selectedVal = args.item;

             let $row = this.rowByItem(args.item);

             if ($row.hasClass("highlight") === false) {
                 //Deseleciona todas as linhas
                 for (let i = 0; i < this.data.length; i++) {
                     this.rowByIndex(i).removeClass("highlight");
                 }
                 $row.toggleClass("highlight");

             } else {
                 selectedVal = null;
                 $row.toggleClass("highlight");

             }
             console.log(selectedVal);
         }

Upvotes: 0

Will Jones
Will Jones

Reputation: 2191

A little late to the party on this one, however the accepted answer by @Narenda didn't completely solve my problem. This may help someone else that stumbles across this later.

If you need a single select only, here's a way of doing it:

Extend the jsGrid plugin with a method to find a row by index:

    jsGrid.Grid.prototype.rowByIndex = function(arg){
        //this._content.find("tr")[arg] returns a DOM element instead of a jQuery object
        //Pass the DOM element to the find method to get a jQuery object representing it
        return this._content.find(this._content.find("tr")[arg]);
    };

Modify the rowClick function in @Narenda's answer:

    rowClick: function ( args ) {
        //Deselect all rows
        for(var i = 0; i<this.data.length; i++){
            this.rowByIndex(i).removeClass("jsgrid-highlight-row");
        }

        //Everything else as per the previous answer
        var $row = this.rowByItem(args.item),
        selectedRow = $("#jsGrid").find('table tr.jsgrid-highlight-row');

        if (selectedRow.length) {
            selectedRow.toggleClass('jsgrid-highlight-row');
        };
        $row.toggleClass("jsgrid-highlight-row");
        //Any other code to run on item click
    }

And add some CSS. This mimics the row hover in the default theme:

    tr.jsgrid-highlight-row td.jsgrid-cell {
         background:#c4e2ff;
         border-color:#c4e2ff;
    }

Upvotes: 6

Narendra Jadhav
Narendra Jadhav

Reputation: 10262

You can achieve by this following steps

  1. First on row click you need to get selected row like this

    var selectedRow = $("#jsGrid").find('table tr.highlight').

  2. Then you can use

    selectedRow.toggleClass('highlight') or selectedRow.removeClass('highlight')

DEMO

$("#jsGrid").jsGrid({
    width: "100%",
    height: "auto",
    paging: false,

    //for loadData method Need to set auto load true
    autoload: true,

    noDataContent: "Directory is empty",

    controller: {
        loadData: function(filter) {
            var data = [{
                nickname: "Test",
                email: "[email protected]"
            }, {
                nickname: "Test 1",
                email: "[email protected]"
            }, {
                nickname: "Test 2",
                email: "[email protected]"
            }, {
                nickname: "Test 3",
                email: "[email protected]"
            }];
            return data;
        }
    },

    rowClick: function(args) {
        var $row = this.rowByItem(args.item),
            selectedRow = $("#jsGrid").find('table tr.highlight');

        if (selectedRow.length) {
            selectedRow.toggleClass('highlight');
        };
        
        $row.toggleClass("highlight");
    },

    fields: [{
        name: "nickname",
        type: "text",
        width: 80,
        title: "Name"
    }, {
        name: "email",
        type: "text",
        width: 100,
        title: "Email Address",
        readOnly: false
    }]
});
tr.highlight td.jsgrid-cell {
  background-color: green;
}
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid-theme.min.css" />
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.3/jsgrid.min.js"></script>


<div id="jsGrid"></div>

Upvotes: 4

Related Questions