1

我有下表。 在此处输入图像描述

我想从列标题中删除城市名称并将其放在顶部。如下: 在此处输入图像描述

带有城市名称的列是在代码中动态添加的,并且可以是任何数字,即 0、1 到任何数字。

表结构为:

<table class="gvv1" cellspacing="0" rules="all" border="1" id="MainContent_gvActivities"
style="border-collapse: collapse;">
<tr style="background-color: buttonface;">
    <th scope="col">
        &nbsp;
    </th>
    <th scope="col">
        Cluster
    </th>
    <th scope="col">
        Activity
    </th>
    <th scope="col">
        Data
    </th>
    <th scope="col">
        <b>London_Tasks</b>
    </th>
    <th scope="col">
        <b>London_Achieved</b>
    </th>
    <th scope="col">
        <b>Geneva_Tasks</b>
    </th>
    <th scope="col">
        <b>Geneva_Achieved</b>
    </th>
</tr>
<tr>
    <td>
        <input id="chkSelected_0" type="checkbox" />
    </td>
    <td style="width: 50px; white-space: nowrap;">
        ER.
    </td>
    <td style="width: 50px; white-space: nowrap;">
        Provision
    </td>
    <td style="width: 60px; white-space: nowrap;">
        number
    </td>
    <td>
        <input name="ctl00Activitiesl00" type="text" value="-1.00" />
    </td>
    <td>
        <input name="ctl00Activitiesl01" type="text" value="-1.00" />
    </td>
    <td>
        <input name="ctl00Activitiesl02" type="text" value="-1.00" />
    </td>
    <td>
        <input name="ctl00Activitiesl03" type="text" value="-1.00" />
    </td>
</tr>
<tr>
    <td>
        <input id="chkSelected_1" type="checkbox" />
    </td>
    <td style="width: 50px; white-space: nowrap;">
        ER.
    </td>
    <td style="width: 50px; white-space: nowrap;">
        support
    </td>
    <td style="width: 60px; white-space: nowrap;">
        campaign (Numbers)
    </td>
    <td>
        <input name="ctl0Activities200" type="text" value="-1.00" />
    </td>
    <td>
        <input name="ctl0Activities201" type="text" value="-1.00" />
    </td>
    <td>
        <input name="ctl0Activities202" type="text" value="-1.00" />
    </td>
    <td>
        <input name="ctl0Activities203" type="text" value="-1.00" />
    </td>
</tr>      
</table>
4

2 回答 2

0

一、jsFiddle:http: //jsfiddle.net/52jZC/

这使用 jQuery 并且将动态地与任何具有相同结构的表一起使用类名gvv1。您可以根据需要进行更改:

<script>
// This function was taking from here: http://stackoverflow.com/questions/2815683/jquery-javascript-replace-tag-type
(function($) {    
        $.fn.replaceTagName = function(replaceWith) {
                var tags = [],
                        i    = this.length;
                while (i--) {
                        var newElement = document.createElement(replaceWith),
                                thisi      = this[i],
                                thisia     = thisi.attributes;
                        for (var a = thisia.length - 1; a >= 0; a--) {
                                var attrib = thisia[a];
                                newElement.setAttribute(attrib.name, attrib.value);
                        };
                        newElement.innerHTML = thisi.innerHTML;
                        $(thisi).after(newElement).remove();
                        tags[i - 1] = newElement;
                }
                return $(tags);
        };
})(window.jQuery);

$(function() {
        // This formats each table w/ the class 'gvv1' as specified in your question
            $('.gvv1').each(function(i, v) {
            city1 = $(this).find('th:eq(4)').text().split('_');
            city1 = city1[0];
            var city2 = $(this).find('th:eq(6)').text().split('_');
            city2 = city2[0];
            $(this).find('th:eq(4)').text('Tasks');
            $(this).find('th:eq(5)').text('Achieved');
            $(this).find('th:eq(6)').text('Tasks');
            $(this).find('th:eq(7)').text('Achieved');

            $(this).find('th').replaceTagName('td');

            $(this).prepend('<thead><tr><th colspan="4">&nbsp;</th><th colspan="2">' + city1 + '</th><th colspan="2">' + city2 + '</th></tr></thead>');

        });
});
于 2013-02-11T16:31:08.163 回答
0

您将需要更改

<th>'s

到一个

<td>

首先并适当地为该行指定一个类和样式,以使其看起来像您想要的标题

<tr class="NewClass" style="background-color: buttonface;">
    <td scope="col">
        &nbsp;
    </td>
    <td scope="col">
        Cluster
    </td>
    <td scope="col">
        Activity
    </td>
    <td scope="col">
        Data
    </td>
    <td scope="col">
        <b>London_Tasks</b>
    </td>
    <td scope="col">
        <b>London_Achieved</b>
    </td>
    <td scope="col">
        <b>Geneva_Tasks</b>
    </td>
    <td scope="col">
        <b>Geneva_Achieved</b>
    </td>
</tr>

当触发更改表的事件时使用 .before

$('TriggerEntity').click(function(){

('.NewClass').before('<tr><th>&nbsp</th><th>London</th>...');

});
于 2013-02-11T15:55:00.200 回答