1

我有以下 html 表,其中包含行和列,每个行和列都包含一个带有文本的 td、一个选择和一个输入 [type='text']。每行还有一个最后一个 td 用于从表中删除行,与将表行保存为 json 无关,可以忽略。

   <table id="columnsTable" class="table white-table table-bordered table-hover table-striped">
    <thead>
        <tr>
            <th>Column</th>
            <th>Condition</th>
            <th>Value</th>
            <th></th>
        </tr>
    </thead>
    <tbody id="columnsTableBody">
        <tr id="StudentInfo.FirstName" class="StudentInfo.FirstName" name="StudentInfo.FirstName">
            <td>FirstName</td>
            <td>
                <select id="StudentInfo.FirstName-condition">
                    <option value="LIKE">CONTAINS</option>
                    <option value="=">EQUALS</option>
                    <option value="!=">NOT EQUAL</option>
                    <option value="&gt;">GREATER THAN</option>
                    <option value="&lt;">LESS THAN</option>
                    <option value="&gt;=">GREATER THAN OR EQUAL TO</option>
                    <option value="&lt;=">LESS THAN OR EQUAL TO</option>
                </select></td>
            <td>
                <input type="text" id="StudentInfo.FirstName-value" class="required" title="Value is required"></td>
            <td>
                <label>
                    <input type="checkbox" id="StudentInfo.FirstName-checkbox"></label></td>
        </tr>
        <tr id="StudentInfo.LastName" class="StudentInfo.LastName" name="StudentInfo.LastName">
            <td>LastName</td>
            <td>
                <select id="StudentInfo.LastName-condition">
                    <option value="LIKE">CONTAINS</option>
                    <option value="=">EQUALS</option>
                    <option value="!=">NOT EQUAL</option>
                    <option value="&gt;">GREATER THAN</option>
                    <option value="&lt;">LESS THAN</option>
                    <option value="&gt;=">GREATER THAN OR EQUAL TO</option>
                    <option value="&lt;=">LESS THAN OR EQUAL TO</option>
                </select></td>
            <td>
                <input type="text" id="StudentInfo.LastName-value" class="required" title="Value is required"></td>
            <td>
                <label>
                    <input type="checkbox" id="StudentInfo.LastName-checkbox"></label></td>
        </tr>
        <tr id="StudentInfo.CurrentCollege" class="StudentInfo.CurrentCollege" name="StudentInfo.CurrentCollege">
            <td>CurrentCollege</td>
            <td>
                <select id="StudentInfo.CurrentCollege-condition">
                    <option value="LIKE">CONTAINS</option>
                    <option value="=">EQUALS</option>
                    <option value="!=">NOT EQUAL</option>
                    <option value="&gt;">GREATER THAN</option>
                    <option value="&lt;">LESS THAN</option>
                    <option value="&gt;=">GREATER THAN OR EQUAL TO</option>
                    <option value="&lt;=">LESS THAN OR EQUAL TO</option>
                </select></td>
            <td>
                <input type="text" id="StudentInfo.CurrentCollege-value" class="required" title="Value is required"></td>
            <td>
                <label>
                    <input type="checkbox" id="StudentInfo.CurrentCollege-checkbox"></label></td>
        </tr>
        <tr id="StudentInfo.EmailAddress" class="StudentInfo.EmailAddress" name="StudentInfo.EmailAddress">
            <td>EmailAddress</td>
            <td>
                <select id="StudentInfo.EmailAddress-condition">
                    <option value="LIKE">CONTAINS</option>
                    <option value="=">EQUALS</option>
                    <option value="!=">NOT EQUAL</option>
                    <option value="&gt;">GREATER THAN</option>
                    <option value="&lt;">LESS THAN</option>
                    <option value="&gt;=">GREATER THAN OR EQUAL TO</option>
                    <option value="&lt;=">LESS THAN OR EQUAL TO</option>
                </select></td>
            <td>
                <input type="text" id="StudentInfo.EmailAddress-value" class="required" title="Value is required"></td>
            <td>
                <label>
                    <input type="checkbox" id="StudentInfo.EmailAddress-checkbox"></label></td>
        </tr>
        <tr id="StudentInfo.Status" class="StudentInfo.Status" name="StudentInfo.Status">
            <td>Status</td>
            <td>
                <select id="StudentInfo.Status-condition">
                    <option value="LIKE">CONTAINS</option>
                    <option value="=">EQUALS</option>
                    <option value="!=">NOT EQUAL</option>
                    <option value="&gt;">GREATER THAN</option>
                    <option value="&lt;">LESS THAN</option>
                    <option value="&gt;=">GREATER THAN OR EQUAL TO</option>
                    <option value="&lt;=">LESS THAN OR EQUAL TO</option>
                </select></td>
            <td>
                <input type="text" id="StudentInfo.Status-value" class="required" title="Value is required"></td>
            <td>
                <label>
                    <input type="checkbox" id="StudentInfo.Status-checkbox"></label></td>
        </tr>
    </tbody>
</table>

如何将行保存为以下格式的 JSON?,以便我可以循环返回它以从 JSON 重新构建表行。

columns: {
    StudentInfo.FirstName: {
       condition: 'CONTAINS',
       value: 'Carl'
    }
    StudentInfo.LastName: {
        condition: 'EQUALS',
        value: 'W'
    }
}
4

2 回答 2

1

将其存储为数组的集合,您可以循环通过它来重建表格 html。这也将减少内存使用量。而且我认为您应该将其重命名为,rows而不是columns因为它们是表格的理想行。

rows: [
    ['FirstName',  'CONTAINS',  'Carl'],
    ['LastName',  'EQUALS',  'W'],
];

用法:

var $table = $('tableSelector');
$.each(rows, function(i, obj){
   //Now use obj which is again an array and create the required html
   $table.append(...);
});
于 2013-01-10T20:18:41.910 回答
0

我认为这段代码会将您的值放入 JS 对象中。虽然我没有测试过

 var tbody = $("#columnsTable tbody");

var StudentInfo = {
    "FirstName" : {
        "title" : "FirstName", 
        "condition" : tbody.find("select[id='StudentInfo.FirstName-condition'] option:selected").val(), 
        "value" : tbody.find("input[id='StudentInfo.FirstName-value']").val(), 
        "checked" : (tbody.find("input[id='StudentInfo.FirstName-checkbox']:checked").length > 0)
    }, 
    "LastName" : {
        "title" : "LastName", 
        "condition" : tbody.find("select[id='StudentInfo.LastName-condition'] option:selected").val(), 
        "value" : tbody.find("input[id='StudentInfo.LastName-value']").val(), 
        "checked" : (tbody.find("input[id='StudentInfo.LastName-checkbox']:checked").length > 0)
    }, 
    "CurrentCollege" : {
        "title" : "CurrentCollege", 
        "condition" : tbody.find("select[id='StudentInfo.CurrentCollege-condition'] option:selected").val(), 
        "value" : tbody.find("input[id='StudentInfo.CurrentCollege-value']").val(), 
        "checked" : (tbody.find("input[id='StudentInfo.CurrentCollege-checkbox']:checked").length > 0)
    }, 
    "EmailAddress" : {
        "title" : "EmailAddress", 
        "condition" : tbody.find("select[id='StudentInfo.EmailAddress-condition'] option:selected").val(), 
        "value" : tbody.find("input[id='StudentInfo.EmailAddress-value']").val(), 
        "checked" : (tbody.find("input[id='StudentInfo.EmailAddress-checkbox']:checked").length > 0)
    }, 
    "Status" : {
        "title" : "Status", 
        "condition" : tbody.find("select[id='StudentInfo.Status-condition'] option:selected").val(), 
        "value" : tbody.find("input[id='StudentInfo.Status-value']").val(), 
        "checked" : (tbody.find("input[id='StudentInfo.Status-checkbox']:checked").length > 0)
    },
};

因为您在对象的 id 和类名中使用了句点,所以我认为 jQuery 会遇到问题。我在上面的代码中使用了一个选择器,它不应该对句点有任何问题。

编辑

修复了一些选择器

编辑 2

至于使用这个:alert(StudentInfo.CurrentCollege.condition);

于 2013-01-10T20:32:09.610 回答