ユーザーがCSVファイルをアップロードすると、製品リストが表示されるhtmlテーブルがあります。多くの場合、テーブルには1,000行を超える行があります。問題は、ユーザーが[送信]をクリックしたときに、すべての行の入力の1つに重複するエントリがないかどうかを確認する必要があることです。これには非常に長い時間がかかり、多くの場合、応答しないスクリプトの警告が表示されます。
重複をチェックするために使用しているスクリプトは次のとおりです。
// 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>