简体   繁体   English

使用jQuery清空具有相同数据的行的以下单元格

[英]Emptying the following cell of row having the same data using jquery

I have this simple fiddle: http://jsfiddle.net/anilca/kbfssbd4/ 我有一个简单的小提琴: http : //jsfiddle.net/anilca/kbfssbd4/

var dataArr = [
    { DayNum: 0, Day: "Sunday", Group: "A" },
    { DayNum: 1, Day: "Sunday", Group: "B" },
    { DayNum: 2, Day: "Sunday", Group: "C" },
    { DayNum: 3, Day: "Monday", Group: "B" },
    { DayNum: 4, Day: "Monday", Group: "A" },
    { DayNum: 5, Day: "Tuesday", Group: "C" },
    { DayNum: 6, Day: "Tuesday", Group: "B" }
];
var grid = $("#grid").kendoGrid({
    dataSource: {
        data: dataArr,
        sort: [
            { field: "DayNum", dir: "asc" },
            { field: "Group", dir: "asc" }
        ],
        schema: {
            model: {
                fields: {
                    DayNum: { type: "number" },
                    Day: { type: "string" },
                    Group: { type: "string" }
                }
            }
        }
    },
    selectable: true,          
    columns: [
        {
            field: "Day",
            title: "Day"
        },
        {
            field: "Group",
            title: "Group"
        }
    ]
}).data("kendoGrid");

I want to show empty cells for Day column for the row after the first appearance of the appropriate day. 我想在适当的一天的首次出现后,为该行的“天”列显示空单元格。 I mean my aim is to have the following screenshot without manipulating the data: 我的意思是我的目标是在不操作数据的情况下获得以下屏幕截图:

在此处输入图片说明

I thinks it's possible to do that using jquery after building the kendo grid. 我认为有可能在构建kendo网格之后使用jquery来做到这一点。 But I couldn't find out how to do it. 但是我不知道该怎么做。

Thanks in advance, 提前致谢,

I would do it using schema.parse method. 我会使用schema.parse方法。 This method manipulates the data but doesn't changes it, which is good. 此方法可以处理数据,但不会更改数据,这很好。 And no jQuery needed: 而且不需要jQuery:

parse: function(data) {
    var lastDay = "";

    for (var i = 0; i < data.length; i++) {
        if (lastDay == "" || data[i].Day != lastDay) {
            lastDay = data[i].Day;
        }
        else {
            data[i].Day = "";
        }
    }

    return data;
}

Updated Fiddle . 更新小提琴

UPDATE: 更新:

Yeah, I was wrong when I said that the parse doesn't changes the data. 是的,当我说parse 不会更改数据时,我错了。 IN fact it changes. 实际上,它发生了变化。 What you can do is to create dummy property and manipulate it: 您可以做的是创建虚拟属性并对其进行操作:

parse: function(data) {
    var lastDay = "";

    for (var i = 0; i < data.length; i++) {
        if (lastDay == "" || data[i].Day != lastDay) {
            lastDay = data[i].Day;
            data[i].DayText = data[i].Day;
        }
        else {
            data[i].DayText = "";
        }
    }

    return data;
}

See that I'm using(and creating) DayText this time. 看到我这次正在使用(并创建) DayText Your first column definition changes to: 您的第一列定义更改为:

{
    field: "DayText",
    title: "Day"
}

So it keeo the Day property as is. 因此,它保持Day属性不变。 Fiddle . 小提琴

I threw a jQuery-each function at the end that seems to do the trick, try adding it to the bottom of your javascript: 我在最后添加了一个jQuery-each函数,似乎可以解决问题,请尝试将其添加到javascript的底部:

var day = "";  // holds latest day value

// for each row...
$('tr').each(function(){
    var thisDay = $(this).find('td:first').html();  // grab the text from the first column
    if (thisDay == day)  // if its the same as last time
    {
        $(this).find('td:first').html('');  // hide it
    }
    day = thisDay;  // hold onto this day
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM