How to merge 2 endpoints and display it in 1 table?

How to merge 2 endpoints and display it in 1 table?

boidurjaboidurja Posts: 1Questions: 1Answers: 0

I have 2 tables(endpoints) - users and leaderboard. In users table their are columns user_id and full_name. In leaderboard table there is a user_id column and other columns. I want to display leaderboard table. But instead of user_id I want to show full_name column.

So I will have to take the full_name from the users table. This is my code:

1st ajax call -

var users = [];

$.ajax({
        url: url + "admin/users",
        type: "GET",
        dataType: 'json',
        async: false,
        crossDomain: true,
        success: function (result){
            for (i=0; i < result.data.length; i++) {
                users[i] = result.data[i];
            }
        }
    })

2nd ajax call -

$.ajax({
        url: url + "admin/leaderboard",
        type: "GET",
        dataType: 'json',
        async: false,
        crossDomain: true,
        success: function (result){

            for (i=0; i < result.data.length; i++) {
                var row = document.getElementById("table-body").insertRow(i);
                var cell1 = row.insertCell(0);
                var cell2 = row.insertCell(1);
                
                for (j=0 ; j < users.length ; j++) {

                    if (result.data[i].user_id === users[j]._id) {
                        cell1.innerHTML = users[j].full_name;
                        break;
                    } 
                    else {
                        cell1.innerHTML = 'NA';
                    }
                }

                cell2.innerHTML = result.data[i].points;
            }

            $('#dataTable').DataTable();
        }
    })

The 2nd ajax call occurs only after the 1st ajax call is completed.

Right now I have only 15 rows in the users table. But later on there will be thousands of rows. If there are thousands of rows in the users endpoint then the 1st ajax call will take a long time to complete. It will have detrimental effects to the end user's experience.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    It sounds like it may be easier to do the tweaking on the server-side. Could you create another server end-point that does the data merge there?

    Colin

Sign In or Register to comment.