简体   繁体   English

将PHP脚本返回的HTML表转换为Ionic框架的ion-list格式

[英]Converting an HTML table return from a PHP script to be in the format of ion-list for the Ionic framework

Currently I am developing an application on the Ionic framework. 目前,我正在开发有关Ionic框架的应用程序。 I have created a PHP file to pull 'Users' from my database and it is currently returning them in an HTML table format. 我已经创建了一个PHP文件来从数据库中提取“用户”,并且当前它以HTML表格式返回它们。

In the services section of the framework I have set up the 'AJAX' call which is pulling this information: 在框架的服务部分中,我设置了“ AJAX”调用,该调用将提取此信息:

.service('getUsers', function($http){
    var Users;
    return {
        getEmps: function(){
            return $http.get('http://#####.#####/#####/#####/getUsers.php').then(function(items) { 
                Users = items.data; return Users; 
            })
        }
    }
})

Then inside my controller I am doing the following: 然后在我的控制器内,我正在执行以下操作:

       getUsers.getEmps().then(function(data) {
       $scope.Employees = data;
          console.log($scope.Employees); 
       });

This successfully returns the table to the console: 这成功将表返回到控制台:

JohnSmith 约翰·史密斯

I am trying to create the a multidimensional array from this. 我试图从中创建一个多维数组。 I tried to do the following: 我尝试执行以下操作:

  var details = $('tr').map(function (i, row) { // creating an Object to return: return { 'First Name': row.cells[0].textContent, 'Last Name': row.cells[1].textContent } // converting the map into an Array: }).get(); console.log(details); 

However I am having no luck returning an array which I can use to pass into the ionic frameworks - ion-list. 但是我没有运气返回一个数组,我可以使用该数组传递给离子框架-ion-list。

You should return your data in JSON format from PHP. 您应该从PHP以JSON格式返回数据。 You can encode any array to JSON using json_encode($array); 您可以使用json_encode($array);任何数组编码为JSON json_encode($array); and print it out. 并打印出来。 Also remember to set the content type in PHP to JSON: header('Content-Type: application/json'); 还记得将PHP中的内容类型设置为JSON: header('Content-Type: application/json'); . Once fetched in JSON format you can access your data like any other JavaScript object, so there's no need to parse tables, etc. If you need to display the data as a table just loop through it using ng-repeat. 一旦以JSON格式获取,您就可以像访问其他任何JavaScript对象一样访问数据,因此无需解析表等。如果需要将数据显示为表,则可以使用ng-repeat遍历它。 Sending the whole HTML table(code) over HTTP is very inefficient. 通过HTTP发送整个HTML表(代码)效率很低。

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

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