检查重复项时的jQuery性能问题

时间:2013-02-15 15:18:50

标签: javascript jquery performance jsp html-table

我有一个html表,当用户上传CSV文件时,该表填充了产品列表。通常,该表将有超过一千行。问题是,当用户点击“提交”时,我必须在每行的一个输入中检查重复的条目。这需要花费很长时间,而且经常会收到无响应的脚本警告。

这是我用来检查重复项的脚本:

// Check for duplicate products
    $('#match_table').submit(function(evt) {
        var arr = document.getElementsByName("pid[]");
        var sorted_arr =  new Array();
        for (var i = 0; i < arr.length; i++) {
            sorted_arr[i] = new Array();
            sorted_arr[i][0] = arr[i].value;
            sorted_arr[i][1] = arr[i].id;
        }
        sorted_arr.sort((function(index){
            return function(a, b){
                return (a[index] === b[index] ? 0 : (a[index] < b[index] ? -1 : 1));
            };
        })(0));
        var results = [];
        for (var i = 0; i < sorted_arr.length - 1; i++) {
            if (sorted_arr[i + 1][0] == sorted_arr[i][0]) {
                results.push(sorted_arr[i][0]);
                evt.preventDefault();
                document.getElementById(sorted_arr[i][1]).style.border = "1px solid red";
                document.getElementById(sorted_arr[i+1][1]).style.border = "1px solid red";
            }
        }
        if(results.length != 0){
            return false;
        }
    });

有关如何提高此脚本性能的任何想法/示例?或者如何解决这个问题?

编辑:HTML的片段

<form id="match_table" action="tool_tmaxupload.jsp" name="match_table" method="post">
                <table class="nozebra">
                    <thead>
                        <tr>
                            <th class="essential"></th>
                            <th class="essential">CSV Product</th>
                            <th class="essential">CSV Desc</th>
                            <th class="essential">Match Product</th>
                            <th class="essential">Match Desc</th>
                            <th class="essential">Match Vend</th>
                            <th class="essential">Amount</th>
                            <th class="essential">To Rebate</th>
                            <th class="essential">To Growth</th>
                            <th class="essential">Paid On What</th>
                        </tr>
                    </thead>
                    <tbody>

                                <tr class=even>
                                    <td><input type="checkbox" name="checkbox[]" class="checkall"></td>
                                    <td>343601</td>
                                    <td>TEST</td>
                                    <td><input type="text" size="8" name="pid[]" id="pid_0" value="343601"/></td>
                                    <td>COMBO LUNCH MEAT</td>
                                    <td><input type="text" size="8" name="ve[]" value="672" /> FARMLAND FOODS INC</td>
                                    <td><input type="text" size="5" name="ramt[]" value="5.5"/></td>
                                    <td>
                                        <select name="torebate[]" id="torebate">
                                            <option value="0" >No</option>
                                            <option value="1"  selected=selected>Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="togrowth[]" id="togrowth">
                                            <option value="0"  selected=selected>No</option>
                                            <option value="1" >Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="pow[]" id="pow">
                                            <option value="1"  selected=selected>% of FOB Cost</option>
                                            <option value="2" >% of Delivered Cost</option>
                                            <option value="3" >$ Per Case</option>
                                            <option value="4" >$ Per Pound</option>
                                            <option value="5" >% of Sold For Price</option>
                                        </select>
                                    </td>
                                </tr>

                                <tr class=odd>
                                    <td><input type="checkbox" name="checkbox[]" class="checkall"></td>
                                    <td>561720</td>
                                    <td>TEST</td>
                                    <td><input type="text" size="8" name="pid[]" id="pid_1" /></td>
                                    <td></td>
                                    <td><input type="text" size="8" name="ve[]" /></td>
                                    <td><input type="text" size="5" name="ramt[]" value="6.5"/></td>
                                    <td>
                                        <select name="torebate[]" id="torebate">
                                            <option value="0"  selected=selected>No</option>
                                            <option value="1" >Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="togrowth[]" id="togrowth">
                                            <option value="0" >No</option>
                                            <option value="1"  selected=selected>Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="pow[]" id="pow">
                                            <option value="1" >% of FOB Cost</option>
                                            <option value="2"  selected=selected>% of Delivered Cost</option>
                                            <option value="3" >$ Per Case</option>
                                            <option value="4" >$ Per Pound</option>
                                            <option value="5" >% of Sold For Price</option>
                                        </select>
                                    </td>
                                </tr>

                                <tr class=even>
                                    <td><input type="checkbox" name="checkbox[]" class="checkall"></td>
                                    <td>193420</td>
                                    <td>TEST</td>
                                    <td><input type="text" size="8" name="pid[]" id="pid_2" value="346770"/></td>
                                    <td>HAM SPIRAL SLIC disc use346778</td>
                                    <td><input type="text" size="8" name="ve[]" value="672" /> FARMLAND FOODS INC</td>
                                    <td><input type="text" size="5" name="ramt[]" value="7.5"/></td>
                                    <td>
                                        <select name="torebate[]" id="torebate">
                                            <option value="0" >No</option>
                                            <option value="1"  selected=selected>Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="togrowth[]" id="togrowth">
                                            <option value="0"  selected=selected>No</option>
                                            <option value="1" >Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="pow[]" id="pow">
                                            <option value="1" >% of FOB Cost</option>
                                            <option value="2" >% of Delivered Cost</option>
                                            <option value="3"  selected=selected>$ Per Case</option>
                                            <option value="4" >$ Per Pound</option>
                                            <option value="5" >% of Sold For Price</option>
                                        </select>
                                    </td>
                                </tr>

                                <tr class=even>
                                    <td><input type="checkbox" name="checkbox[]" class="checkall"></td>
                                    <td>193420</td>
                                    <td>TEST</td>
                                    <td><input type="text" size="8" name="pid[]" id="pid_3" value="346771"/></td>
                                    <td>HAM SPIRAL SLICED BONE IN</td>
                                    <td><input type="text" size="8" name="ve[]" value="672" /> FARMLAND FOODS INC</td>
                                    <td><input type="text" size="5" name="ramt[]" value="7.5"/></td>
                                    <td>
                                        <select name="torebate[]" id="torebate">
                                            <option value="0" >No</option>
                                            <option value="1"  selected=selected>Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="togrowth[]" id="togrowth">
                                            <option value="0"  selected=selected>No</option>
                                            <option value="1" >Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="pow[]" id="pow">
                                            <option value="1" >% of FOB Cost</option>
                                            <option value="2" >% of Delivered Cost</option>
                                            <option value="3"  selected=selected>$ Per Case</option>
                                            <option value="4" >$ Per Pound</option>
                                            <option value="5" >% of Sold For Price</option>
                                        </select>
                                    </td>
                                </tr>

                                <tr class=odd>
                                    <td><input type="checkbox" name="checkbox[]" class="checkall"></td>
                                    <td>191239</td>
                                    <td>TEST</td>
                                    <td><input type="text" size="8" name="pid[]" id="pid_4" value="346778"/></td>
                                    <td>HAM SPIRAL SLICED BONE IN</td>
                                    <td><input type="text" size="8" name="ve[]" value="672" /> FARMLAND FOODS INC</td>
                                    <td><input type="text" size="5" name="ramt[]" value="8.5"/></td>
                                    <td>
                                        <select name="torebate[]" id="torebate">
                                            <option value="0"  selected=selected>No</option>
                                            <option value="1" >Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="togrowth[]" id="togrowth">
                                            <option value="0"  selected=selected>No</option>
                                            <option value="1" >Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="pow[]" id="pow">
                                            <option value="1" >% of FOB Cost</option>
                                            <option value="2" >% of Delivered Cost</option>
                                            <option value="3" >$ Per Case</option>
                                            <option value="4"  selected=selected>$ Per Pound</option>
                                            <option value="5" >% of Sold For Price</option>
                                        </select>
                                    </td>
                                </tr>

                                <tr class=even>
                                    <td><input type="checkbox" name="checkbox[]" class="checkall"></td>
                                    <td>448584</td>
                                    <td>TEST</td>
                                    <td><input type="text" size="8" name="pid[]" id="pid_5" /></td>
                                    <td></td>
                                    <td><input type="text" size="8" name="ve[]" /></td>
                                    <td><input type="text" size="5" name="ramt[]" value="9.5"/></td>
                                    <td>
                                        <select name="torebate[]" id="torebate">
                                            <option value="0" >No</option>
                                            <option value="1"  selected=selected>Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="togrowth[]" id="togrowth">
                                            <option value="0" >No</option>
                                            <option value="1"  selected=selected>Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="pow[]" id="pow">
                                            <option value="1"  selected=selected>% of FOB Cost</option>
                                            <option value="2" >% of Delivered Cost</option>
                                            <option value="3" >$ Per Case</option>
                                            <option value="4" >$ Per Pound</option>
                                            <option value="5" >% of Sold For Price</option>
                                        </select>
                                    </td>
                                </tr>

                        </tbody>
                        <tfoot>
                            <tr>
                                <th colspan="10"><input type="hidden" name="rbid" value="10034" /></th>
                            </tr>
                        </tfoot>
                </table>
                <input class="check" type="button" value="Check all" />
                <input id="AddRow" type="button" value="Add">
                <input id="DeleteRow" type="button" value="Delete">
                <input type="submit" name="submit" value="Load" />
            </form>

0 个答案:

没有答案