doupang3062 2017-06-22 13:40
浏览 62
已采纳

too long

I want to apply conditions on data-column-id which is being fetched from php code.Is this possible to do something like this?

if(data-column-id)==0{
data-column-id="ordinary";
}else{
data-column-id="ordinary";
}// i want this for cat_type

table

<table id="categories_grid" class="table table-condensed table-hover table-striped" data-toggle="bootgrid">
        <thead>
            <tr>
                <th data-column-id="cat_id" data-type="numeric" data-identifier="true">CatID</th>
                <th data-column-id="cat_name">Name</th>
                <th data-column-id="cat_type">Type</th>
                <th data-column-id="commands" data-formatter="commands" data-sortable="false">Commands</th>
            </tr>
        </thead>
 </table>

AJAX

$( document ).ready(function() {
        var grid = $("#categories_grid").bootgrid({

            ajax: true,
            rowSelect: true,
            post: function ()
            {
                /* To accumulate custom parameter with the request object */
                return {
                    id: "b0df282a-0d67-40e5-8558-c9e93b7befed"

                };
            },

            url: "response_categories.php",
            formatters: {
                "commands": function(column, row)
                {
                    return "<button type=\"button\" class=\"btn btn-xs btn-default command-edit\" data-row-id=\"" + row.cat_id + "\"><span class=\"glyphicon glyphicon-edit\"></span></button> " +
                        "<button type=\"button\" class=\"btn btn-xs btn-default command-delete\" data-row-id=\"" + row.cat_id + "\"><span class=\"glyphicon glyphicon-trash\"></span></button>";
                }
               /* "type":function (column,row) {
                    if(row.cat_type == 0)
                    {
                        return "ordinary";
                    }
                    else
                        return "special";

                }*/
            }
        }).on("loaded.rs.jquery.bootgrid", function()
  • 写回答

1条回答 默认 最新

  • dongmubi4444 2017-06-22 14:04
    关注

    A check like this, and the setting of the data attribute can be done via JavaScript. Here I will be using jQuery.

    Given a DOM element:

    <th id='test_id' data-column-id="cat_id" data-type="numeric" data-identifier="true">CatID</th>
    

    We can select it, and its data attributes as such. In order to select it more easily and for example purposes I've added an id to the table header above. (test_id)

    var column_id = $('#test_id').data('column-id');
    

    Now that we have its value we can perform the check you wish:

    if(column_id == 0){
        // logic
    }else{
        // logic
    }
    

    In order to set a data attribute you simply do the following:

    $('#test_id').data('column-id', 'some value');
    

    Following this statement your DOM element becomes:

    <th id='test_id' data-column-id="some value" data-type="numeric" data-identifier="true">CatID</th>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊