3

I am using jquery datatable and data input injson format.

$('#newItemBasketTab').dataTable({
  "aaData": result.itembasketdata,
  "aoColumns": 
  [
     {"mDataProp": "nic5dcodename"},
     {"mDataProp": "asiccprodcodename"},
     {"mDataProp": "unit_name"},
     {"mDataProp": "prod_quantity"},
     {"mDataProp": "prod_value"}
  ]
});

Now I want to place a checkbox in first column of datatable and based on an ID field in json data, the checkbox needs to be checked or unchecked. Is it possible to add html content to datatable?

2 Answers 2

1

You code will look like,

$('#newItemBasketTab').dataTable({
  "aaData": result.itembasketdata,
  "aoColumns": 
  [
     {"mDataProp": "Selection", 
      "fnRender":function(obj, type){
                   if(obj.aData['ID'])
                        return "<input type='checkbox' checked='checked'>"
                   else
                        return "<input type='checkbox'>"
                 }
     }
     {"mDataProp": "nic5dcodename"},
     {"mDataProp": "asiccprodcodename"},
     {"mDataProp": "unit_name"},
     {"mDataProp": "prod_quantity"},
     {"mDataProp": "prod_value"}
  ]
});

You can add any html code that you want to display in fnRender function

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

Comments

0

Use mrender property -

$('#newItemBasketTab').dataTable({
  "aaData": result.itembasketdata,
  "aoColumnDefs": [
    {
      "aTargets": [ 0 ],
      "mData": "ID",
      "mRender": function ( data, type, full ) {
        var checked = "checked";
        if(data) { checked = "checked"; }
        return "<input type='checkbox' checked='" + checked + "'>";
      }
    },
    {"mData": "nic5dcodename"},
    {"mData": "asiccprodcodename"},
    {"mData": "unit_name"},
    {"mData": "prod_quantity"},
    {"mData": "prod_value"}
  ]
});

DOCS

Comments

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.