2013-04-17 48 views
1

我有2个表格:客户联系人。表联系人有一个外键指向客户表。jQuery EasyUI - 添加链接到单元格

<table id="dgContactSearch" title="Suche" class="easyui-datagrid" style="height:160px" 
          url="getContacts.php" 
          toolbar="#toolbarContactSearch" pagination="true" 
          rownumbers="true" fitColumns="true" singleSelect="true"> 
         <thead> 
          <tr> 
           <th field="customer_id" width="50">Customer</th> 
           <th field="name" width="50">Name</th> 
           <th field="function" width="50">Funktion</th> 
           <th field="phone" width="50">Phone</th> 
           <th field="mobile" width="50">Mobile</th> 
           <th field="fax" width="50">Fax</th> 
           <th field="email" width="50">Email</th> 
           <th field="comment" width="50">Commnent</th> 
          </tr> 
         </thead> 
        </table> 

这里是php文件

$page = isset($_POST['page']) ? intval($_POST['page']) : 1; 
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10; 
    $searchItemContact = isset($_POST['searchItemContact']) ? mysql_real_escape_string($_POST['searchItemContact']) : ''; 

    $searchItemContact = htmlentities($searchItemContact, ENT_QUOTES, 'UTF-8'); 

    $offset = ($page-1)*$rows; 

    $result = array(); 

    $where = "name like '%$searchItemContact%' OR function like '%$searchItemContact%' OR email like '%$searchItemContact%' OR comment like '%$searchItemContact%'"; 
    $rs = mysql_query("select count(*) from contact where " . $where); 
    $row = mysql_fetch_row($rs); 
    $result["total"] = $row[0]; 

    $rs = mysql_query("select * from contact where " . $where . " limit $offset,$rows"); 

    $items = array(); 
    while($row = mysql_fetch_object($rs)){ 
     array_push($items, $row); 
    } 
    $result["rows"] = $items; 
    echo json_encode($result); 

我想使<th field="customer_id" width="50">Customer</th>联。即<a href="customerView.php?id=$customer_id>#</a> 因此,当我加载表时,我看到客户名称instaed的id并建立一个链接到客户页面! 请帮忙。

更新

我总算有点变通方法:

<th data-options="field:'name',width:100,align:'left',formatter:formatCustomerId">Name</th> 

,然后JavaScript函数,随之而来:

function formatCustomerId(val,row){ 
    var url = "customerView.php?id="; 
    return '<a href="'+url + row.customer_id+'">'+val+'</a>'; 
} 
+0

你应该张贴jQuery代码太 –

+0

这jQuery代码? –

+0

初始化你的数据网格的那个 –

回答

3

最终的解决方案是所有以前的答案的混合:)。

<table id="dgContactSearch" title="Benutzer Suche" class="easyui-datagrid" style="height:160px" 
          url="getContacts.php" 
          toolbar="#toolbarContactSearch" pagination="true" 
          rownumbers="true" fitColumns="true" singleSelect="true"> 
         <thead> 
          <tr> 
           <th data-options="field:'firma',width:80,align:'left',formatter:formatCustomerId">Kunde</th> 
           <th data-options="field:'name',width:50,align:'left',formatter:formatContactUrl">Name</th> 
           <th field="function" width="50">Funktion</th> 
           <th field="phone" width="50">Phone</th> 
           <th field="email" width="50">Email</th> 
           <th field="mobile" width="50">Mobile</th> 
           <th field="fax" width="50">Fax</th> 

           <th field="comment" width="120">Kommentare</th> 
          </tr> 
         </thead> 
</table> 

脚本:

<script> 
function formatCustomerId(val,row){ 
    var url = "customerView.php?id="; 
    return '<a href="'+url + row.customer_id+'">'+val+'</a>'; 
} 

function formatContactUrl(val,row){ 
    var url = "contactView.php?id="; 
    return '<a href="'+url + row.id+'">'+val+'</a>'; 
} 
</script> 

和getContacts.php

<?php 
    include 'includes/db_functions.php'; 
    db_link(); 

    $page = isset($_POST['page']) ? intval($_POST['page']) : 1; 
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10; 
    $searchItemContact = isset($_POST['searchItemContact']) ? mysql_real_escape_string($_POST['searchItemContact']) : ''; 

    $searchItemContact = htmlentities($searchItemContact, ENT_QUOTES, 'UTF-8'); 

    $offset = ($page-1)*$rows; 

    $result = array(); 

    $where = "name like '%$searchItemContact%' OR function like '%$searchItemContact%' OR customer.name like '%$searchItemContact%' OR email like '%$searchItemContact%' OR comment like '%$searchItemContact%'"; 
    $rs = mysql_query("select count(*) from contact where " . $where); 
    $row = mysql_fetch_row($rs); 
    $result["total"] = $row[0]; 


    $sql = "SELECT contact.*, customer.name 
    FROM `contact` 
    INNER JOIN `kunden` on contact.customer_id = kunden.id "; 

    $rs = mysql_query($sql . "where " . $where . " limit $offset,$rows"); 

    $items = array(); 
    while($row = mysql_fetch_object($rs)){ 
     array_push($items, $row); 
    } 
    $result["rows"] = $items; 
    echo json_encode($result); 

?> 
+0

谢谢。 “官方”建议的实现方式要比这个“仅限一格的格式化程序”实现得多:http://www.jeasyui.com/forum/index.php?topic=1725.0 – AFract

0

对于那种链接我使用一个数据 - url属性来保存我想重定向的url,然后使用jquery重定向用户,例如:

在你的服务器端,当你生成的表,要链接关联写入数据url属性:

<td class='customer_whatever' data-url='<?= $customer_page_url ?>'> Customer Name</td> 

在jQuery中捕捉所有,并添加onclick事件:

$('td').each(function(){ 
    if($(this).data('url')){ 
     $(this).click(function(){ 

     $(location).attr('href',$(this).data('url')); 
     }); 
    } 
}); 

在CSS中不要忘了把:

.customer_whatever{ cursor:pointer;} 

这是一个小的工作示例:http://jsfiddle.net/9XA3k/1/

+0

这种情况下的问题是我无法控制​​标签。我只能控制标签。我如何分别访问​​标签并自定义来自json数组的内容? –