Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
926 views
in Technique[技术] by (71.8m points)

datatable - How to display data in data table with ajax call without a column name as "Id"

I am running into a weird issue. My Database Table has few records with columns UserId, FirstName, LastName.

On page load I am getting all the data from Database and storing it in a json object.

Now I am making an Ajax call to populate this data into a data table. The code snippet for the same is below

function loadDataTable() {
    dataTable = $('#tblData').DataTable({
        "ajax": {
            "url": "/Individual/IndHome/GetAll",
            "type": "GET",
        },
        "columns": [
            { "data": "userid", "width": "30%" },
            { "data": "firstname", "width": "30%" },
            { "data": "lastname", "width": "30%" },
            {
                "data": "UserId",
                "render": function (data) {
                    return `
                            <div class="text-center">
                                <a href="/Individual/IndHome/Upsert/${data}" class="btn btn-success text-white btnEdit" style="cursor:pointer">
                                    EDIT
                                </a>
                                <a onclick=Delete("/Individual/IndHome/Delete/${data}") class="btn btn-danger text-white" style="cursor:pointer">
                                    <i class="fas fa-trash-alt"></i> 
                                </a>
                            </div>
                           `;
                }, "width": "10%"
            }
        ]
    });

Now the data table only populates First Name and Last Name the UserId Column is just blank. When I change the UserId Column in my Database to just Id. It populates everything correctly. Am I making a mistake or missing out on something.

I read somewhere that Data Table looks for a Column named as "ID" and in my case its "UserId" Can it be an issue ? Because Each and Every table of mine cannot have an ID Column

question from:https://stackoverflow.com/questions/65943299/how-to-display-data-in-data-table-with-ajax-call-without-a-column-name-as-id

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...