2013-04-17 115 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數組的內容? –

相關問題