rstaib / jquery-bootgrid

Nice, sleek and intuitive. A grid control especially designed for bootstrap.
http://www.jquery-bootgrid.com
MIT License
972 stars 362 forks source link

set the rows is selected (selectedRows) in json data #316

Open orhoncms opened 8 years ago

orhoncms commented 8 years ago

I can't set the table rows was selected from the json data.

So I changed the source code to fixed my program. chaged the code in line 602 like this :

// var selected = ($.inArray(row[that.identifier], that.selectedRows) !== -1),

var selected = ($.inArray(row[that.identifier], that.selectedRows) !== -1) || row['checked'],

And in json data:

{
  "current": 1,
  "rowCount": 10,
  "rows": [
    {
      "id": 19,
      "sender": "123@test.de",
      "received": "2014-05-30T22:15:00",
      "checked": true
    },
    {
      "id": 14,
      "sender": "123@test.de",
      "received": "2014-05-30T20:15:00",
      "checked": false
    },
    ...
  ],
  "total": 1123
}

is there some better suggestion to do it ? thanks

Iodine- commented 8 years ago

Hey,

What you have currently would not actually set the row state as checked behind the scenes - it would only appear selected on the front end I believe. You can test that by calling

var mySelectedRows = $('#myGrid').bootgrid('getSelectedRows');
// mySelectedRows = []

You could do this however from your initialization chunk without modifying bootgrid. The idea is to capture the checked property of your rows and then call bootgrid's underlying select method to set everything correctly. An example is below:

var grid = $("#grid").bootgrid({
                ajaxSettings: {
                    method: "GET"
                },
                ajax: true,
                url: 'http://127.0.0.1:8080/demo/data.json',
                selection: true,
                multiSelect: true
            }).on('loaded.rs.jquery.bootgrid', function(){
                var rows = grid.bootgrid('getCurrentRows');
                $.each(rows, function(index, row){
                    if(row.checked){
                        grid.bootgrid('select', [row.id]);
                    }
                })
            });

You could also collect all the selected row id's first and then pass them into the select call all at once in the parameter array. see select method

Always best to try to accomplish what you need without modifying your plugins in my opinion, so I feel this is a more maintainable solution for now.

Feel free to let me know if this works for you or if there is anything else i can do.

--][--