Pre selected rows

Pre selected rows

edumorganedumorgan Posts: 9Questions: 0Answers: 0
edited August 2011 in General
Hi, someone have a example how load the table with pre selected rows from a database?

Thanks
Regards
Eduardo

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    I don't have an example of that, but one way of doing it is simply to call the click function on the rows you want selected after the table has been drawn ( $('tbody tr:eq(0)').click() for example would select the first row).

    Allan
  • edumorganedumorgan Posts: 9Questions: 0Answers: 0
    Thanks for you reply, this is my code, but i want to call the database and pre select the table, i tried the code but didnt work:

    [code]
    function InitTable() {
    /* Init the table */
    oTable = $("#tblData").dataTable({
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "Data.ashx",
    "sPaginationType": "full_numbers",
    "fnServerData": function (sSource, aoData, fnCallback) {
    $.ajax({
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": fnCallback
    });
    },
    "aaSorting": [[1, "desc"]],
    "aoColumns": [null, null, null, { "bSortable": false, "sWidth": "15px"}],
    "fnRowCallback": function (nRow, aData, iDisplayIndex) {
    $('#tblData tbody tr').each(function () {
    if (jQuery.inArray(aData[0], selected) != -1) {
    $(this).addClass('row_selected');
    }
    });
    return nRow;
    },
    "fnDrawCallback": function (oSettings) {
    $('#tblData tbody tr').each(function () {
    var iPos = oTable.fnGetPosition(this);
    if (iPos != null) {
    var aData = oTable.fnGetData(iPos);
    if (jQuery.inArray(aData[0], selected) != -1)
    $(this).addClass('row_selected');
    }
    $(this).click(function () {
    var iPos = oTable.fnGetPosition(this);
    var aData = oTable.fnGetData(iPos);
    var iId = aData[0];
    is_in_array = jQuery.inArray(iId, selected);
    if (is_in_array == -1) {
    selected[selected.length] = iId;
    }
    else {
    selected = jQuery.grep(selected, function (value) {
    return value != iId;
    });
    }
    if ($(this).hasClass('row_selected')) {
    $(this).removeClass('row_selected');
    }
    else {
    $(this).addClass('row_selected');
    }
    });

    });
    }
    });


    }
    [/code]
  • semseosemseo Posts: 9Questions: 0Answers: 0
    It maybe a problem with strings and arrays. Perhaps the database is returning string data and the isarray() requires integers. Try adding parseInt() to thecode you have.
    e.g.
    [code]
    if ( jQuery.inArray( parseInt( aData[0] ), gaiSelected ) != -1 ){
    $(nRow).addClass('row_selected');
    }
    [/code]
This discussion has been closed.