jqgrid autocomplete无法发布id列

时间:2014-12-24 10:51:03

标签: jquery json autocomplete jqgrid

我已按照jqGrid inline edit: odd behavior with an autocomplete column

上的说明操作

但我错了...

我已经粘贴到:

{name:'SUPPLIERID', index:'SUPPLIERID', width:10, editable:true,align:'right',formatter: 'number', hidden: true},
{name:'SUPPLIER', index:'SUPPLIER', width:10, editable:true,edittype: 'text',
           editoptions:{dataInit: function(elem) { 
                $(elem).autocomplete({
                    source: function(request, response) {$.getJSON("../../main/lookup/supplierExt.php", { q: request.term }, response)},
                    select: function(event, ui){
                        //alert (ui.item.SUPPLIERID);
                        var rowId = $("#g_terima").jqGrid('getGridParam', 'selrow');
                        var rowData = $("#g_terima").jqGrid('getRowData', rowId);
                        rowData.SUPPLIERID = ui.item.SUPPLIERID;
                        $("#g_terima").jqGrid('setRowData', rowId, rowData);

                        }
                });
            }}},

和我们的查找Json:

    <?php
session_start();
include($_SESSION[apppath].'modules/firebird.php');
$term = trim(strip_tags($_REQUEST['q']));

$sql = "select s.supplierid, c.contact supplier from supplier s 
left join contact c on c.contactid = s.contactid
where s.companyid = $_SESSION[companyid] and c.contact like '%".$term."%'  
order by c.contact ";
$stmt = $conn->query($sql);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $row['id']=(int)$row[SUPPLIERID];
    $row['SUPPLIERID']=(int)$row[SUPPLIERID];
    $row['value']=htmlentities(stripslashes($row[SUPPLIER]));

    $row['label']=htmlentities(stripslashes($row[SUPPLIER]));
    $row_set[] = $row;
}

echo json_encode($row_set);
return true;
//exit();
?>

自动填充看起来很有希望,但我无法将ID / SUPPLIERID传递给SUPPLIERID COLUMN。 请问我做错了什么?我无法找到它......

在粘贴奥列格的建议后:

    var g_terima = '#g_terima'; var pg_terima = '#pg_terima'; var file_terima = 'bip_terima_.php';
jQuery(document).ready(function(){ 
jQuery(g_terima).jqGrid({
    url:file_terima,
    editurl: file_terima,
    datatype: 'json',
    mtype: 'GET',
    colNames:['Tgl','SUPPLIERID', 'Supplier', 'Internal','Peternak','Tipe', 'Produk', 'Jml/Berat', 'Harga','Total','PPN', 'Total+PPN'
     , 'Inv No','Tax No','NoId'],
    colModel :[ 
    {name:'TGL', index:'TGL', width:9, editable:true,sorttype:'date',
                editoptions:{
                    dataInit:function(el){$(el).datepicker({dateFormat:'yy-mm-dd',changeMonth: true,changeYear: true})}},
                    searchoptions:{dataInit:function(el){$(el).datepicker({dateFormat:'yy-mm-dd',changeMonth: true,changeYear: true})}}
            }, 
      {name:'SUPPLIERID', index:'SUPPLIERID', width:10, editable:true,align:'right',formatter: 'number', hidden: true},
      {name:'SUPPLIER', index:'SUPPLIER', width:10, editable:true,edittype: 'text',
           editoptions:{dataInit: function(elem) { 
                var $self = $(this);
                $(elem).autocomplete({
                    source: function(request, response) {$.getJSON("../../main/lookup/supplierExt.php", { q: request.term }, response)},
                    select: function(event, ui){
                        //alert (ui.item.SUPPLIERID);
                        var rowId = $self.jqGrid('getGridParam', 'selrow');
                        $("#" + rowId + "_SUPPLIERID").val(ui.item.SUPPLIERID);

                        }
                });
            }}},

SUPPLIERID COLUMN仍为空值...

enter image description here

1 个答案:

答案 0 :(得分:0)

您发布的内容不够详细。你使用哪种编辑模式?如果您使用内联编辑,那么在编辑行期间无法使用setRowData方法,但您仍然可以使用jQuery.val修改处于编辑模式的编辑单元格的值。

如果您使用内联编辑,那么正在编辑的单元格的ID将从rowid,_和列名称(在您的情况下为SUPPLIERID)构建。因此,您可以将代码中的getRowDatasetRowData替换为以下内容:

dataInit: function(elem) {
    var $self = $(this);
    $(elem).autocomplete({
        source: function(request, response) {
            $.getJSON(
                "../../main/lookup/supplierExt.php",
                { q: request.term },
                response
            );
        },
        select: function (event, ui) {
            var rowId = $self.jqGrid('getGridParam', 'selrow');
            $("#" + rowId + "_SUPPLIERID").val(ui.item.SUPPLIERID);
        }
    });
}

更新:了解您使用的编辑模式非常重要,因为输入字段的ID将根据不同的规则进行设置。以下代码检测是否将使用表单编辑,内联编辑或工具栏过滤器来选择相应的ID。

select: function (event, ui) {
    var id;
    if ($(elem).hasClass("FormElement")) {
        // form editing
        id = "SUPPLIERID";
    } else if ($(elem).closest(".ui-search-toolbar").length > 0) {
        // filter foolbar
        id = "gs_SUPPLIERID";
    } else if ($(elem).closest("tr.jqgrow").length > 0) {
        id = $(elem).closest("tr.jqgrow").attr("id") + "_SUPPLIERID";
    }
    $("#" + id).val(ui.item.SUPPLIERID);
}