创建HTML:PHP服务器端与jQuery的客户端 [英] Creating HTML: PHP server-side vs. jQuery client-side

查看:138
本文介绍了创建HTML:PHP服务器端与jQuery的客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个设计问题。予有需要进入一个HTML表,这将在稍后被用户操纵的数据。基本上,用户将能够在表中的行选择项目。

This is a design question. I have data that needs to go into an HTML table, which will later be manipulated by the user. Basically the user will be able to select items in the table rows.

我有两个选择 - 在这两种情况下我使用AJAX来获取数据:

I have two options - in both cases I'm using AJAX to get the data:

  1. 使用PHP创建服务器端的HTML code,将其发送给客户端的HTML。然后,用户操纵使用Javascript(jQuery的,本质上)表。

  1. Create the HTML code using PHP on the server side, send it to the client as HTML. The user then manipulates the table using Javascript (jQuery, essentially).

使用JSON发送原始数据到客户端,然后使用jQuery既创建HTML,后来由用户操作它。

Send the raw data to the client using JSON, then use jQuery to both create the HTML and later manipulate it by the user.

从设计/易用性的编码/美容的角度来看,它的做法是建议? 感谢您的任何见解。

From a design/ease of coding/beauty point of view, which approach is recommended? Thanks for any insight.

推荐答案

为什么产生在PHP中的HTML:

Why to generate the HTML in PHP:

  • 的JavaScript应该定义的行为,而不是内容。
  • 创建JavaScript中需要更多的标记(多行字符串并不像在PHP中一样简单)。
  • 这是很难维持,如果在几个地方(PHP和放大器; JS)生成的HTML。
  • 您可以使用jQuery的DOM操作功能来创建你的HTML,但你搬起石头砸自己的脚从长远来看。
  • 在它的速度更快建立在服务器不是在浏览器的HTML(在计算意义上的)。
  • 循环与PHP&ndash的更轻松;很容易生成表格标记。
  • 您保留某种combatibility,如果用户已经禁用了JavaScript,如果你的输出在页面加载的标记。

为什么要生成HTML jQuery的:

Why to generate the HTML in jQuery:

  • 您会节省一些带宽。
  • 绑定事件可能是简单的。

所以,我赞成第一个方案中,产生在PHP中的HTML。

So, I'm in favour of the first option, generating the HTML in PHP.

这两种方法的视觉对比,创建一个简单的表。  

Visual comparison of the two methods, creating a simple table.  

// PHP

$html = '<table>';    
foreach($data as $row) {
    $html .= '<tr>';
    $html .= '<td><a href="#" class="button">Click!</a></td>';
    $html .= '<td>'.$row['id'].'</td>';
    $html .= '<td>'.$row['name'].'</td>';
    $html .= '</tr>';
}
$html .= '</table>'; 
echo $html;
?>

// jQuery

$('#container').load('handler.php', function() {
    $('#container a.button').click(function() {
        // Do something
    });
});

&NBSP;

 

// PHP

echo json_encode($data);

// jQuery

$.ajax({
    url: 'handler.php',
    dataType: 'json',
    success: function(data) {
        var table = $('<table />');

        var len = data.length;
        for(var i = 0; i < len; i++) {
            var row = $('<tr />');
            var a = $('<a />').attr('href', '#').addClass('button');

            row.append($('<td />').append(a));
            row.append($('<td />').html(data[i].id);
            row.append($('<td />').html(data[i].name);

            table.append(row);
        }

        table.find('.button').click(function() {
            // Do something
        });

        $('#container').html(table);
    }
});

从设计/易用性的角度编码/美点,我肯定会说去用PHP的方法。

From a design / ease of coding / beauty point of view, I'd definitely say go with the PHP approach.

这篇关于创建HTML:PHP服务器端与jQuery的客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆