doupang3062
2017-06-22 13:40 阅读 55
已采纳

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 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>
    
    点赞 评论 复制链接分享

相关推荐