简体   繁体   中英

Retrieiving individual cell data of a DataTable row in jQuery

I need to make a the last column of the row disabled or enabled based the value present in the previous column of the same row. We have used Datatables to create the table.

I am not able to get the values present in the cells of the row while rendering the data using mRender for comparing. Can anyone please help in getting the values of the row in mRender ?

I tried with iDataRow and also with createdRow and fnRowCallback to make the last column disabled after the creation of table.

I don't know why createdRow is not working in aoColumns .

Please find my code snippet below :

"aoColumns": [{
    "mData": "productID",
    "bSearchable": true,
    "bSortable": true
}, {
    "mData": "barcode",
    "bSearchable": true,
    "bSortable": true
}, {
    "mData": "fulfillmentChannel",
    "bSearchable": true,
    "bSortable": true
}, {
    "mData": "dateAvailable",
    "bSearchable": true,
    "bSortable": true
}, {
    "mData": "stockStatus",
    "bSearchable": true,
    "bSortable": true
}, {
    "mData": "stockAllocation",
    "mRender": function (data, type, full, iDataRow) {
        console.log(iDataRow); 
        <c:if test="${User.permission eq 'All'}">
            return '<input type="text" class="form-control" style="width:100%" id="stockAllocation" validationDescription="<spring:message code="validation.mustBeDigit"></spring:message>" validationRules="^[0-9]+$" name="stockAllocation" value="' + data + '"></input>'; 
        </c:if>
        <c:if test="${User.permission eq 'ReadOnly'}">
            return data;
        </c:if >
    },
    "bSearchable": true,
    "bSortable": true
}],

I want the values of fullfillmentchannell and stockAllocation into mRender write a condition and make the stockAllocaton field disabled.

SOLUTION

You can use full variable to access row's data, for example full['fulfillmentChannel'] or full['stockAllocation'] .

Then you could write in mRender function:

"mRender": function (data, type, full, iDataRow) {
   if(full['fulfillmentChannel'] === 'A' && full['stockAllocation'] === 'B'){
      data = 'Disabled';
   } else {
      data = 'Not disabled';
   }

   return data;
}

DEMO

See this jsFiddle for demonstration of similar example using DataTables 1.10.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM