Как передать в заголовок кнопки значение из JSON ответа
Не мастер писать статьи. прилагаю куски кода. Как из массива json array_stat получить значения и передать их в названия кнопок в datatables?
Читал что id не отрабатывают(https://datatables.net/forums/discussion/49533/how-to-attach-an-id-to-a-button), поэтому использую разные классы.
Понимаю что ответ json приходит после генерации/прогрузки в браузере таблицы с кнопками(и названиями разумеется)
wt_st = "<? echo '?wtol_status='.$_GET[wtol_status];?>";
table_omts_tmc_ajax = $('#datatable_all_omts_tmc_ajax').DataTable({
orderCellsTop: true,
fixedHeader: true,
aoColumnDefs: [
{"sWidth": "1%", "aTargets": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]},
{
"targets": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16<? if ($tmc_rules_on) {
echo ",17";
}?>], "orderable": false
}
],
//dom: 'Bfrtp',
lengthMenu: [[10, 25, 50, 100, -1], [10, 25, 50, 100, "Все"]],
dom: 'Bflrtip',
pageLength: <? echo $a_long_table;?>,
//deferRender: true,
scrollY: 650,
scrollX: 650,
//width:400,
scrollCollapse: true,
scroller: true,
<? if ($tmc_rules_on) { ?>order: [2, "desc"],<?} else { ?>order: [1, "desc"],<?} ?>
sorting: false,
language: {
url: "js/Russian.lang"
},
"processing": true,
ajax: "tmc_omts_ajax_table_fetch.php"+wt_st,
"columns": [
<? if ($tmc_rules_on) { ?>{data: 'wtol_copy'},<?} ?>
{data: 'wtol_id'},
{data: 'wtol_status'},
{data: 'wtol_data_creat'},
{data: 'wtol_where'},
{data: 'wtol_zplace'},
{data: 'wtol_tmc_name'},
{data: 'wtol_tmc_kolvo'},
{data: 'wtol_price'},
{data: 'wtol_rid'},
{data: 'wtol_pplace'},
{data: 'wtol_dpost'},
{data: 'wtol_pka'},
{data: 'wtol_ns4et'},
{data: 'wtol_pay_data'},
{data: 'wtol_gartal'},
{data: 'wtol_mol_id'},
{data: 'wtol_data_close'}
],
rowCallback: function( row, data, index ) {
if (data.wtol_status_orig == '-14') {
$('td', row).css('background-color', 'MistyRose');
}
if (data.wtol_status_orig == '11') {
$('td', row).css('background-color', 'Seashell');
}
if (data.wtol_status_orig == '0') {
$('td', row).css('background-color', 'Beige');
}
},
buttons:
[
{
text: 'Новые',//+wt_st,
className: "bt_tmc_omts wtol_status0",
action : function () {
location.href = 'tmc_omts_ajax_table.php?wtol_status=0';
}
},
{
text: 'Закупка',
className: "bt_tmc_omts wtol_status1",
action : function () {
location.href = 'tmc_omts_ajax_table.php?wtol_status=1';
}
},
{
text: 'Перемещение',
className: "bt_tmc_omts wtol_status2",
action : function () {
location.href = 'tmc_omts_ajax_table.php?wtol_status=2';
}
},
{
text: 'Оплата',
className: "bt_tmc_omts wtol_status3",
action : function () {
location.href = 'tmc_omts_ajax_table.php?wtol_status=3';
}
},
{
text: 'Выполненные',
className: "bt_tmc_omts wtol_status4",
action : function () {
location.href = 'tmc_omts_ajax_table.php?wtol_status=4';
}
},
{
text: 'Отклонённые',
className: "bt_tmc_omts wtol_status-14",
action : function () {
location.href = 'tmc_omts_ajax_table.php?wtol_status=-14';
}
},
{
text: 'Все',
className: "bt_tmc_omts wtol_status99",
action : function () {
location.href = 'tmc_omts_ajax_table.php';
}
},
<? if($tmc_rules_on) { ?>
{
text: 'Добавить',
className: 'stat_bt_last',
action: function (e, dt, node, config) {
document.getElementById('add_new_omts_tmc').reset();
document.getElementById("update_omts_tmc").value = "0";
document.getElementById("myModalLabel").textContent = "Новая Заявка";
document.getElementById("tmc_pay").style.display = 'none';
document.getElementById("div_fakt_post").style.display = 'none';
document.getElementById("div_fast_zav").style.display = 'none';
/*document.getElementById("create_new_omts_tmc").style.display = 'initial';
document.getElementById("transport_omts_tmc").style.display = 'initial';*/
document.getElementById("null_new_omts_tmc").style.display = 'initial';
document.getElementById("close_omts_tmc").style.display = 'none';
document.getElementById("pay_omts_tmc").style.display = 'none';
document.getElementById("delete_omts_tmc").style.display = 'none';
document.getElementById("new_tmc_not_edit").classList.remove("show-grid");
document.getElementById("tmc_pay").classList.remove("show-grid");
document.getElementById("div_fakt_post").classList.remove("show-grid");
$('#add_omts_tmc_ajax').modal('show');
}
},
<? } else { ?>
{
text: 'Добавить',
className: 'stat_bt_last',
action: function (e, dt, node, config) {
document.getElementById('add_new_omts_tmc').reset();
document.getElementById("update_omts_tmc").value = "0";
document.getElementById("myModalLabel").textContent = "Новая Заявка";
document.getElementById("tmc_pay").style.display = 'none';
document.getElementById("div_fakt_post").style.display = 'none';
document.getElementById("div_fast_zav").style.display = 'none';
document.getElementById("search_rpd_users_itr").value = "<? echo $aname;?>";
document.getElementById("search_rpd_users_itr").readOnly = true;
document.getElementById("wtol_status").value = "0";
document.getElementById("null_new_omts_tmc").style.display = 'initial';
document.getElementById("null_new_omts_tmc").textContent = 'Создать';
document.getElementById("new_tmc_not_edit").classList.remove("show-grid");
document.getElementById("tmc_pay").classList.remove("show-grid");
document.getElementById("div_fakt_post").classList.remove("show-grid");
$('#add_omts_tmc_ajax').modal('show');
}
},
<? } ?>
/*{
text : 'Обновить',
action: function{table_omts_tmc_ajax.ajax.reload();}
},*/
{
extend: 'copy',
text: 'В Буфер',
}, 'excel', 'pdf',
{
extend: 'print',
text: 'Печать',
},
<? if(($aidusers==28)||($aidusers==8)||($aidusers==1)) { ?>
{
text: 'Согласование',
action : function () {
location.href = 'tmc_access.php';
}
},
],
});
setInterval( function () {
table_omts_tmc_ajax.ajax.reload();
}, 60000 );
// Setup - add a text input to each footer cell
$('#datatable_all_omts_tmc_ajax thead tr').clone(true).appendTo('#datatable_all_omts_tmc_ajax thead');
$('#datatable_all_omts_tmc_ajax thead tr:eq(1) th').each(function (i) {
var title = $(this).text();
$(this).html('<input type="text" placeholder="" />');
$('input', this).on('keyup change', function () {
if (table_omts_tmc_ajax.column(i).search() !== this.value) {
table_omts_tmc_ajax
.column(i)
.search(this.value)
.draw();
}
});
});
И кусок скрипта генерации json
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_assoc($result)) {
$array_stat[$row[wtol_status]]++;
}
/*
bla bla bla
*/
$dataset = array(
"echo" => 1,
"totalrecords" => count($array),
"totaldisplayrecords" => count($array),
"array_stat" =>$array_stat,
"data" => $array
);
echo json_encode($dataset);