返回一个 php 数组到 jquery ajax 并填充一个列表。与一些 if() 的
尝试制作一个ajax驱动的、可排序的联系人列表。 包含 3 个选项的菜单应在单击后呈现一个包含更新数据的新列表。
一直在四处搜索,但还没有找到任何关于这方面的好信息(至少据我所知......)
这就是目前我的列表中的内容。我知道,变量选择不好..
<?php
if($c['ischeckedin'])
{
$checkedin='Check Out';
$checked='checkedin';}
else{
$checkedin='Check In';
$checked='';}
?>
<li id="<?php print $c['id']; ?>" class="clearfix gradient1 <?php print $checked; ?>">
<?php print $c['name']; ?>
<img src="options.png" class="options rounded3" alt="options" width="16" height="16" />
<?php if($c['ischeckedin']): ?>
<input
type="button"
class="button checkerout"
value="<?php print $checkedin; ?>"
name="<?php print $c['id']; ?>" />
<?php else: ?>
<input
type="button"
class="button checkerin"
value="<?php print $checkedin; ?>"
name="<?php print $c['id']; ?>" />
<?php endif; ?>
</li>
<?php endforeach; ?>
所以基本上它是一个联系人列表。有些已签入,因此有不同的类和按钮。 (任何更清晰的建议都非常受欢迎!)
但现在我需要通过 Ajax 填充列表,因为我需要能够对其进行排序。因此,我在调用时发送了更多数据,并且我的 php/mysql 返回了一些过滤后的数据。
- ID
应该是数据库的 ID。 class
需要在“checkedin”和“”之间有所不同,具体取决于 mysql 的结果。<输入>
- class
应该是“checkerin”或“checkerout”,具体取决于 mysql 的结果。 name
应该是数据库的 id。
问题是,我该如何处理php返回的数据->阿贾克斯->打印?
我如何翻译所有那些讨厌的 if() 以便我可以区分已检查和未检查的联系人?退货之前先编辑一下吗?
Trying to make an ajax driven, sortable contact list.
A menu with 3 alternatives should upon click render a new list with updated data.
Been searching around a bit but havn't found any good info on this (as from what I understand atleast..)
This is what populates my list at the moment. I know, bad choice of variables..
<?php
if($c['ischeckedin'])
{
$checkedin='Check Out';
$checked='checkedin';}
else{
$checkedin='Check In';
$checked='';}
?>
<li id="<?php print $c['id']; ?>" class="clearfix gradient1 <?php print $checked; ?>">
<?php print $c['name']; ?>
<img src="options.png" class="options rounded3" alt="options" width="16" height="16" />
<?php if($c['ischeckedin']): ?>
<input
type="button"
class="button checkerout"
value="<?php print $checkedin; ?>"
name="<?php print $c['id']; ?>" />
<?php else: ?>
<input
type="button"
class="button checkerin"
value="<?php print $checkedin; ?>"
name="<?php print $c['id']; ?>" />
<?php endif; ?>
</li>
<?php endforeach; ?>
So basicly it's a list of contacts. Some are checked in, therefore the different classes and buttons. (Any cleaner suggestions are more than welcome!)
But now I need to populate the list via Ajax, since I need to be able to sort it. So I send some more data along with the call and my php/mysql returns some filtered data.
<li>
-ID
should be the id from the db.class
needs to differ between 'checkedin' and '' depending on result from mysql.
<input>
-class
should be 'checkerin' or 'checkerout' depending on result from mysql.name
should be the id from the db.
The problem, what do I do with the returned data from php -> ajax -> print?
And how do I translate all those nasty if()'s so that I can make difference amongst checked and nonchecked contacts? Edit it before returning it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让 php 将数据创建为关联数组,并将其作为 json_encoded 字符串返回到 ajax,然后遍历 json 并创建列表。或者,您可以让 ajax 返回已创建的页面并使用该信息填充您的 div,只需确保在 ajax 调用上设置选项以允许 javascript 在需要时执行。
Let php create the data as an associative array and return it to the ajax as a json_encoded string, then walk through the json and create your list. Or, you could have the ajax return the already created page and populate your div with that info, just make sure you set the option on the ajax call to allow javascript to execute if you need it to.