【问题标题】:Cannot change value of a row in Jqgrid无法更改 Jqgrid 中行的值
【发布时间】:2014-09-11 10:34:16
【问题描述】:

当我选择一行并单击按钮时,我希望一行中的图像/图标从红色变为绿色。然而它什么也没做。您可以在以下 jsfiddle 上找到它:http://fiddle.jshell.net/njiterry/p9n8qq0x/9/

在下面查看我的 javascript 代码:

var mydata = [{
    Sel: false,
    Country: "Germany",
    Capital: "Berlin",
    Date: "05-09-2014"
}, {
    Sel: false,
    Country: "France",
    Capital: "Paris",
    Date: "05-09-2014"
}, {
    Sel: false,
    Country: "Cameroon",
    Capital: "Yaounde",
    Date: "06-09-2014"
}, {
    Sel: false,
    Country: "Gabon",
    Capital: "Libreville",
    Date: "06-09-2014"
}, {
    Sel: false,
    Country: "Holland",
    Capital: "Amsterdam",
    Date: "07-09-2014"
}, {
    Sel: false,
    Country: "Japan",
    Capital: "Tokyo",
    Date: "08-09-2014"
}, {
    Sel: false,
    Country: "Italy",
    Capital: "Rome",
    Date: "09-09-2014"
}, {
    Sel: false,
    Country: "Spain",
    Capital: "Madrid",
    Date: "09-09-2014"
}, {
    Sel: false,
    Country: "England",
    Capital: "London",
    Date: "10-09-2014"
}, {
    Sel: false,
    Country: "US",
    Capital: "Washington D.C.",
    Date: "12-09-2014"
}],
    grid = jQuery("#pays_grid"),
    initDateWithButton = function (elem) {
        if (/^\d+%$/.test(elem.style.width)) {
            // remove % from the searching toolbar
            //elem.style.width = '';
            $(elem).css({
                width: "230px"
            });
        }
        // to be able to use 'showOn' option of datepicker in advance searching dialog
        // or in the editing we have to use setTimeout
        setTimeout(function () {
            $(elem).datepicker({
                dateFormat: 'dd-mm-yy',
                showOn: 'button',
                changeYear: true,
                changeMonth: true,
                buttonImageOnly: true,
                buttonImage: "http://jqueryui.com/resources/demos/datepicker/images/calendar.gif",
                buttonText: "Select date",
                onSelect: function (dateText, inst) {
                    if (inst.id.substr(0, 3) === "gs_") {
                        grid[0].triggerToolbar();
                    } else {
                        // to refresh the filter
                        $(inst).trigger("change");
                    }
                }
            });
        }, 100);
    };

grid.jqGrid({ //set your grid id
    data: mydata, //insert data from the data object we created above
    datatype: 'local',
    height: '250',
    gridview: true,
    width: 600,
    autoheight: true,
    rowNum: 10,
    rowList: [1, 5, 10],
    colNames: ['Sel.', 'Country', 'Developed', 'Capital', 'Date'],
    onCellSelect: function(rowid, index, contents, event) 
    {   
       var cm = grid.jqGrid('getGridParam','colModel');                          
       if(cm[index].name == "Developed")
       {
            var img= $('tr#'+rowid).find('td:nth-child(3) > img')
            if(img.attr('alt')=='red light')
            {                img.attr({'src':'http://www.iconsdb.com/icons/preview/green/circle-xxl.png','alt':'green light' });
            }
           else
           {
img.attr({'src':'http://www.iconsdb.com/icons/preview/red/circle-xxl.png','alt':'red light' });            
           }
       }
    },

    //define column names
    colModel: [{
        name: 'Sel',
        align: 'center',
        sortable: false,
        width: 10,
        search: false,
        editable: true,
        edittype: 'checkbox',
        editoptions: {
            value: "True:False"
        },
        formatter: "checkbox",
        formatoptions: {
            disabled: false
        }
    }, {
        name: 'Country',
        index: 'Country',
        key: true,
        width: 20,
        align: 'center'
    }, {
        name: 'Developed',
        align: 'center',
        width: 65,
        fixed: true,
        formatter: function () {
            return "<img src='http://www.iconsdb.com/icons/preview/red/circle-xxl.png' alt='red light' style='width:15px;height:15px'/>";
        }
    }, {
        name: 'Capital',
        index: 'Capital',
        width: 20,
        align: 'center'
    }, {
        name: 'Date',
        index: 'Date',
        align: 'center',
        width: 55,
        sorttype: 'date',
        editable: true,
        editoptions: {
            dataInit: initDateWithButton,
            size: 11
        },
        searchoptions: {
            sopt: ['eq', 'ne', 'lt', 'le', 'gt', 'ge'],
            dataInit: initDateWithButton
        }
    }], //define column models
    pager: '#pays_grid_pager', //set your pager div id
    viewrecords: true, //if true, displays the total number of records, etc. as: "View X to Y out of Z” optional
    sortorder: "asc", //sort order; optional
    sortname: 'Country',
    shrinkToFit: true,
    forceFit: true,
    caption: "Country Overview"
}).navGrid('#pays_grid_pager', {
    edit: false,
    add: false,
    del: false,
    search: false,
    refresh: true
}).navButtonAdd('#pays_grid_pager', {
    caption: "Set Developed",
    buttonicon: "ui-icon-circle-check",
    position: "first",
    title: "Set Developed",
    onClickRow: function (rowId, iRow, iCol, e) {
        alert("yes");
    },
    onClickButton: function () {
        alert(1);
        var data = grid.getRowData();
        for (var i = 0; i < data.length; i++) {
            if (data[i].sel === 'True') {
                data[i].Developed = "<img src='http://www.clker.com/cliparts/q/j/I/0/8/d/green-circle-icon-md.png' alt='green light' style='width:15px;height:15px'/>";
                data[i].sel = 'False'
                grid.jqGrid('setRowData', i, data[i]);
                grid.jqGrid('saveRow', i, false);
            }
        }
    }

我想要的是:当我选择一行(通过单击编辑框)并单击“设置已开发”按钮时,所选行上的图标应从红色变为绿色

【问题讨论】:

  • 请发布您的代码,而不仅仅是 jsfiddle 链接。
  • 看看fiddle.jshell.net/p9n8qq0x/17。我在这里使用 no saveRowsetRowData。相反,我修改了数据并重新加载了网格。此外,我添加了beforeSelectRow,它修改了数据对应于"Sel"列中复选框的状态,就像我为the answer创建的the demo一样。我想这是你需要的。在这种情况下,"Sel" 列的状态保持保存在分页上。
  • @Oleg:我真的很喜欢你的解决方案,以避免使用 saveRow 或 setRowData 并将图像保留在 colModel 中。我决定删除 Sel。列并使用多选选项。在图像从红色变为绿色后,我希望取消选中复选框。请参阅我的新小提琴 fiddle.jshell.net/njiterry/p9n8qq0x/31/。您能否将我的新小提琴更改为与您的代码相对应。

标签: javascript jquery html jqgrid


【解决方案1】:

两个问题是你必须将 data[i].sel 重命名为 data[i].Sel

图像的值已更改,但视图没有更改,因此您必须在值和视图中执行此操作,我认为以下代码解决了问题http://jsfiddle.net/p9n8qq0x/14/

 for (var i = 0; i < data.length; i++) {
        console.log(data[i]);
        if (data[i].Sel === 'True') {
          var image="<img src='http://www.clker.com/cliparts/q/j/I/0/8/d/green-circle-icon-md.png' alt='green light' style='width:15px;height:15px'/>";
            data[i].Developed = image;
            $('#'+data[i].Country+' [aria-describedby="pays_grid_Developed"]').html(image);               
            data[i].Sel = 'False';
            grid.jqGrid('setRowData', i, data[i]);
            grid.jqGrid('saveRow', i, false);
        }
    }

【讨论】:

  • 抱歉 Sel 中的错误。非常感谢它有效。如何确保点击按钮后,编辑框(data[i].Sel)未选中。
  • 欢迎您,您可以通过更改以下代码来做到这一点 $('#'+data[i].Country+' [aria- describeby="pays_grid_Developed"]').html(image);并将复选框 attr 设置为 .removeAttr('checked')
猜你喜欢
  • 2012-08-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-04
  • 2012-09-22
  • 2011-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多