Пишу чат, сообщения с сервера приходят в виде JSON за вывод сообщений отвечает JS. Проблема возникла с разбитием сообщений по дням вот не понимаю как сделать на JS, а на PHP могу. Можно на сервере PHPкой разбивать по датам формировать в JSON и уже JS это разбирать, но хотелось бы на JS.
Стандартный Ajax с передачей JSON
JSON:
{"success":1,"chat_user_id":"7","user_id":"7","user_first_name":null,"user_last_name":null,"user_photo":null,"user_screen_name":null,"number_contact":null,"chat_client_info":{"first_name":null,"last_name":null,"patronymic_name":null,"user_avatar":null},"message_list":{"10":{"from":"client","agent_id":null,"user_full_name":"\u041f\u043e\u0441\u0435\u0442\u0438\u0442\u0435\u043b\u044c \u21167","user_avatar":"\/img\/none_foto.png","text":"\u043f\u0440\u0438\u0432\u0435\u0442\n","datetime":"2019-10-29 23:02:20"},"11":{"from":"agent","agent_id":"1","user_full_name":"\u0410\u0433\u0435\u043d\u0442","user_avatar":"\/images\/user\/mini\/1513786871698.jpg","text":"\u043f\u0440\u0438\u0432\u0435\u0442\n","datetime":"2019-10-29 20:10:07"},"12":{"from":"client","agent_id":"1","user_full_name":"\u041f\u043e\u0441\u0435\u0442\u0438\u0442\u0435\u043b\u044c \u21167","user_avatar":"\/img\/none_foto.png","text":"\u043f\u0440\u0438\u0432\u0435\u0442 \n","datetime":"2019-10-30 21:52:13"},"32":{"from":"client","agent_id":"1","user_full_name":"\u041f\u043e\u0441\u0435\u0442\u0438\u0442\u0435\u043b\u044c \u21167","user_avatar":"\/img\/none_foto.png","text":"\u043f\u0440\u0438\u0432\u0435\u0442\n","datetime":"2019-11-03 20:35:42"},"33":{"from":"client","agent_id":"1","user_full_name":"\u041f\u043e\u0441\u0435\u0442\u0438\u0442\u0435\u043b\u044c \u21167","user_avatar":"\/img\/none_foto.png","text":"\u043f\u0440\u0438\u0432\u0435\u0442\n","datetime":"2019-11-03 20:37:51"},"36":{"from":"agent","agent_id":"1","user_full_name":"\u0410\u0433\u0435\u043d\u0442","user_avatar":"\/images\/user\/mini\/1513786871698.jpg","text":"\u043f\u0440\u0438\u0432\u0435\u0442\n","datetime":"2019-11-03 19:19:50"},"39":{"from":"client","agent_id":"1","user_full_name":"\u041f\u043e\u0441\u0435\u0442\u0438\u0442\u0435\u043b\u044c \u21167","user_avatar":"\/img\/none_foto.png","text":"gg\n","datetime":"2019-11-03 22:32:26"}}}
в цикл forEach:
let obj = data.message_list;
var message_list = '';
Object.keys(obj).forEach(function(key){
templateMessage(obj[key]['from'],obj[key]['user_avatar'],obj[key]['user_full_name'],obj[key]['text'],obj[key]['datetime']); // шаблон сообщений
});
function templateMessage(from,avatar,user_full_name,text,datetime) {
var from_class = '';
var avatar_src = '//placekitten.com/g/50/50';
if(from == 'client') { from_class = 'dialogs-user-message agent';}
if(from == 'agent') { from_class = 'dialogs-user-message';}
if(!!avatar) {avatar_src = avatar;}
var sp = document.createElement("div");
sp.innerHTML = '<div class="dialogs-user-message-photo"><img src="'+avatar_src+'" width="50" class="img-circle img-responsive messenger-comment-avatar"></div><div class="dialogs-user-message-body"><div class="name-user">'+user_full_name+' <span>'+datetime+'</span></div><div class="message-user">'+text+'</div></div>';
sp.className = from_class;
document.querySelector(".dialogs-user-message-body").appendChild(sp);
}
Может функцию обработчик сделать, чтобы она разбивала на даты.
Разбивка сообщений по дням.
const z = {"success":1,"chat_user_id":"7","user_id":"7","user_first_name":null,"user_last_name":null,"user_photo":null,"user_screen_name":null,"number_contact":null,"chat_client_info":{"first_name":null,"last_name":null,"patronymic_name":null,"user_avatar":null},"message_list":{"10":{"from":"client","agent_id":null,"user_full_name":"\u041f\u043e\u0441\u0435\u0442\u0438\u0442\u0435\u043b\u044c \u21167","user_avatar":"\/img\/none_foto.png","text":"\u043f\u0440\u0438\u0432\u0435\u0442\n","datetime":"2019-10-29 23:02:20"},"11":{"from":"agent","agent_id":"1","user_full_name":"\u0410\u0433\u0435\u043d\u0442","user_avatar":"\/images\/user\/mini\/1513786871698.jpg","text":"\u043f\u0440\u0438\u0432\u0435\u0442\n","datetime":"2019-10-29 20:10:07"},"12":{"from":"client","agent_id":"1","user_full_name":"\u041f\u043e\u0441\u0435\u0442\u0438\u0442\u0435\u043b\u044c \u21167","user_avatar":"\/img\/none_foto.png","text":"\u043f\u0440\u0438\u0432\u0435\u0442 \n","datetime":"2019-10-30 21:52:13"},"32":{"from":"client","agent_id":"1","user_full_name":"\u041f\u043e\u0441\u0435\u0442\u0438\u0442\u0435\u043b\u044c \u21167","user_avatar":"\/img\/none_foto.png","text":"\u043f\u0440\u0438\u0432\u0435\u0442\n","datetime":"2019-11-03 20:35:42"},"33":{"from":"client","agent_id":"1","user_full_name":"\u041f\u043e\u0441\u0435\u0442\u0438\u0442\u0435\u043b\u044c \u21167","user_avatar":"\/img\/none_foto.png","text":"\u043f\u0440\u0438\u0432\u0435\u0442\n","datetime":"2019-11-03 20:37:51"},"36":{"from":"agent","agent_id":"1","user_full_name":"\u0410\u0433\u0435\u043d\u0442","user_avatar":"\/images\/user\/mini\/1513786871698.jpg","text":"\u043f\u0440\u0438\u0432\u0435\u0442\n","datetime":"2019-11-03 19:19:50"},"39":{"from":"client","agent_id":"1","user_full_name":"\u041f\u043e\u0441\u0435\u0442\u0438\u0442\u0435\u043b\u044c \u21167","user_avatar":"\/img\/none_foto.png","text":"gg\n","datetime":"2019-11-03 22:32:26"}}};
const data = z.message_list;
const result = {};
Object.values(data).forEach(item => {
const date = item.datetime.split(' ')[0];
if (!result[date]) {
result[date] = [];
}
result[date].push(item)
});
console.log(result)
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Лендинг с 4-я блокамиХочу что бы листать можно было только по одному, без разницы сколько прокрутишь мишкой
Использую на сайте готовый плагин анимации, основанный на threejs
Вот выводится число 10099 , а как сделать чтобы выводилось 1
Я отдаю себе отчет о том, что это супер нубский вопрос, но тем не менее, я просидел час в попытках понять, почему глоабальный вар изменяетсяЯ...