Tabbed DataTable Issue

Tabbed DataTable Issue

zgoforthzgoforth Posts: 493Questions: 98Answers: 2

Hello, so I have created a tabbed DataTable that has 8 separate tabs for different categories. All the information/data used to populate the first 7 tabs are from the same source/SharePoint list. The 8th tab has relevant information/data, but the values are different. When I try to draw that data to the 8th tab, I get a bunch of errors saying:

DataTables warning: table id=weeklyweight - Requested unknown parameter 'WeeklyWeight' for row 0, column 1. For more information about this error, please see http://datatables.net/tn/4

I think what could possibly be messing it up is maybe the Name/Title value because that is getting pulled from both lists.

How can I get around this?

Here is my JS I will attach the rest of my snippet in a Fiddle as it is to large to fit in the body of this post:

function loadData() {
    var webUrl = _spPageContextInfo.webabsoluteurl;
    var urls = [webUrl + "/_api/web/lists/getbytitle('ContestInformation')/items?$select=Name/Id,Name/Title,Weight,WeeklyWeight,Steps,WeeklySteps,ExerciseMinutes,WeeklyExerciseMin,StepPoints,MinutePoints&$expand=Name",
            webUrl + "/_api/web/lists/getbytitle('WeeklyWinners')/items?$select=Name/Id,Name/Title,Week,Category,Prize&$expand=Name"
        ];
    
        for (i = 0; i < urls.length; i++) { //for loop to run through the AJAX until all URLs have been reached
            $.ajax({
                url: urls[i],
                method: "GET",
                headers: {
                    "Accept": "application/json; odata=verbose"
                },
                success: function(data) { // success function which will then execute "GETTING" the data to post it to a object array (data.value)
                    console.log(data);
                    if (data.d != null && data.d != undefined && data.d.results.length > 0) {
                        var table = $('#weeklyweight').DataTable();
                        var table1 = $('#overallweight').DataTable();
                        var table2 = $('#weeklysteps').DataTable();
                        var table3 = $('#overallsteps').DataTable();
                        var table4 = $('#weeklyminutes').DataTable();
                        var table5 = $('#overallminutes').DataTable();
                        var table6 = $('#steppoints').DataTable();
                        var table7 = $('#minutepoints').DataTable();
                        var table8= $('#weeklywinners').DataTable();
                        table.rows.add(data.d.results).draw();
                        table1.rows.add(data.d.results).draw();
                        table2.rows.add(data.d.results).draw();
                        table3.rows.add(data.d.results).draw();
                        table4.rows.add(data.d.results).draw();
                        table5.rows.add(data.d.results).draw();
                        table6.rows.add(data.d.results).draw();
                        table7.rows.add(data.d.results).draw();
                        table8.rows.add(data.d.results).draw();
            }
                }
            });
        }
    }

$(document).ready( function () {
    function filterGlobal () {
        $('#weeklyweight').DataTable().search(
            $('#global_filter').val()
        ).draw();
        $('#overallweight').DataTable().search(
            $('#global_filter').val()
        ).draw();
        $('#weeklysteps').DataTable().search(
            $('#global_filter').val()
        ).draw();
        $('#overallsteps').DataTable().search(
            $('#global_filter').val()
        ).draw();
        $('#weeklyminutes').DataTable().search(
            $('#global_filter').val()
        ).draw();
        $('#overallminutes').DataTable().search(
            $('#global_filter').val()
        ).draw();
        $('#steppoints').DataTable().search(
            $('#global_filter').val()
        ).draw();
        $('#minutepoints').DataTable().search(
            $('#global_filter').val()
        ).draw();
        $('#weeklywinners').DataTable().search(
            $('#global_filter').val()
        ).draw();
    }
      
      var table = $('#weeklyweight').DataTable({
            "columns": [
                { "data": "Name.Title" },
                { "data": "WeeklyWeight", render : $.fn.dataTable.render.number( ',', '.', 2, '', '%' ) }
            ],
            "order": [[ 1, "desc" ]]
      });
      var table1 = $('#overallweight').DataTable({
            "columns": [
                { "data": "Name.Title" },
                { "data": "Weight", render : $.fn.dataTable.render.number( ',', '.', 2, '', '%' ) }
            ],
        "order": [[ 1, "desc" ]]
      });    
      var table2 = $('#weeklysteps').DataTable({
            "columns": [
                { "data": "Name.Title" },
                { "data": "WeeklySteps" }
            ],
            "order": [[ 1, "desc" ]]   
      });
      var table3 = $('#overallsteps').DataTable({
            "columns": [
                { "data": "Name.Title" },
                { "data": "Steps" }
            ],
            "order": [[ 1, "desc" ]]   
      });
      var table4 = $('#weeklyminutes').DataTable({
            "columns": [
                { "data": "Name.Title" },
                { "data": "WeeklyExerciseMin" }
            ],
            "order": [[ 1, "desc" ]]
      });
      var table5 = $('#overallminutes').DataTable({
            "columns": [
                { "data": "Name.Title" },
                { "data": "ExerciseMinutes" }
            ],
            "order": [[ 1, "desc" ]]
      });
      var table6 = $('#steppoints').DataTable({
            "columns": [
                { "data": "Name.Title" },
                { "data": "StepPoints" }
            ],
            "order": [[ 1, "desc" ]]   
      });
      var table7 = $('#minutepoints').DataTable({
            "columns": [
                { "data": "Name.Title" },
                { "data": "MinutePoints" }
            ],
            "order": [[ 1, "desc" ]]  
      });
      var table7 = $('#weeklywinners').DataTable({
        "columns": [
            { "data": "Week"},
            { "data": "Name.Title" },
            { "data": "Category" },
            { "data": "Prize"}
        ],
        "order": [[ 0, "desc" ]]  
      });

      $('input.global_filter').on( 'keyup click', function () {
        filterGlobal();
      } );
      loadData();
    } );

Fiddle: https://jsfiddle.net/4pb3cfrs/

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 21,178Questions: 26Answers: 4,923

    This code is using the same dataset for each table:

                            var table = $('#weeklyweight').DataTable();
                            var table1 = $('#overallweight').DataTable();
                            var table2 = $('#weeklysteps').DataTable();
                            var table3 = $('#overallsteps').DataTable();
                            var table4 = $('#weeklyminutes').DataTable();
                            var table5 = $('#overallminutes').DataTable();
                            var table6 = $('#steppoints').DataTable();
                            var table7 = $('#minutepoints').DataTable();
                            var table8= $('#weeklywinners').DataTable();
                            table.rows.add(data.d.results).draw();
                            table1.rows.add(data.d.results).draw();
                            table2.rows.add(data.d.results).draw();
                            table3.rows.add(data.d.results).draw();
                            table4.rows.add(data.d.results).draw();
                            table5.rows.add(data.d.results).draw();
                            table6.rows.add(data.d.results).draw();
                            table7.rows.add(data.d.results).draw();
                            table8.rows.add(data.d.results).draw();
    

    DataTables warning: table id=weeklyweight - Requested unknown parameter 'WeeklyWeight' for row 0, column 1. For more information about this error, please see http://datatables.net/tn/4

    Sounds like your JSON response (data.d.results) doesn't contain the property WeeklyWeight. Start by looking at your JSON response using the browser's network inspector tool.

    Kevin

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    So both responses in my Network tab of the Dev Tools, are

    items$select=Name/Id,Name/Title,Weight,WeeklyWeight,Steps,WeeklySteps,ExerciseMinutes,WeeklyExerciseMin,StepPoints,MinutePoints&$expand=Name
    

    and the other is

    items?$select=Name/Id,Name/Title,Week,Category,Prize&$expand=Name
    

    And when I click on both of these request to view the response, they are both stored as data.d.results but carry different values/items

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    Going to try this, what do you think.

    Instead of an AJAX call, use a fetch then a promise

    function loadData(source, url) {
      return fetch(url, { headers: { accept: "application/json; odata=verbose" } }) // make request
        .then((r) => {
          if (!r.ok) throw new Error("Failed: " + url);  // Check for errors
          return r.json();  // parse JSON
        })
        .then((data) => data.d.results) // unwrap to get results array
        .then((results) => {
          results.forEach((r) => (r.source = source)); // add source to each item
          return results;
        });
    }
    window.addEventListener("load", function () {
      Promise.all([
        loadData("List1", _spPageContextInfo.webAbsoluteUrl + "_api/web/lists/getbytitle('ContestInformation')/items?$select=Name/Id,Name/Title,Weight,WeeklyWeight,Steps,WeeklySteps,ExerciseMinutes,WeeklyExerciseMin,StepPoints,MinutePoints&$expand=Name"),
        loadData("List2", _spPageContextInfo.webAbsoluteUrl + "_api/web/lists/getbytitle('WeeklyWinners')/items?$select=Name/Id,Name/Title,Week,Category,Prize"),
      ])
        .then(([r1, r2]) => {
          const objItems = r1;
          const objItems1= r2;
    
  • kthorngrenkthorngren Posts: 21,178Questions: 26Answers: 4,923
    Answer ✓

    Are you making two requests?

    If so you need to populate the proper Datatables to match the request. Otherwise you are overwriting the first response with the second. Which is failing because they are different structures for different tables.

    It looks like you pasted the request. What I wanted is for you to look at the response.

    Kevin

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    @kthorngren Here is an example of the response from the first request:

    {
        "d": {
            "results": [
                {
                    "Name": {
                        "Id": 9,
                        "Title": "Lionel Messi"
                    },
                    "Weight": "0.28",
                    "Steps": null,
                    "ExerciseMinutes": 2289,
                    "WeeklySteps": null,
                    "WeeklyExerciseMin": 399,
                    "WeeklyWeight": "0.00",
                    "StepPoints": null,
                    "MinutePoints": 660
                },
    

    Here is an example from the second request:

                {
                    "d": {
                        "results": [
                            {
                                "Name": {
                                    "Id": 61,
                                    "Title": "Cristiano Ronaldo"
                                },
                                "Prize": "Gift Card",
                                "Week": "Week 01",
                                "Category": "Weight Loss %"
                            },
    
  • kthorngrenkthorngren Posts: 21,178Questions: 26Answers: 4,923
    Answer ✓

    Ok. So either make two separate requests (not in a loop) or on the first iteration populate the 7 Datatables and on the second populate the 8th.

    Kevin

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    So instead of what I have in the OP something like this?

    function loadData() {
        var webUrl = _spPageContextInfo.webabsoluteurl;
        var urls = [webUrl + "/_api/web/lists/getbytitle('ContestInformation')/items?$select=Name/Id,Name/Title,Weight,WeeklyWeight,Steps,WeeklySteps,ExerciseMinutes,WeeklyExerciseMin,StepPoints,MinutePoints&$expand=Name",
                webUrl + "/_api/web/lists/getbytitle('WeeklyWinners')/items?$select=Name/Id,Name/Title,Week,Category,Prize&$expand=Name"
            ];
                $.ajax({
                    url: urls[0],
                    method: "GET",
                    headers: {
                        "Accept": "application/json; odata=verbose"
                    },
                    success: function(data) { // success function which will then execute "GETTING" the data to post it to a object array (data.value)
                        console.log(data);
                        if (data.d != null && data.d != undefined && data.d.results.length > 0) {
                            var table = $('#weeklyweight').DataTable();
                            var table1 = $('#overallweight').DataTable();
                            var table2 = $('#weeklysteps').DataTable();
                            var table3 = $('#overallsteps').DataTable();
                            var table4 = $('#weeklyminutes').DataTable();
                            var table5 = $('#overallminutes').DataTable();
                            var table6 = $('#steppoints').DataTable();
                            var table7 = $('#minutepoints').DataTable();
                            table.rows.add(data.d.results).draw();
                            table1.rows.add(data.d.results).draw();
                            table2.rows.add(data.d.results).draw();
                            table3.rows.add(data.d.results).draw();
                            table4.rows.add(data.d.results).draw();
                            table5.rows.add(data.d.results).draw();
                            table6.rows.add(data.d.results).draw();
                            table7.rows.add(data.d.results).draw();
                }
                    }
                });
                $.ajax({
                    url: urls[1],
                    method: "GET",
                    headers: {
                        "Accept": "application/json; odata=verbose"
                    },
                    success: function(data) { // success function which will then execute "GETTING" the data to post it to a object array (data.value)
                        console.log(data);
                        if (data.d != null && data.d != undefined && data.d.results.length > 0) {
                            var table8= $('#weeklywinners').DataTable();
                            table8.rows.add(data.d.results).draw();
                }
                    }
                });
            
        }
    
  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    Hm I posted the reply, but it didn't go through?

    Something like this?

    function loadData() {
        var webUrl = _spPageContextInfo.webabsoluteurl;
        var urls = [webUrl + "/_api/web/lists/getbytitle('ContestInformation')/items?$select=Name/Id,Name/Title,Weight,WeeklyWeight,Steps,WeeklySteps,ExerciseMinutes,WeeklyExerciseMin,StepPoints,MinutePoints&$expand=Name",
                webUrl + "/_api/web/lists/getbytitle('WeeklyWinners')/items?$select=Name/Id,Name/Title,Week,Category,Prize&$expand=Name"
            ];
                $.ajax({
                    url: urls[0],
                    method: "GET",
                    headers: {
                        "Accept": "application/json; odata=verbose"
                    },
                    success: function(data) { // success function which will then execute "GETTING" the data to post it to a object array (data.value)
                        console.log(data);
                        if (data.d != null && data.d != undefined && data.d.results.length > 0) {
                            var table = $('#weeklyweight').DataTable();
                            var table1 = $('#overallweight').DataTable();
                            var table2 = $('#weeklysteps').DataTable();
                            var table3 = $('#overallsteps').DataTable();
                            var table4 = $('#weeklyminutes').DataTable();
                            var table5 = $('#overallminutes').DataTable();
                            var table6 = $('#steppoints').DataTable();
                            var table7 = $('#minutepoints').DataTable();
                            table.rows.add(data.d.results).draw();
                            table1.rows.add(data.d.results).draw();
                            table2.rows.add(data.d.results).draw();
                            table3.rows.add(data.d.results).draw();
                            table4.rows.add(data.d.results).draw();
                            table5.rows.add(data.d.results).draw();
                            table6.rows.add(data.d.results).draw();
                            table7.rows.add(data.d.results).draw();
                }
                    }
                });
                $.ajax({
                    url: urls[0],
                    method: "GET",
                    headers: {
                        "Accept": "application/json; odata=verbose"
                    },
                    success: function(data) { // success function which will then execute "GETTING" the data to post it to a object array (data.value)
                        console.log(data);
                        if (data.d != null && data.d != undefined && data.d.results.length > 0) {
                            var table8= $('#weeklywinners').DataTable();
                            table8.rows.add(data.d.results).draw();
                }
                    }
                });
            
        }
    
  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    I fixed it B)

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    One more issue I am dealing with (only applies from the first list/first 7 tabs). For the first tab, it is posting everyones name, even if the weeklyweight value is null. Then for the following tabbed tables the corresponding field/value it is posting with it (i.e overallweight, weeklysteps, etc.) if the value is null it still posts everyones names with empty columns beside it. Can I render out the null values with names?

  • zgoforthzgoforth Posts: 493Questions: 98Answers: 2

    For each corresponding "table.rows.add" would I do something along the lines of

    success: function(data) { // success function which will then execute "GETTING" the data to post it to a object array (data.value)
                        console.log(data);
                        if (data.d != null && data.d != undefined && data.d.results.length > 0) {
                            var table = $('#weeklyweight').DataTable();
                            var table1 = $('#overallweight').DataTable();
                            var table2 = $('#weeklysteps').DataTable();
                            var table3 = $('#overallsteps').DataTable();
                            var table4 = $('#weeklyminutes').DataTable();
                            var table5 = $('#overallminutes').DataTable();
                            var table6 = $('#steppoints').DataTable();
                            var table7 = $('#minutepoints').DataTable();
                            if(weeklyWeight !== null){
                                table.rows.add(data.d.results).draw();
                            } else {}
                            table1.rows.add(data.d.results).draw();
                            table2.rows.add(data.d.results).draw();
                            table3.rows.add(data.d.results).draw();
                            table4.rows.add(data.d.results).draw();
                            table5.rows.add(data.d.results).draw();
                            table6.rows.add(data.d.results).draw();
                            table7.rows.add(data.d.results).draw();
                        }
                    }
                });
    
  • kthorngrenkthorngren Posts: 21,178Questions: 26Answers: 4,923

    Questions duplicated in this thread.

    Kevin

This discussion has been closed.