mapping column names to list of string values?

mapping column names to list of string values?

RushdyRushdy Posts: 7Questions: 2Answers: 0

<

table id=”stockList”>
<thead>
<tr>
<th>Product Code</th>
<th>Product Descrip</th>
<th>price </th>
<th>qty</th>
</tr>
</thead>

<

table>

Return data { [“prd123”,”pen”,”£1.5”,”500”], [“prd124”,”pen2”,”£2”,”800”], [“prd125”,”pen”,”£1.5”,”500”], [“prd1297”,”pen”,”£1.5”,”500”]}

How do I set the column names to map my object values (marked in ????? below)

  1. 2. $(document).ready(function () { 3. $("#myTable").DataTable({ 4. "processing": true, 5. "serverSide": true, 6. "filter": false, 7. "orderMulti": false, 8. "ajax": { 9. "url": "/home/LoadData", 10. "type": "POST", 11. "datatype": "json" 12. }, 13. "columns": [ 14. { "data": "??????", "name": " ProductCode ", "autoWidth": true }, 15. { "data": "?????", "name": " ProductDescrip ", "autoWidth": true }, 16. { "data": "?????", "name": " price ", "autoWidth": true }, 17. { "data": "?????", "name": " qty ", "autoWidth": true }, 18. 19. }); 20. }); 21.

Appreciate your help

Answers

  • RushdyRushdy Posts: 7Questions: 2Answers: 0

    <

    table id=”stockList”>
    <thead>
    <tr>
    <th>Product Code</th>
    <th>Product Descrip</th>
    <th>price </th>
    <th>qty</th>
    </tr>
    </thead>

    <

    table>

    Return data { [“prd123”,”pen”,”£1.5”,”500”], [“prd124”,”pen2”,”£2”,”800”], [“prd125”,”pen”,”£1.5”,”500”], [“prd1297”,”pen”,”£1.5”,”500”]}

    How do I set the column names to map my object values (marked in ????? below)

    <script>
    $(document).ready(function () {
    $("#myTable").DataTable({
    "processing": true, 
    "serverSide": true, 
    "filter": false,
    "orderMulti": false, 
    "ajax": {
    "url": "/home/LoadData",
    "type": "POST",
    "datatype": "json"
    },
    "columns": [
    { "data": "??????", "name": " ProductCode ", "autoWidth": true },
    { "data": "?????", "name": " ProductDescrip ", "autoWidth": true },
    { "data": "?????", "name": " price ", "autoWidth": true },
    { "data": "?????", "name": " qty ", "autoWidth": true },
    
    });
    });
    </script>
    

    Appreciate your help

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49

    In your case I don't believe you need to use columns . If your return array is always in the same sequence as your thead, DataTables will map returnArr[x] to associated x column

  • RushdyRushdy Posts: 7Questions: 2Answers: 0

    shouldn't i have to have column name to search on field ?

  • jr42.gordonjr42.gordon Posts: 305Questions: 2Answers: 49

    Yes you are correct. Or you could add them as classes to the th elements. Try it with data removed. Let me know what result is.

  • allanallan Posts: 63,208Questions: 1Answers: 10,415 Site admin

    The columns.name property is only useful for column selection (column-selector). It has no other use and I've thought of dropping it many times (although it can actually be useful as a column selector, so I won't!).

    "data": "??????"

    You have arrays as that data source, so you don't need to define columns.data (assuming the array order is in the column order). It would be worth reading over the data source section of the manual.

    Allan

This discussion has been closed.