简体   繁体   English

数据表仅显示来自 jquery ajax 响应的行

[英]Datatable showing only on row from jquery ajax response

I wrote two different functions using jquery, the first function(table()) loop through an object from JSON response in jquery ajax success. I wrote two different functions using jquery, the first function(table()) loop through an object from JSON response in jquery ajax success. Then call the second function(column()) called column with a parameter an element of the array from the object in the first loop.然后调用第二个函数(column()),在第一个循环中使用参数从 object 中的数组元素调用列。 to make it short i expect the result to be rows, with columns of data from the database.简而言之,我希望结果是行,其中包含数据库中的数据列。

 search(); function search(query){ $.ajax({ type:'GET', url:'/getmsg', data:{ "_token":"{{ csrf_token() }}", query:query, }, success:function(response){ table = row(response); $("#content").html(table); console.log(response); }, }); } //ROWS function row(response){ // LOOP TROUGH THE OBJECTS // DISPLAY THE ROWS columns = ""; for(i = 0; i<response.length; i++){ //columns = "<tr>"; object = response[i]; columns += column(object); } console.log(columns); return columns; } //COLUMN function column(object){ // return object["id"]; // output = "<td>"+object['id']+"</td><td>"+object['name']+"</td><td>"+object['email']+"</td>"; obj = Object.values(object); output = "<tr>"; for(i=0; i<obj.length; i++){ output +="<td>"+obj[i]+"</td>"; } output += "</tr>"; return output; }
This is the Table from the HTML 这是 HTML 中的表格
 <table class="table table-bordered" id="transaction-table"> <input type="text" onkeyup="search(this.value)"> <thead> <tr> <th>Ref Id</th> <th>Desc</th> <th>Amount</th> <th>Status</th> <th>Date</th> </tr> </thead> <tbody id="content"> </tbody> <tfoot> <tr> <th>Ref Id</th> <th>Desc</th> <th>Amount</th> <th>Status</th> <th>Date</th> </tr> </tfoot> </table>

Below is the Output (Don't mind the label on the table headers)下面是 Output(不要介意表头上的 label) 在此处输入图像描述

THIS IS WHAT MY CONSOLE.LOG IS OUTPUTING From the following code.这是我的 CONSOLE.LOG从以下代码中输出的内容。

 success:function(response){ table = row(response); $("#content").html(table); console.log(response); },

Result结果在此处输入图像描述

This is how I was able to fix this error.这就是我能够修复此错误的方法。 STEP 1 1. I tried for figure out the where the logical error occurred by trying to display count for of loops in the first function table and it works correctly, which means the error is within the column() function 2. I tried the same way in the second function column() and I figured out it only loops once.1步 1. 我试图通过显示第一个 function 表中的循环计数来找出逻辑错误发生的位置,它工作正常,这意味着错误在 column() function 内 2. 我尝试了同样的方法在第二个 function column() 中,我发现它只循环一次。 this means I point of concentration.这意味着我的注意力集中。 Still, it doesn't show any syntax error.不过,它没有显示任何语法错误。

STEP 2 I started suspecting scoping issues I decided to copy the script in the column() function and paste it in the table() withing the loop.第 2步我开始怀疑范围问题我决定复制 column() function 中的脚本并将其粘贴到 table() 中并使用循环。 This way i'm exacuting the code directly, but you notice the outer loop and the inner loop are having a pointer variable with the same declaration ( i ) which will cause a syntax error.这样我直接执行代码,但是您注意到外循环和内循环具有具有相同声明( i )的指针变量,这将导致语法错误。 i changed the inner loop pointer declaration to ( j ) IT STARTED WORKING PROPERLY.我将内部循环指针声明更改为 (j)它开始正常工作。 The error i have been battling with was scoping error.我一直在与之斗争的错误是范围错误。 unlike JAVA and PHP that i know, i believe js has the most complicated scoping logical errors, so watch out.与我知道的 JAVA 和 PHP 不同,我相信 js 有最复杂的范围逻辑错误,所以要小心。 CONCLUTION In JavaScript there are two types of scope: 1. Local scope 1. Global scope结论在 JavaScript 中有两种类型的 scope: 1. 本地 scope 1. 全局 Z31A1ZFD1410BEA4BEF25D18

According to https://www.w3schools.com/js/js_scope.asp Local JavaScript Variables根据https://www.w3schools.com/js/js_scope.asp本地 JavaScript 变量

Variables declared within a JavaScript function, become LOCAL to the function.在 JavaScript function 中声明的变量变为 function 的本地变量。

Local variables have Function scope: They can only be accessed from within the function.局部变量有 Function scope:它们只能从 function 内部访问。 Example例子

 // code here can NOT use carName function myFunction() { var carName = "Volvo"; // code here CAN use carName }

Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.由于局部变量只在其函数内部被识别,同名变量可以在不同的函数中使用。

Local variables are created when a function starts and deleted when the function is completed.局部变量在 function 启动时创建,在 function 完成时删除。

Global JavaScript Variables全局 JavaScript 变量

A variable declared outside a function becomes GLOBAL.在 function 之外声明的变量变为 GLOBAL。

A global variable has the global scope: All scripts and functions on a web page can access it.全局变量具有全局 scope:web 页面上的所有脚本和函数都可以访问它。 Example例子

 var carName = "Volvo"; // code here can use carName function myFunction() { // code here can also use carName }

I HAD TO CHANGE THE POING VARIABLE NAME FORM i TO j, BELOW IS WHAT MY CODE LOOKS LIKE AFTER DEBUGGING我不得不将 POING 变量名称从 i 更改为 j,下面是我的代码在调试后的样子

 search(); function search(query){ $.ajax({ type:'GET', url:'/getmsg', data:{ "_token":"{{ csrf_token() }}", query:query, }, success:function(response){ table = row(response); $("#content").html(table); console.log(response); }, }); } //ROWS function row(response){ // LOOP TROUGH THE OBJECTS // DISPLAY THE ROWS columns = ""; for(i = 0; i<response.length; i++){ //columns = "<tr>"; object = response[i]; columns += column(object); } console.log(columns); return columns; } //COLUMN function column(object){ // return object["id"]; // output = "<td>"+object['id']+"</td><td>"+object['name']+"</td><td>"+object['email']+"</td>"; obj = Object.values(object); output = "<tr>"; for(j=0; j<obj.length; j++){ output +="<td>"+obj[j]+"</td>"; } output += "</tr>"; return output; }

THE CORRECT OUTPUT正确的 OUTPUT 在此处输入图像描述

CONCLUSION When you call a second function from a function, make sure when declaring a variable you give it a unique name, or declare it with var(explicitly), else you will face a logical error.结论当您从 function 调用第二个 function 时,请确保在声明变量时给它一个唯一的名称,或者使用 var(显式)声明它,否则您将面临逻辑错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM