简体   繁体   English

如果我通过ajax响应追加所有的html数据是好的做法吗?

[英]is it good practice if i append all the html data through ajax response?

i wanted to ask i am running a ajax function that loads all the data to my html. 我想问我正在运行一个ajax函数,将所有数据加载到我的html。 now all the data shows itself but i have done this by appending all the data as their are so many post i need to create a loop that shows all the post in a loop 现在所有的数据显示自己,但我已经通过附加所有数据,因为他们是如此多的帖子,我需要创建一个循环,显示循环中的所有帖子

here is my code 这是我的代码

    <script type="text/javascript">
 $(window).load(function(e){

    // grab the scroll amount and the window height
       loadmore();
       // get_recieve_friend_requests();
       // get_sent_friend_requests();
    });

 function loadmore(){
          var lastID = $('.load-more').attr('lastID');
         //alert(lastID);

              jQuery.ajax({
                  type:'POST',

                  url:'<?php echo base_url("user/get_all_post"); ?>',
                   data: {id:  lastID },
                      dataType: 'json', 


                  beforeSend:function(data){
                      $('.load-more').show();
                  },
                  success:function(data){

                         var ParsedObject = JSON.stringify(data);            
                         var json = $.parseJSON(ParsedObject);



                         if (json=="") {
                          $("#bottom").append('<div class="btn btn-default col-md-6" >'+'No More Results'+'</div>');
                          $("#Load_more_data").hide()

                         }else{
                           $postID=json[json.length-1].id;

              $('.load-more').attr('lastID', $postID);

                $.each(json, function (key, data) {
   $post_id=data.id;

   var post_id=data.id;
     // $('.post_id_value').attr('post_id', $post_id);
    var post_status=data.status;
     var status_image=data.status_image;
    var multimage=data.multimage;


                             if(!post_status=="" && !status_image==""){
                               $("#status_data").append('<input type="text" value="'+ post_id+'" class="post_id_value"> <div class="col-md-6 postdata"><a ><?php echo img($user_image); ?></a><a class="weshare_user_name text-font"><?php echo $uname; echo " "; echo $lname;?></a><div class="weshare_user_status">'+post_status+'</div><div class="weshare_user_singleimage"><img style="height:300px; width:400px;" src="<?php echo base_url('uploads'); ?>/'+status_image+'"></div><div class="row"><div class="col-md-12"><ul class="list-inline"><li><a href="#"><span class="glyphicon glyphicon-thumbs-up"></span> Like</a></li><li><a  onclick="like();"><span class="glyphicon glyphicon-comment"></span> Comment</a></li><li><a href="#"><span class="glyphicon glyphicon-share-alt"></span> Share</a></li></ul></div></div></div>');
                             }else{

                             }

                              if(!post_status=="" && status_image==""){
                               $("#status_data").append('<input type="text" value="'+ post_id+'" class="post_id_value"><div class="col-md-6 postdata" ><a ><?php echo img($user_image); ?></a><a class="weshare_user_name text-font"><?php echo $uname; echo " "; echo $lname;?></a><div class="weshare_user_status">'+post_status+'</div><div class="row"><div class="col-md-12"><ul class="list-inline"><li><a  onclick="like();"><span class="glyphicon glyphicon-thumbs-up"></span> Like</a></li><li><a ><span class="glyphicon glyphicon-comment"></span> Comment</a></li><li><a href="#"><span class="glyphicon glyphicon-share-alt"></span> Share</a></li></ul></div></div></div>');
                             }else{

                             }


                             if (multimage=="") {

                             }
                              else{
                               $("#status_data").append('<input type="text" value="'+ post_id+'" class="post_id_value"><div class="col-md-6 postdata" ><a ><?php echo img($user_image); ?></a><a class="weshare_user_name text-font"><?php echo $uname; echo " "; echo $lname; ?></a><div class="weshare_user_multimage"><img style="height:300px; width:400px;" src="<?php echo base_url('uploads'); ?>/'+multimage+'"></div><div class="row"><div class="col-md-12"><ul class="list-inline"><li><a  onclick="like();"><span class="glyphicon glyphicon-thumbs-up"></span> Like</a></li><li><a href="#"><span class="glyphicon glyphicon-comment"></span> Comment</a></li><li><a ><span class="glyphicon glyphicon-share-alt"></span> Share</a></li></ul></div></div></div>');
                              }                              

                    });
                  }
              }
            });
          }
                  function like()
          {
              var Post_id = $('.post_id_value').attr('value');
              var User_id = $('.id_data').attr('value');
              alert(Post_id);
                jQuery.ajax({
                  type:'POST',
                  url:'<?php echo base_url("user/post_likes"); ?>',
                  data: {Post_id:  Post_id,User_id:User_id},
                  dataType: 'json', 
                  success:function(data)
                  {
                    console.log(data);
                    alert();
                  }
          });
              }

  </script>

see that i have created all the div in a response is it possible to return all these values so that i can accesss them in a loop using $.each as there are 500 posts so what can i do can you tell me? 看到我已经在响应中创建了所有div,可以返回所有这些值,以便我可以使用$ .each在循环中访问它们,因为有500个帖子所以我能做什么才能告诉我?

when i alert 'json' it shows me this look at the image 当我提醒'json'时,它会向我展示这个图像 警报时的json值

Access global scope javascript object filled by ajax. 访问由ajax填充的全局范围javascript对象。

Like this? 像这样? (it is not exact solution, just for an inspiration) (这不是确切的解决方案,只是为了灵感)

<script type="text/javascript">
    var posts={};
    $(window).load(function(e){
       ajax();
    }
    function ajax(){
       //... the ajax stuff
       success:function(ajaxData){
          //... json stringify and parse object
          $.each(json, function (key, data){//fill the global scope object with received data to be accessible outside the ajax function
              posts.push({data.post_id:{"post_status":data.post_status, "status_image":data.status_image}});
          }
       }
    }
</script>

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

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