1

I have a jquery datatable with checkboxes in first column. For each checkbox select/deselect I want to update a status field of corresponding row in database. I could attach click event handler to the checkboxes. With each checkbox click, I want to

  1. mark the row containing a checked checkbox as selected.
  2. Get the row data associated with clicked checkbox.

The code I have done so far:

$('#newItemBasketTab').dataTable({
    "fnDrawCallback": function( oSettings ) {
       $('.ibchkclass').click(function() 
       {
          updateItemBasket(this);
       });
    },

    "aaData": result.itembasketdata,
    "aoColumnDefs":
    [
       {"aTargets": [0],"width": "5%","sClass": "centre_class","mData": "item_basket_id",
        "mRender": function ( data, type, full ){
          var checkedStatus = '';
          if(data != null) { checkedStatus = "checked"; }
          return "<input type='checkbox' class=\"ibchkclass\" id=\"chk_" + full.id1 + "_" + full.id2 + "\" " + checkedStatus + ">";
         }
       },
       {"aTargets": [1],"width": "35%","mData": "name"},
       {"aTargets": [2],"width": "10%","sClass": "price_class", "mData": "prod_quantity"},
       {"aTargets": [3],"width": "10%", "sClass": "price_class", "mData": "prod_value"}
    ]
});

function updateItemBasket(cb)
{
    var cbid = cb.id;
    if( $('#'+cbid).is(':checked') == true)
    {
        alert('true');
        //Select respective row
        //Get item_basket_id, prod_quantity and prod_value
    }
    else
    {

    }
}

Thanks.

1
  • Can you clarify the HTML code with an example of this please? Commented Jun 24, 2015 at 8:48

1 Answer 1

1

use closest() $('#'+cbid).closest("tr"); , you can get the current parent tr when the check box is checked using closest function

function updateItemBasket(cb)
{
    var cbid = cb.id;
    if( $('#'+cbid).is(':checked') == true)
    {
        alert('true');
       var tr=$('#'+cbid).closest("tr"); // get selected tr
        //Select respective row
        //Get item_basket_id, prod_quantity and prod_value
    }
    else
    {

    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. I modify as: var clrow = $('#'+cbid).closest("tr"); var aData = table.fnGetData(clrow); alert(aData.prod_quantity );
I also want to know whether I can I add a style class to tr variable?
ya we can add using tr.addClass("className") or $('#'+cbid).closest("tr").addClass("className")
I add statement : clrow.addClass("selected_row_class"); and I have this in <style> tag. tr.selected_row_class { background-color:red; }. But color does not change when checkbox is checked.
you have to remove and add class based on checkbox change

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.