我有这个数组,我在PHP中生成并存储到一个名为"$auction_additionalrevenue "的变量中。我使用bootstrap生成了一个非常漂亮的数据表。
我的最终目标是允许用户单击所述数据表中的一行,然后使用模态允许他们进行编辑(然后在表中创建一行)。
我已经多次使用FileMaker作为db源,其中表中的行是"相关数据"或用户正在查看的父记录的子记录。这一次,数据不是相关的,而是在父记录本身的"重复字段"中,我从中创建了一个多维数组。
在这个特定的实例中,我似乎不能得到正确的是如何在模型中向用户显示他们单击的特定行的数据。这很令人沮丧,因为我拥有多维数据(或嵌套数据?)存储在全局变量中的PHP数组。换句话说,我不需要回到数据库来获取数据,因为它已经在数组/变量中了。
我如何在渲染页面/Javascript/PHP之间传递所有这些数据?我可以很容易地通过"onclick"将记录/行回显到JS中,如下所示。
onClick = showadrev ()
当我这样做时,我可以将$mkey ($mkey = index/row)写入控制台日志或警告它没有问题。
接下来我想让模态弹出窗口显示嵌套在索引/行上的数组
这是我的数组:
Array
(
[0] => Array
(
[0] => Raffle
[1] => Pick of Live
[2] =>
[3] => 100
[4] => 150
[5] => Cocktail Only
[6] => Struggled a bit, offered some from the stage to hit the minimum
)
[1] => Array
(
[0] => Raffle
[1] => Ticket/Tangible
[2] => $1000 gift certificate to Canlis
[3] => 25
[4] => Unlimited
[5] => Cocktail Only
[6] => Really don't know how this did, but the box was pretty full of tickets
)
[2] => Array
(
[0] => Game
[1] => Ticket/Tangible
[2] => The Horse Race. Everyone is a winner. Receive a gift certificate ranging from $25 to $99.
[3] => 25
[4] => Unlimited
[5] => Cocktail Only
[6] => Great participation
)
[3] => Array
(
[0] => Other
[1] => Mystery/ Blind Pull
[2] => Wine Pull
[3] => 25
[4] => 50
[5] => cocktail only
[6] => There was about a dozen or so left
)
[4] => Array
(
[0] => Other
[1] => Ticket/Tangible
[2] => Centerpiece sales
[3] => 25
[4] => 35
[5] => Pre & During
[6] => Unknown
)
)
下面是我渲染的数据表:
<div class="wizard-step-3">
<fieldset>
<legend class="pull-left width-full">Aditional Revenue</legend>
<!-- <div class="table-responsive"> -->
<table id="data-table" class="table table-bordered table-striped" width="100%">
<thead>
<tr>
<th>Item</th>
<th>Type</th>
<th>Title</th>
<th>Price</th>
<th>Total Available</th>
<th>Sold By</th>
<th>Description</th>
<th>How did it Go?</th>
</tr>
</thead>
<tbody>
<?php foreach ($auction_additionalrevenues as $mkey => $adrevitems){
$no = $mkey + 1;
?><tr style="cursor:pointer;" onClick=showadrev(<?php echo $mkey ; ?>)><?php;
echo '<td>'.$no.'</td>';
echo '<td>'.$adrevitems[0].'</td>';
echo '<td>'.$adrevitems[1].'</td>';
echo '<td>'.$adrevitems[3].'</td>';
echo '<td>'.$adrevitems[4].'</td>';
echo '<td>'.$adrevitems[5].'</td>';
echo '<td>'.$adrevitems[2].'</td>';
echo '<td>'.$adrevitems[6].'</td>';
echo '</tr>';
} ?>
</tbody>
</table>
<!-- </div> -->
</fieldset>
</div>
你可以这样做
<script type="text/javascript" >
var data = <?php echo json_encode( $array ); ?>;
</script>
json数据看起来像这样
[["Raffle","Pick of Live","",100,150,"Cocktail Only", "Struggled a bit, offered some from the stage to hit the minimum"], .... ]
这将/应该是一个有效的Javascript数组,只要它没有行返回。如果您从DB json_encode(false)
而不是实际的数组中提取它,您将需要确保您的非json编码类似于false。
如果你把这个插入页面你基本上会得到这样的东西
<script type="text/javascript" >
var data = [["Raffle","Pick of Live","",100,150,"Cocktail Only", "Struggled a bit, offered some from the stage to hit the minimum"], .... ];
</script>
我不确定如果这是你想要的,你也可以添加到表中使用数据属性,像这样
<td class="data_row" data-array="<?php echo implode(',', $array[0]); ?>" >
然后您可以通过选择该td,取出数据并使用split来获得它。
var rowData = $('td.data_row').data('array').split(',');
Implode将使其成为逗号分隔的列表,split类似于PHP的explosion,使其返回为数组。虽然,我不确定是否可以将数组直接放入data属性中(我对此表示怀疑),并避免这样做。
如果你也想要键,它有点难,因为在JS中它们将是对象,但在这种情况下,你需要先将它们从DB中拉出来。(关联数组VS JS对象)
对于现有的结构你也可以像这样使用jQuery
$('tr.row').click( function(event) {
var rowData = [];
$(this).find('td').each( function(i,v){
rowData.push($(this).text());
});
//do somthing with rowData
});
这基本上是找到点击行(添加了一个row类,使它更容易),然后找到所有的子td,然后循环,并添加其中的文本到一个数组
艺术学的答案应该是这个问题的公认答案。下面是我如何实现它的。
//echo $system_id;
//echo $system_auth_id;
print_r(json_encode($AuthData));
我留下了注释的东西,这样你就可以看到我在做什么之前,我读到这篇文章,并意识到我需要在PHP端编码之前,把它发送到我的JS应用程序。
结果:[{"system_id":"61a694d0-3605-4502-952b-38d87b451a56","system_auth_id":"caa5906f-d9ae-4297-8e9f-5ea8d9ed8b51","system_lastauth_id":"ace681bb-48f5-4831-a23d-6608c696f264","system_rundate":"2019-04-27T22:46:07.090Z","system_auth_lastrundate":"2019-04-27T22:45:57.932Z","system_auth_updated_func":"main_init_auth_extend","system_auth_lastupdateddate":"2019-04-27T22:45:57.932Z","system_auth_lastupdated_func":"main_init_auth_extend","system_auth_request":"0","system_auth_request_lastfunc":"n'/a","system_auth_denials":"0","system_auth_denial_lastfunc":"n'/a","system_auth_success":"0","system_auth_success_lastfunc":"n'/a"}]
先前的结果:
Array( [0] => stdClass Object ( [system_id] => 61a694d0-3605-4502-952b-38d87b451a56 [system_auth_id] => caa5906f-d9ae-4297-8e9f-5ea8d9ed8b51 [system_lastauth_id] => ace681bb-48f5-4831-a23d-6608c696f264 [system_rundate] => 2019-04-27T22:46:07.090Z [system_auth_lastrundate] => 2019-04-27T22:45:57.932Z [system_auth_updated_func] => main_init_auth_extend [system_auth_lastupdateddate] => 2019-04-27T22:45:57.932Z [system_auth_lastupdated_func] => main_init_auth_extend [system_auth_request] => 0 [system_auth_request_lastfunc] => n/a [system_auth_denials] => 0 [system_auth_denial_lastfunc] => n/a [system_auth_success] => 0 [system_auth_success_lastfunc] => n/a ) )