如何在一个jqgrid列中添加两种格式?

时间:2013-10-04 22:28:54

标签: javascript jquery jqgrid

在我的数据库中,我在一列中有两个选项:

  1. /uploads/1369839038.zip
  2. NULL
  3. 现在我将“电子邮件”列格式化,以便显示为链接

    formatter:function(cellvalue, options, rowObject){
                    return '<a href="' + cellvalue + '"target="_blank">FILES </a> '
                } }
    

    但我想制作一个格式化程序,如果它像JQGRID show blank instead of Null那样有“NULL”,则显示为空白,或者显示为我已经拥有的链接。我想在这个“附件”列的一个格式化器中组合两者,以便根据数据显示链接或空白。请大家多多帮助。

    我的JQgrid

        $(function(){ 
          $("#list").jqGrid({
            url:'request.php',
            editurl: "jqGridCrud.php",
            datatype: 'xml',
            mtype: 'GET',
            height: 'AUTO',
        width: 850,
        scrollOffset:0,
        hidegrid: false,
    
        colNames:['id','Project', 'Assigned To','Assign Date','Check Date','Due Date','Attachments','Notes',""],
        colModel :[ 
          {name:'id', index:'id', width:28, align: 'center'}, 
          {name:'name', index:'name', width:170, align:'left',editable:true, editoptions:{
                size:60} }, 
          {name:'id_continent', index:'id_continent', width:50, align:'right',editable:true,edittype:'select', 
          editoptions:{value: "Henry:Henry; Ramon:Ramon; Paul:Paul" },mtype:'POST'  }, 
    
          {name:'lastvisit', index:'lastvisit', width:55, align:'right',formatter: 'date',srcformat:'yyyy-mm-dd',newformat: 'm/d/yy',editable:true, edittype: 'text',mtype:'POST' ,       editoptions:{size:10, dataInit:function(elem){$(elem).datepicker({dateFormat:'m/d/yy'});}}} ,
    
    
          {name:'cdate', index:'cdate', width:55, align:'right',formatter: 'date',srcformat:'yyyy-mm-dd',newformat: 'm/d/yy', edittype: 'text',editable:true ,mtype:'POST' ,editoptions:{size:10, dataInit:function(elem){$(elem).datepicker({dateFormat:'m/d/yy'});}}} ,
    
          {name:'ddate', index:'ddate', width:55, align:'right',formatter: 'date',srcformat:'yyyy-mm-dd',newformat: 'm/d/yy',date:'true',editable:true, edittype: 'text',editoptions:{size:10, dataInit:function(elem){$(elem).datepicker({dateFormat:'m/d/yy'});}}} ,
    
    
          {name:'files', index:'files', width:50,align:'center',sortable:false,mtype:'POST',formatter:function(cellvalue, options, rowObject){
                return '<a href="' + cellvalue + '"target="_blank">FILES </a> '
            } },
    
            {name:'notes', index:'notes', width:100, align:'left',sortable:false, editable:true,edittype:'textarea', editoptions:{
                rows:5,cols:60,maxlength:200} },    
    
            {name:'act', index:'act',width:30 ,align:'left', sortable:false,formatter: "actions",cellattr: function () { return ' title="Delete Project"'; },
    formatoptions: {
        keys: true,
         deltitle: 'delete',
        delbutton: true,
        editbutton:false,
        delOptions: {
            url: 'delete-perm.php',
            afterShowForm: function ($form) {
        $("#dData", $form.parent()).click();
    },
            msg: "Remove Selected Project?",
            bSubmit: "Remove",
            bCancel: "Cancel"
        }
    }}, 
        ],
        pager: '#pager',
    
        rowNum:30,
        rowList:[30,40,80],
        sortname: 'ddate',
        sortorder: 'asc',
        viewrecords: true,
        gridview: true,
        caption: 'Current Assignments',
    
    
        ondblClickRow: function(rowid) {
    
        $(this).jqGrid('editGridRow', rowid,
                            {width:550,Height:550,recreateForm:true,closeAfterEdit:true,
                             closeOnEscape:true,reloadAfterSubmit:true, modal:true,mtype:'post',top:350,left: 30});}
    
    
                });
    
     jQuery.extend(jQuery.jgrid.nav, {
            deltitle: '',
            delcaption: 'Project Complete'
    
    
        },{delicon: "ui-icon-circle-check",deltext: "Project Complete"});   
    
        $("#list").jqGrid("navGrid", "#pager", { add: false, search: false, refresh:false,edit:false }).navButtonAdd('#pager',{
    
    
                                    caption:"Export to Excel", 
                                    buttonicon:"ui-icon-save", 
                                    onClickButton: function () {
            jQuery("#list").jqGrid('excelExport', { url: 'ExportExcel.php' });
    }, 
                                    position:"last"
                                });
        // setup grid print capability. Add print button to navigation bar and bind to click.
        setPrintGrid('list','pager','Current Assignments');
    
    });
    

1 个答案:

答案 0 :(得分:1)

我不确定我是否正确理解您的问题,但为什么不尝试使用以下自定义格式化程序:

function (cellvalue, options, rowObject) {
    if (cellvalue === undefined || cellvalue === null || cellvalue === 'NULL') {
        return '&nbsp;'; // or just ""
    }
    return '<a href="' + cellvalue + '"target="_blank">FILES </a>';
}