简体   繁体   English

在jquery移动列表视图中格式化json内容

[英]Formatting json content in jquery mobile list view

Am creating an application in jquery mobile which the final output should be similar to this: 我在jquery mobile中创建一个应用程序,最终输出应与此类似:
在此处输入图片说明

The raw html code that produces that image is 产生该图像的原始html代码是

        <ul data-role="listview" data-inset="true">
    <li data-role="list-divider">Today, May 1, 2016 <span class="ui-li-count">2</span></li>
      <li><a href="#">
        <h3>Reply</h3>
        <p>20,00 we do have discounts for earlier payers </p>


        <p class="ui-li-aside">10 mins ago</p>
      </a></li>
      <li><a href="#">
        <h4>Me</h4>
        <p>How much is the current fee per semester? Do you accet credit cards</p>
        <p class="ui-li-aside">25 mins ago</p>
      </a></li>
      <li data-role="list-divider">Yesterday, April 30, 2016 <span class="ui-li-count">1</span></li>
      <li><a href="#">
        <h3>Reply</h3>
        <p>Hello, How can i help you?</p>
        <p class="ui-li-aside">10:15 pm</p>
      </a></li>
        <li data-role="list-divider">Friday, April 29, 2016 <span class="ui-li-count">1</span></li>
      <li><a href="#">
        <h3>Me</h3>
        <p>Hello</p>
        <p class="ui-li-aside">10:15 pm</p>
      </a></li>
     </ul>


In my project am using json to fetch data from the database and show it to the user in a listview like above. 在我的项目中,我正在使用json从数据库中获取数据,并在如上的列表视图中将其显示给用户。

Code to fetch data from the database: 从数据库获取数据的代码:

  var id_from = localStorage.loggedin_id;
  var id_to_send = localStorage.user_schoolls_head;
 $.getJSON("http://127.0.0.1/tum_old/custom/php/message.php",  
{id_from:id_from,id_to_send:id_to_send},
function(response){
console.log(response); 
});

After the console.log(response) i get the javascript object as: 在console.log(response)之后,我得到的javascript对象为:

在此处输入图片说明

A from the picture with the list a me situation is like the one for from_id and then to_id (example 1:4) while a reply is the vice versa. 带有列表“我”的图片中的一个情况类似于from_id,然后是to_id(示例1:4),反之亦然。 How can i format the time to display : 我该如何格式化显示时间:

Today, yeasterday...from hence(on) it will display the dats on the list dividers
Time to show 10mins ago, 20mins ago, but beyond 1 hour it shows the actual time (eg: 10:15pm)
How to also sort these messages according to dates so that 
the earliest is seen first in the today area.. 

ie: IT SHOULD APPEAR LIKE THE FIRST IMAGE 即:它应该像第一幅图像一样出现

MY DATABASE RECORD: 我的数据库记录: 在此处输入图片说明

To format dates you can use momentjs ( http://momentjs.com ). 要格式化日期,可以使用momentjs( http://momentjs.com )。 Sorting can be achieved simply by adding ORDER BY time_sent DESC in your sql query 只需在SQL查询中添加ORDER BY time_sent DESC即可实现排序

Check out this fiddle . 看看这个小提琴

It would be very unwise to sort your objects using javascript. 使用javascript对对象进行排序是非常不明智的。 Like Zoran said, you should do that in your query using ORDER BY time_sent DESC . 就像Zoran所说的那样,您应该在查询中使用ORDER BY time_sent DESC

Update Not sure what you mean by divider based on date but I've updated the jsfiddle to output exactly how the date is formatted in your example. 更新不确定基于日期分隔符是什么意思,但是我已经更新了jsfiddle以输出示例中日期的确切格式。 Hope it helps. 希望能帮助到你。

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

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