编辑:删除了我的YouTube视频,这是简单的解决方案:
var activitiesDatabase = {"Categories": [
{
"CategoryId": "cashFlow",
"Activities": [
{"ActivityName": "Measure and analyse cash flows", "EstimatedTime": 20},
{"ActivityName": "Measure and analyse cash flows", "EstimatedTime": 20},
]
},
{
"CategoryId": "growthStrategies",
"Activities": [
{"ActivityName": "Research market and identify trends", "EstimatedTime": 30},
{"ActivityName": "Research market and identify trends", "EstimatedTime": 30},
]
},
]};
// Next method receive the array of categories, a category ID, and return
// the list of "Activity Names" related to that category.
const getActivityNamesByCategory = (catArray, catID) =>
{
// Check arguments.
catArray = (catArray.constructor === Array) ? catArray : [];
// Generate output list.
let actNames = catArray.reduce((res, curr) =>
{
if (curr.CategoryId == catID)
curr.Activities.map((x) => res.push(x.ActivityName));
return res;
},
[]);
return actNames;
};
// Test 1
let out1 = getActivityNamesByCategory(activitiesDatabase.Categories, "cashFlow");
console.log(out1);
// Test 2
let out2 = getActivityNamesByCategory(activitiesDatabase.Categories, "growthStrategies");
console.log(out2);
// Test wrong input 1
let out3 = getActivityNamesByCategory(activitiesDatabase, "asd");
console.log(out3);
// Test wrong input 2
let out4 = getActivityNamesByCategory(activitiesDatabase.Categories);
console.log(out4);
“工作代码”:
$(document).on('click', '.edit_btn', function() {
var rowData = $('#example').DataTable().row($(this).parents('tr')).data();
});
我尝试过的事情 我在youTube视频中尝试了很多事情。我无法在链接上使用原始示例。
我应该这样使用Jquery吗?
<script type="text/javascript">
$(document).on('click','.edit_btn',function (){
var id = $(this).attr("id").match(/\d+/)[0];
var edit_id = $('#example').DataTable().row( id ).data();
var edit_id = edit_id[0];
$.ajax({
url: 'index.php',
datatype: "json",
data: { edit_id : edit_id },
async: false,
success: function(result) {
//alert(edit_id);
$("#edit_id").val(edit_id);
} //success func
}); //ajax
}); //end ready
</script>
还是这样的php?
var last_name = $('#example').DataTable().row( last_name ).data();
ajax文件中有什么?
答案 0 :(得分:0)
您可以尝试获取父tr元素,尽管我不确定这是否是最好的方法。
简体:
$(document).on('click', '.edit_btn', function() {
var rowData = $('#example').DataTable().row($(this).parents('tr')).data();
});
基本上,您可以从单击的按钮元素中获取父tr,然后从该行中获取数据。
我还注意到,按钮上的数据ID似乎一团糟。看起来像这样:data-id='s-"0"'
。我不确定您是如何生成此行的,但是如果您将其修复,并且看起来像data-id="0"
,则可以使用$('#example').DataTable().row($(this).data('id')).data();
选择行。
如您所见,这里有很多可能性。