Hi @Ashok Kumar,
As mentioned by AgaveJoe, it would be a better choice to use the corresponding API(DataTable.row.add()
). Based on the code you provided, I did a simple test.
Here is the code:
<head runat="server">
<title></title>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdn.datatables.net/2.0.6/css/dataTables.dataTables.min.css" rel="stylesheet" />
<script></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="sb_add" runat="server" OnClientClick="addNewRow();return false;" Text="+ ADD" CssClass="btn btn-primary w-100 w-md-auto text-center px-4" />
<%--<button type="button" id="sb_add" onclick="addNewRow()" value="+ ADD" class="btn btn-primary w-100 w-md-auto text-center px-4"></button>--%>
<table class="table table-borderless rowSpace table-round-row dtr-inline SB_table dataTable nowrap "
id="strastrategy_builder_datatable" style="width: 100%;">
<thead>
<tr>
<th>Action</th>
<th>Instrument</th>
<th>Expiry</th>
<th>Strike</th>
<th>Qty</th>
<th>Future Price</th>
<th>Price</th>
<th>Delta</th>
<th>Gamma</th>
<th>Theta</th>
<th>Vega</th>
<th>IV</th>
<th data-dt-order="disable"></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div class="sbnolegs">NO LEGS ADDED</div>
</div>
</form>
<script src="Scripts/jquery-3.4.1.js"></script>
<script src="https://cdn.datatables.net/2.0.6/js/dataTables.min.js"></script>
<script src="Scripts/bootstrap.min.js"></script>
<script>
$("#strastrategy_builder_datatable").find("th").off("click.DT");
const table = $('#strastrategy_builder_datatable').DataTable({
ordering: true,
info: false,
paging: false,
searching: false,
responsive: true,
language: {
emptyTable: ' ',
},
columnDefs: [
{
width: '100px',
responsivePriority: 1,
targets: 0
},
{
className: 'dt-center',
width: '50px',
responsivePriority: 2,
targets: -1
},
{
width: '110px',
// responsivePriority: 3,
targets: -2
},
{
width: '110px',
responsivePriority: 4,
targets: -3
},
{
width: '110px',
responsivePriority: 5,
targets: -4
},
{
width: '110px',
responsivePriority: 6,
targets: -5
},
{
width: '110px',
responsivePriority: 7,
targets: -6
},
{
width: '100px',
responsivePriority: 8,
targets: -7
},
{
width: '100px',
responsivePriority: 9,
targets: -8
},
{
width: '120px',
responsivePriority: 10,
targets: -9
},
{
width: '120px',
// responsivePriority: 10,
targets: -10
},
{
width: '160px',
// responsivePriority: 10,
targets: -11
},
{
width: '100px',
// responsivePriority: 10,
targets: -12
}
]
});
$.fn.dataTable.ext.errMode = 'none';
let counter = 1;
function addNewRow() {
table.row.add([
"Action" + counter,
"Instrument" + counter,
"Expiry" + counter,
"trike" + counter,
"Qty" + counter,
"Future Price" + counter,
"Price" + counter,
"Delta" + counter,
"Gamma" + counter,
"Theta" + counter,
"Vega" + counter,
"IV" + counter
]).draw(false);
counter++;
}
</script>
</body>
</html>
Result:
Among them, you need to get the values of each field to be added as parameter in add()
function, like this: [param1, param2, param3...]
.
In addition, you need to prevent postback when clicking the button, you can use return false
after calling the add()
function or using the button and specify the type as button
instead of the default "submit".
Best regards,
Xudong Peng
If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.