Use ajax to dynamically load the background data json array into the list on several pages of the app made in jquery mobile

Later, I searched for information and found that although the data was loaded after the new node was added, the style was not loaded, so there are two points to note in the whole process.

1. Use loops to dynamically add json to html

2. Dynamically refresh the style of each node.

server.php

<?php
//todo: get data and convert to json
// try{
//     $pdo=new PDO('mysql:host=localhost;dbname=','root','playboy');
//     $sql="select * from user where username=? and password=?";
//     $stmt=$pdo->prepare($sql);
//     $stmt->execute(array($username,$password));
//     echo $stmt->rowCount();

// }catch(PDOException $e){
//     echo $e->getMessage();
// }
//  $pdo=new PDO('mysql:host=localhost;dbname= test_jilv','root','');
$result = array();
try{    
$pdo=new PDO('mysql:host=localhost;dbname=test_jilv','root','playboy');
$pdo->query("set names utf8");

        $sql="select * from my_activity order by id desc";

        $stmt=$pdo->prepare($sql);

        $stmt->execute();

        while($row=$stmt->fetch()){

            $data['title']=$row['title'];
            $data['address']=$row['address'];
            $data['time']=$row['time'];
            //echo json_encode($data);
            $result[] = $data;
            //...
            //...

        }
}catch(PDOException $e){
    echo $e->getMessage();
}
echo json_encode($result);
//echo json_encode($data);

function toUtf($value){
    if($value){
        return icon_to_utf8($value);
    }else{
        return false;
    }
}
?>

2.index,html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewpoint" content="width=device-width,initial-scale=1" />
<link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.2/jquery.mobile.min.css">
<script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/jquerymobile/1.4.2/jquery.mobile.min.js"></script>
<style>
.ui-bar-f
{
color:#30b4ed;
background-color:#30b4ed;
}
.ui-body-f
{
font-weight:bold;
color:#30b4ed;
}
#img{
    margin-top:11px;
    margin-left:3px;
    border-radius:0;
}
</style>
</head>
<body>
<script type="text/javascript">
            $("document").ready(function() {
                var url ="server.php";
                var data={};
                $.getJSON(url,data,function(res){
                            var htmlStr='';
                            for (var i =0; i<res.length; i++) {
                      var o = res[i];
                      htmlStr += '<li>'
                              +  '  <a href="activity_detail.html" data-transition="flip">'
                              +  '    <img src="RAW.PNG"/>'
                              +  '    <h2 id="" name="">' + o['title'] +'</h2>'
                              +  '    <p id="" name="">' + o['address'] +'</p>'
                              +  '    <p id="" name="">' + o['time'] +'</p>'
                              +  '  </a>'
                              +  '</li>';
                    
                    }
                      $('#list').append(htmlStr);
 $('ul').listview('refresh'); //Refresh the node style
                    });
              });
        </script>
<div data-role="page" >
    <div data-role="header" data-position="fixed" data-theme="f">
        <h1></h1>
    </div>
 <ul id="list"data-role="listview"data-inset="true"><!--Do not use the content column, just add the list to the page-->
        
    </ul>
</div>
</body>
</html>

115112_9xCI_1778933.png

Reprinted at: https://www.cnblogs.com/hcxl/p/8321590.html

Intelligent Recommendation

Vue uses jQuery ajax to load data pages without display solution

No data code is We can see that the data in the lower right corner is already loaded, but it is not displayed on the page. We are going to add a line of code async: false to let ajax execute first. &n...

echarts pie chart dynamically obtains background data ajax+json (3)

First of all, I recommend echarts official website: there are all histograms, line charts, and pie charts. https://echarts.apache.org/examples/zh/index.html You can download the corresponding echarts ...

Ajax dynamically obtains the json data from the background and loads it into the page table

In actual development, you often encounter the interaction of front and back data, and the intuitive representation data is often loaded into a table on the page for easy viewing. This project of mine...

How to use AJAX to deliver JSON data to the background

How to use AJAX to deliver JSON data to the background Back end frame: Django, front end: jQuery, Ajax We will encounter when developing: If you use the represented transmission method, the background...

Use jQuery to request the background to assemble the json data format through AJAX, and initialize the directory tree TreeView

The directory tree structure is needed in recent projects, using the bootstrap-treeview plug-in, and data is requested from the background. Plug-in dependency: jQuery Bootstrap Introduce treeview plug...

More Recommendation

AJax dynamically load list box

Functional description Optionally select two drop-down boxes and take the value that satisfies the condition from the server according to the selected value as the condition, and display it in the new...

MVC Ajax Post JSON Data to Several Notes of the Background

Today, when the POST entity object is passed through Ajax, 400, 415, 405, etc., or after the background entity parameter does not get the corresponding value, summary the problem is as follows: 1) The...

Use tables to dynamically display json data in jsp pages

Because the number of data obtained from the background is uncertain, use append. The js code is as follows:  ...

Use Echarts and Ajax to dynamically load data for big data visualization

In the previous post [Java/Web call Hadoop for MapReduce exampleIn 】, we implemented JavaWeb to call Hadoop to count the number of occurrences of words in text files uploaded by users. The effect is a...

Example of jquery operation Ajax to get background json data

The following is a small example of using jquery to operate Ajax to obtain back-end json data under the SpringBoot framework. Any questions are welcome to point out. Backend code: Front-end html code:...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top