deferLoading and Initial Data

deferLoading and Initial Data

fkmbkkfkmbkk Posts: 31Questions: 5Answers: 0

The default initial data for deferLoading is the preloaded HTML.

Can I load the initial data from my Javascript ? like json ?

This question has an accepted answers - jump to answer

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    yes

  • fkmbkkfkmbkk Posts: 31Questions: 5Answers: 0
     <script type="text/javascript">
    
            var dataSet = [
              ["Tiger Nixon", "System Architect", "Edinburgh", "$333.43"],
              ["Garrett Winters", "Accountant", "Tokyo", "$233.323"]
            ];
    
            $(document).ready(function () {
                $('#example').DataTable({
                    data: dataSet,
                    "processing": true,
                    "serverSide": true,
                    "deferLoading": 24,
                    "ajax": {
                        "url": "api/products/GetAllProducts",
                        "type": "POST",
                        dataSrc: "data"
                    },
                    "columns": [
                        { "data": "Id" },
                        { "data": "Name" },
                        { "data": "Category" },
                        { "data": "Price" }
                    ]
                });
            });
    
        </script>
    

    But I get this error : Requested unknown parameter 'Id' for row 0....

    Did I miss something ?

  • fkmbkkfkmbkk Posts: 31Questions: 5Answers: 0

    The reason I want to load my initial data using js is because I do not want to use any server side code.

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    Answer ✓

    You're using the JS Array sourced data (which is what I linked you to), but since you're using an object instead of an array, you need to use an object as the source.

    The columns.data setting will tell DT what index to use for that column within the object its pulled for that row, so if you change your dataSet variable to this, it will work..

    var dataSet = [
        {
            Id: "Tiger Nixon",
            Name: "System Architect",
            Category: "Edinburgh",
            Price: "$333.43"
        },
        {
            Id: "Garrett Winters",
            Name: "Accountant",
            Category: "Tokyo",
            Price: "$233.323"
        }
    ];
    
This discussion has been closed.