Jtable Select Rows after Load

w2olves picture w2olves · Jan 14, 2014 · Viewed 9.2k times · Source

Once my Jtable is loaded, a function is triggered that returns ids. I would like to select rows based on this id or have a checkbox checked in each row.

The load works fine and the checkboxes show up. However I am not able to get a handle on the checkboxes to select them programmatically.

Any help is appreciated.

Code for my table creation is like this:

        //Table Definition
        $('#dvChangeOrd').jtable({
            title: 'Change Order Selection (Select all that apply to this request):',
            paging: true,
            pageSize: 5,
            sorting: true,
            defaultSorting: 'ChangeOrd ASC',
            selecting: true, //Enable selecting
            multiselect: true, //Allow multiple selecting
            selectingCheckboxes: true, //Show checkboxes on first column
            recordsLoaded: function (event, data) {
                },
            actions: {
                listAction: 'c013.aspx/LoadLists',
            },

            fields: {
                ChangeOrd: {
                    title: 'Change Order',
                    width: '15%',
                },
                Type: {
                    title: 'Type',
                    width: '25%',
                },
                Status: {
                    title: 'Status',
                    width: '10%',
                },
                ChangeDesc: {
                    title: 'Change Description',
                    width: '50%',
                },
                Amount: {
                    title: 'Amount',
                    width: '20%',
                },
            },
            //Register to selectionChanged event to hanlde events
            selectionChanged: function () {

            },
        });

Once the records are loaded, in the record loaded function possibly, I would like to select the checkboxes with id's matching from Server Response or any other way where I get a set of ids from the server and then check the checkboxes corresponding to it.

Thanks

Answer

Dmitry Ukolov picture Dmitry Ukolov · Mar 25, 2014

Try this:

recordsLoaded: function(event, data) {
  var selectedRows = data.serverResponse.selected_ids.map(function(id) {
    return $('#dvChangeOrd').jtable('getRowByKey', id)[0];
  });
  $('#dvChangeOrd').jtable('selectRows', $(selectedRows));
}

Note: selected_ids returns from server as additional attribute of jTable response:

render json: {Result: 'OK', TotalRecordCount: all_contacts.count, Records: items, selected_ids: selected_ids}