【问题标题】:How to convert HTML table with select and input to json and back again如何将带有选择和输入的 HTML 表转换为 json 并再次返回
【发布时间】:2013-01-11 04:38:11
【问题描述】:

我有下面的 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'
    }
}

【问题讨论】:

    标签: javascript jquery html json asp.net-mvc-4


    【解决方案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(...);
    });
    

    【讨论】:

    • 谢谢,这正是我一直在寻找的……现在只是想弄清楚如何遍历行并获取每个子数组所需的值。
    • obj[0] =&gt; "FirstName", obj[1] =&gt; "CONTAINS" ... 用于第一次迭代。为了更好的可读性,您可以定义以原始 JSON 对象字段命名的常量:NAME = 0, CONDITION = 1, VAL = 2,因此您可以使用obj[VAL]。 (在第一步中持有"Carl"
    【解决方案2】:

    我认为这段代码会将你的值放入一个 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);.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      • 1970-01-01
      • 1970-01-01
      • 2015-01-19
      • 2013-02-18
      相关资源
      最近更新 更多